/
logging_fs.go
157 lines (130 loc) · 3.66 KB
/
logging_fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package vfs
import (
"io"
"os"
)
// WithLogging wraps an FS and logs filesystem modification operations to the
// given logFn.
func WithLogging(fs FS, logFn LogFn) FS {
return &loggingFS{
FS: fs,
logFn: logFn,
}
}
// LogFn is a function that is used to capture a log when WithLogging is used.
type LogFn func(fmt string, args ...interface{})
type loggingFS struct {
FS
logFn LogFn
}
var _ FS = (*loggingFS)(nil)
func (fs *loggingFS) Create(name string) (File, error) {
fs.logFn("create: %s", name)
f, err := fs.FS.Create(name)
if err != nil {
return nil, err
}
return newLoggingFile(f, name, fs.logFn), nil
}
func (fs *loggingFS) Open(name string, opts ...OpenOption) (File, error) {
fs.logFn("open: %s", name)
f, err := fs.FS.Open(name, opts...)
if err != nil {
return nil, err
}
return newLoggingFile(f, name, fs.logFn), nil
}
func (fs *loggingFS) OpenReadWrite(name string, opts ...OpenOption) (File, error) {
fs.logFn("open-read-write: %s", name)
f, err := fs.FS.OpenReadWrite(name, opts...)
if err != nil {
return nil, err
}
return newLoggingFile(f, name, fs.logFn), nil
}
func (fs *loggingFS) Link(oldname, newname string) error {
fs.logFn("link: %s -> %s", oldname, newname)
return fs.FS.Link(oldname, newname)
}
func (fs *loggingFS) OpenDir(name string) (File, error) {
fs.logFn("open-dir: %s", name)
f, err := fs.FS.OpenDir(name)
if err != nil {
return nil, err
}
return newLoggingFile(f, name, fs.logFn), nil
}
func (fs *loggingFS) Rename(oldname, newname string) error {
fs.logFn("rename: %s -> %s", oldname, newname)
return fs.FS.Rename(oldname, newname)
}
func (fs *loggingFS) ReuseForWrite(oldname, newname string) (File, error) {
fs.logFn("reuseForWrite: %s -> %s", oldname, newname)
f, err := fs.FS.ReuseForWrite(oldname, newname)
if err != nil {
return nil, err
}
return newLoggingFile(f, newname, fs.logFn), nil
}
func (fs *loggingFS) MkdirAll(dir string, perm os.FileMode) error {
fs.logFn("mkdir-all: %s %#o", dir, perm)
return fs.FS.MkdirAll(dir, perm)
}
func (fs *loggingFS) Lock(name string) (io.Closer, error) {
fs.logFn("lock: %s", name)
return fs.FS.Lock(name)
}
func (fs loggingFS) Remove(name string) error {
fs.logFn("remove: %s", name)
err := fs.FS.Remove(name)
return err
}
func (fs loggingFS) RemoveAll(name string) error {
fs.logFn("remove-all: %s", name)
err := fs.FS.RemoveAll(name)
return err
}
type loggingFile struct {
File
name string
logFn LogFn
}
var _ File = (*loggingFile)(nil)
func newLoggingFile(f File, name string, logFn LogFn) *loggingFile {
return &loggingFile{
File: f,
name: name,
logFn: logFn,
}
}
func (f *loggingFile) Close() error {
f.logFn("close: %s", f.name)
return f.File.Close()
}
func (f *loggingFile) Sync() error {
f.logFn("sync: %s", f.name)
return f.File.Sync()
}
func (f *loggingFile) SyncData() error {
f.logFn("sync-data: %s", f.name)
return f.File.SyncData()
}
func (f *loggingFile) SyncTo(length int64) (fullSync bool, err error) {
f.logFn("sync-to(%d): %s", length, f.name)
return f.File.SyncTo(length)
}
func (f *loggingFile) ReadAt(p []byte, offset int64) (int, error) {
f.logFn("read-at(%d, %d): %s", offset, len(p), f.name)
return f.File.ReadAt(p, offset)
}
func (f *loggingFile) WriteAt(p []byte, offset int64) (int, error) {
f.logFn("write-at(%d, %d): %s", offset, len(p), f.name)
return f.File.WriteAt(p, offset)
}
func (f *loggingFile) Prefetch(offset int64, length int64) error {
f.logFn("prefetch(%d, %d): %s", offset, length, f.name)
return f.File.Prefetch(offset, length)
}