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