github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/fs/logger/logger.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "time" 8 9 "github.com/spf13/afero" 10 ) 11 12 // assert that logger.logger implements afero.Fs. 13 var _ afero.Fs = (*logger)(nil) 14 15 type logger struct { 16 w io.Writer 17 backend afero.Fs 18 } 19 20 func New(w io.Writer, fs afero.Fs) afero.Fs { 21 return &logger{ 22 w: w, 23 backend: fs, 24 } 25 } 26 27 func (l *logger) logf(fmtstr string, args ...interface{}) { 28 fmtstr = fmt.Sprintf("[%s] %s\n", l.backend.Name(), fmtstr) 29 fmt.Fprintf(l.w, fmtstr, args...) 30 } 31 32 // Create creates a file in the filesystem, returning the file and an 33 // error, if any happens. 34 func (l *logger) Create(name string) (afero.File, error) { 35 ret, err := l.backend.Create(name) 36 l.logf("Create(%s) %v", name, err) 37 return ret, err 38 } 39 40 // Mkdir creates a directory in the filesystem, return an error if any 41 // happens. 42 func (l *logger) Mkdir(name string, perm os.FileMode) error { 43 err := l.backend.Mkdir(name, perm) 44 l.logf("Mkdir(%s, %s) %v", name, perm, err) 45 return err 46 } 47 48 // MkdirAll creates a directory path and all parents that does not exist 49 // yet. 50 func (l *logger) MkdirAll(path string, perm os.FileMode) error { 51 err := l.backend.MkdirAll(path, perm) 52 l.logf("MkdirAll(%s, %s) %v", path, perm, err) 53 return err 54 } 55 56 // Open opens a file, returning it or an error, if any happens. 57 func (l *logger) Open(name string) (afero.File, error) { 58 ret, err := l.backend.Open(name) 59 l.logf("Open(%s) %v", name, err) 60 return ret, err 61 } 62 63 // OpenFile opens a file using the given flags and the given mode. 64 func (l *logger) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { 65 ret, err := l.backend.OpenFile(name, flag, perm) 66 l.logf("OpenFile(%s, %x, %s) %v", name, flag, perm, err) 67 return ret, err 68 } 69 70 // Remove removes a file identified by name, returning an error, if any 71 // happens. 72 func (l *logger) Remove(name string) error { 73 err := l.backend.Remove(name) 74 l.logf("Remove(%s) %v", name, err) 75 return err 76 } 77 78 // RemoveAll removes a directory path and any children it contains. It 79 // does not fail if the path does not exist (return nil). 80 func (l *logger) RemoveAll(path string) error { 81 err := l.backend.RemoveAll(path) 82 l.logf("RemoveAll(%s) %v", path, err) 83 return err 84 } 85 86 // Rename renames a file. 87 func (l *logger) Rename(oldname string, newname string) error { 88 err := l.backend.Rename(oldname, newname) 89 l.logf("Rename(%s, %s) %v", oldname, newname, err) 90 return err 91 } 92 93 // Stat returns a FileInfo describing the named file, or an error, if any 94 // happens. 95 func (l *logger) Stat(name string) (os.FileInfo, error) { 96 ret, err := l.backend.Stat(name) 97 l.logf("Stat(%s) %v", name, err) 98 return ret, err 99 } 100 101 // The name of this FileSystem 102 func (l *logger) Name() string { 103 return "logger" 104 } 105 106 //Chmod changes the mode of the named file to mode. 107 func (l *logger) Chmod(name string, mode os.FileMode) error { 108 err := l.backend.Chmod(name, mode) 109 l.logf("Chmod(%s, %s) %v", name, mode, err) 110 return err 111 } 112 113 // Chown changes the uid and gid of the named file. 114 func (l *logger) Chown(name string, uid, gid int) error { 115 err := l.backend.Chown(name, uid, gid) 116 l.logf("Chown(%s, %d, %d) %v", name, uid, gid, err) 117 return err 118 } 119 120 //Chtimes changes the access and modification times of the named file 121 func (l *logger) Chtimes(name string, atime time.Time, mtime time.Time) error { 122 err := l.backend.Chtimes(name, atime, mtime) 123 l.logf("Chtimes(%s, %d, %d) %v", name, atime.Unix(), mtime.Unix(), err) 124 return err 125 }