github.com/gaukas/wazerofs@v0.1.0/wraplogfs/file.go (about) 1 package wraplogfs 2 3 import ( 4 "fmt" 5 "io" 6 "log" 7 8 expsys "github.com/tetratelabs/wazero/experimental/sys" 9 wasys "github.com/tetratelabs/wazero/sys" 10 ) 11 12 // fileWithLog implements expsys.File that is instrumented with logging 13 type fileWithLog struct { 14 stdlog *log.Logger 15 base expsys.File 16 writeBytes bool 17 18 name string 19 fsName string 20 } 21 22 func (d fileWithLog) log(nm string) func(format string, params ...any) { 23 return func(format string, params ...any) { 24 txt := fmt.Sprintf(format, params...) 25 txt = fmt.Sprintf("WrapLogFile %s %s %s: %s", d.fsName, d.name, nm, txt) 26 d.stdlog.Println(txt) 27 } 28 } 29 30 // Close implements expsys.File 31 func (d fileWithLog) Close() (e1 expsys.Errno) { 32 l := d.log("Chmod") 33 l("calling with params: <>") 34 defer func() { 35 l("returned results: %s", e1) 36 }() 37 return d.base.Close() 38 } 39 40 // Datasync implements expsys.File 41 func (d fileWithLog) Datasync() (e1 expsys.Errno) { 42 l := d.log("Datasync") 43 l("calling with params: <>") 44 defer func() { 45 l("returned results: %s", e1) 46 }() 47 return d.base.Datasync() 48 } 49 50 // Dev implements expsys.File 51 func (d fileWithLog) Dev() (u1 uint64, e1 expsys.Errno) { 52 l := d.log("Dev") 53 l("calling with params: <>") 54 defer func() { 55 l("returned results: %s %s", u1, e1) 56 }() 57 return d.base.Dev() 58 } 59 60 // Ino implements expsys.File 61 func (d fileWithLog) Ino() (i1 wasys.Inode, e1 expsys.Errno) { 62 l := d.log("Ino") 63 l("calling with params: <>") 64 defer func() { 65 l("returned results: %s %s", i1, e1) 66 }() 67 return d.base.Ino() 68 } 69 70 // IsAppend implements expsys.File 71 func (d fileWithLog) IsAppend() (b1 bool) { 72 l := d.log("IsAppend") 73 l("calling with params: <>") 74 75 defer func() { 76 l("returned results: %t", b1) 77 }() 78 return d.base.IsAppend() 79 } 80 81 // IsDir implements expsys.File 82 func (d fileWithLog) IsDir() (b1 bool, e1 expsys.Errno) { 83 l := d.log("IsDir") 84 l("calling with params: <>") 85 86 defer func() { 87 l("returned results: %t %s", b1, e1) 88 }() 89 return d.base.IsDir() 90 } 91 92 // Pread implements expsys.File 93 func (d fileWithLog) Pread(buf []byte, off int64) (n int, errno expsys.Errno) { 94 l := d.log("Pread") 95 96 if d.writeBytes { 97 l("calling with params: %v %d", buf, off) 98 } else { 99 l("calling with params: (none) %d", off) 100 } 101 defer func() { 102 if d.writeBytes { 103 l("returned results: %d %s, buffer: %v", n, errno, buf) 104 } else { 105 l("returned results: %d %s", n, errno) 106 } 107 }() 108 return d.base.Pread(buf, off) 109 } 110 111 // Pwrite implements expsys.File 112 func (d fileWithLog) Pwrite(buf []byte, off int64) (n int, errno expsys.Errno) { 113 l := d.log("Pwrite") 114 115 if d.writeBytes { 116 l("calling with params: %v %d", buf, off) 117 } else { 118 l("calling with params: (none) %d", off) 119 } 120 121 defer func() { 122 l("returned results: %d %s", n, errno) 123 }() 124 return d.base.Pwrite(buf, off) 125 } 126 127 // Read implements expsys.File 128 func (d fileWithLog) Read(buf []byte) (n int, errno expsys.Errno) { 129 l := d.log("Read") 130 131 if d.writeBytes { 132 l("calling with params: %v", buf) 133 } else { 134 l("calling with params: (none)") 135 } 136 defer func() { 137 if d.writeBytes { 138 l("returned results: %d %s, buffer: %v", n, errno, buf) 139 } else { 140 l("returned results: %d %s", n, errno) 141 } 142 }() 143 return d.base.Read(buf) 144 } 145 146 // Readdir implements expsys.File 147 func (d fileWithLog) Readdir(n int) (dirents []expsys.Dirent, errno expsys.Errno) { 148 l := d.log("Readdir") 149 l("calling with params: %d", n) 150 defer func() { 151 l("returned results: %+v %s", dirents, errno) 152 }() 153 return d.base.Readdir(n) 154 } 155 156 func printWhence(whence int) string { 157 switch whence { 158 case io.SeekStart: 159 return "SeekStart" 160 case io.SeekEnd: 161 return "SeekEnd" 162 case io.SeekCurrent: 163 return "SeekCurrent" 164 default: 165 return fmt.Sprintf("Invalid whence (%d)", whence) 166 } 167 } 168 169 // Seek implements expsys.File 170 func (d fileWithLog) Seek(offset int64, whence int) (newOffset int64, errno expsys.Errno) { 171 l := d.log("Seek") 172 l("calling with params: %d %s", offset, printWhence(whence)) 173 174 defer func() { 175 l("returned results: %d %s", newOffset, errno) 176 }() 177 return d.base.Seek(offset, whence) 178 } 179 180 // SetAppend implements expsys.File 181 func (d fileWithLog) SetAppend(enable bool) (e1 expsys.Errno) { 182 l := d.log("SetAppend") 183 l("calling with params: %t", enable) 184 185 defer func() { 186 l("returned results: %d %s", e1) 187 }() 188 return d.base.SetAppend(enable) 189 } 190 191 // Stat implements expsys.File 192 func (d fileWithLog) Stat() (s1 wasys.Stat_t, e1 expsys.Errno) { 193 l := d.log("Stat") 194 l("calling with params: <>") 195 196 defer func() { 197 l("returned results: %+v %s", s1, e1) 198 }() 199 return d.base.Stat() 200 } 201 202 // Sync implements expsys.File 203 func (d fileWithLog) Sync() (e1 expsys.Errno) { 204 l := d.log("Sync") 205 l("calling with params: <>") 206 defer func() { 207 l("returned results: %s", e1) 208 209 }() 210 return d.base.Sync() 211 } 212 213 // Truncate implements expsys.File 214 func (d fileWithLog) Truncate(size int64) (e1 expsys.Errno) { 215 l := d.log("Truncate") 216 l("calling with params: %d", size) 217 218 defer func() { 219 l("returned results: %s", e1) 220 }() 221 return d.base.Truncate(size) 222 } 223 224 // Utimens implements expsys.File 225 func (d fileWithLog) Utimens(atim int64, mtim int64) (e1 expsys.Errno) { 226 l := d.log("Utimens") 227 l("calling with params: %d %d", atim, mtim) 228 defer func() { 229 l("returned results: %s", e1) 230 }() 231 return d.base.Utimens(atim, mtim) 232 } 233 234 // Write implements expsys.File 235 func (d fileWithLog) Write(buf []byte) (n int, errno expsys.Errno) { 236 l := d.log("Write") 237 238 if d.writeBytes { 239 l("calling with params: %v", buf) 240 } else { 241 l("calling with params: (none)") 242 } 243 244 defer func() { 245 l("returned results: %d %s", n, errno) 246 }() 247 return d.base.Write(buf) 248 }