@@ -16,13 +16,19 @@ var GlobalLogDirectory *paths.Path
1616// that forward and logs all read/write/close operations on the given filename
1717// that is created in the GlobalLogDirectory.
1818func LogReadWriteCloserAs (upstream io.ReadWriteCloser , filename string ) io.ReadWriteCloser {
19- return & dumper {upstream , OpenLogFileAs (filename )}
19+ return & dumper {
20+ upstream : upstream ,
21+ logfile : OpenLogFileAs (filename ),
22+ }
2023}
2124
2225// LogReadWriteCloserToFile return a proxy for the given upstream io.ReadWriteCloser
2326// that forward and logs all read/write/close operations on the given file.
2427func LogReadWriteCloserToFile (upstream io.ReadWriteCloser , file * os.File ) io.ReadWriteCloser {
25- return & dumper {upstream , file }
28+ return & dumper {
29+ upstream : upstream ,
30+ logfile : file ,
31+ }
2632}
2733
2834// OpenLogFileAs creates a log file in GlobalLogDirectory.
@@ -41,14 +47,21 @@ func OpenLogFileAs(filename string) *os.File {
4147type dumper struct {
4248 upstream io.ReadWriteCloser
4349 logfile * os.File
50+ reading bool
51+ writing bool
4452}
4553
4654func (d * dumper ) Read (buff []byte ) (int , error ) {
4755 n , err := d .upstream .Read (buff )
4856 if err != nil {
4957 d .logfile .Write ([]byte (fmt .Sprintf ("<<< Read Error: %s\n " , err )))
5058 } else {
51- d .logfile .Write ([]byte (fmt .Sprintf ("<<< Read %d bytes:\n %s\n " , n , buff [:n ])))
59+ if ! d .reading {
60+ d .reading = true
61+ d .writing = false
62+ d .logfile .Write ([]byte ("\n <<<\n " ))
63+ }
64+ d .logfile .Write (buff [:n ])
5265 }
5366 return n , err
5467}
@@ -58,7 +71,12 @@ func (d *dumper) Write(buff []byte) (int, error) {
5871 if err != nil {
5972 _ , _ = d .logfile .Write ([]byte (fmt .Sprintf (">>> Write Error: %s\n " , err )))
6073 } else {
61- _ , _ = d .logfile .Write ([]byte (fmt .Sprintf (">>> Wrote %d bytes:\n %s\n " , n , buff [:n ])))
74+ if ! d .writing {
75+ d .writing = true
76+ d .reading = false
77+ d .logfile .Write ([]byte ("\n >>>\n " ))
78+ }
79+ _ , _ = d .logfile .Write (buff [:n ])
6280 }
6381 return n , err
6482}
0 commit comments