...
1
2
3
4
5 package iotest
6
7 import (
8 "io"
9 "log"
10 )
11
12 type writeLogger struct {
13 prefix string
14 w io.Writer
15 }
16
17 func (l *writeLogger) Write(p []byte) (n int, err error) {
18 n, err = l.w.Write(p)
19 if err != nil {
20 log.Printf("%s %x: %v", l.prefix, p[0:n], err)
21 } else {
22 log.Printf("%s %x", l.prefix, p[0:n])
23 }
24 return
25 }
26
27
28
29
30 func NewWriteLogger(prefix string, w io.Writer) io.Writer {
31 return &writeLogger{prefix, w}
32 }
33
34 type readLogger struct {
35 prefix string
36 r io.Reader
37 }
38
39 func (l *readLogger) Read(p []byte) (n int, err error) {
40 n, err = l.r.Read(p)
41 if err != nil {
42 log.Printf("%s %x: %v", l.prefix, p[0:n], err)
43 } else {
44 log.Printf("%s %x", l.prefix, p[0:n])
45 }
46 return
47 }
48
49
50
51
52 func NewReadLogger(prefix string, r io.Reader) io.Reader {
53 return &readLogger{prefix, r}
54 }
55
View as plain text