OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package iotest | 5 package iotest |
6 | 6 |
7 import ( | 7 import ( |
8 "io" | 8 "io" |
9 "log" | 9 "log" |
10 "os" | |
11 ) | 10 ) |
12 | 11 |
13 type writeLogger struct { | 12 type writeLogger struct { |
14 prefix string | 13 prefix string |
15 w io.Writer | 14 w io.Writer |
16 } | 15 } |
17 | 16 |
18 func (l *writeLogger) Write(p []byte) (n int, err os.Error) { | 17 func (l *writeLogger) Write(p []byte) (n int, err error) { |
19 n, err = l.w.Write(p) | 18 n, err = l.w.Write(p) |
20 if err != nil { | 19 if err != nil { |
21 log.Printf("%s %x: %v", l.prefix, p[0:n], err) | 20 log.Printf("%s %x: %v", l.prefix, p[0:n], err) |
22 } else { | 21 } else { |
23 log.Printf("%s %x", l.prefix, p[0:n]) | 22 log.Printf("%s %x", l.prefix, p[0:n]) |
24 } | 23 } |
25 return | 24 return |
26 } | 25 } |
27 | 26 |
28 // NewWriteLogger returns a writer that behaves like w except | 27 // NewWriteLogger returns a writer that behaves like w except |
29 // that it logs (using log.Printf) each write to standard error, | 28 // that it logs (using log.Printf) each write to standard error, |
30 // printing the prefix and the hexadecimal data written. | 29 // printing the prefix and the hexadecimal data written. |
31 func NewWriteLogger(prefix string, w io.Writer) io.Writer { | 30 func NewWriteLogger(prefix string, w io.Writer) io.Writer { |
32 return &writeLogger{prefix, w} | 31 return &writeLogger{prefix, w} |
33 } | 32 } |
34 | 33 |
35 type readLogger struct { | 34 type readLogger struct { |
36 prefix string | 35 prefix string |
37 r io.Reader | 36 r io.Reader |
38 } | 37 } |
39 | 38 |
40 func (l *readLogger) Read(p []byte) (n int, err os.Error) { | 39 func (l *readLogger) Read(p []byte) (n int, err error) { |
41 n, err = l.r.Read(p) | 40 n, err = l.r.Read(p) |
42 if err != nil { | 41 if err != nil { |
43 log.Printf("%s %x: %v", l.prefix, p[0:n], err) | 42 log.Printf("%s %x: %v", l.prefix, p[0:n], err) |
44 } else { | 43 } else { |
45 log.Printf("%s %x", l.prefix, p[0:n]) | 44 log.Printf("%s %x", l.prefix, p[0:n]) |
46 } | 45 } |
47 return | 46 return |
48 } | 47 } |
49 | 48 |
50 // NewReadLogger returns a reader that behaves like r except | 49 // NewReadLogger returns a reader that behaves like r except |
51 // that it logs (using log.Print) each read to standard error, | 50 // that it logs (using log.Print) each read to standard error, |
52 // printing the prefix and the hexadecimal data written. | 51 // printing the prefix and the hexadecimal data written. |
53 func NewReadLogger(prefix string, r io.Reader) io.Reader { | 52 func NewReadLogger(prefix string, r io.Reader) io.Reader { |
54 return &readLogger{prefix, r} | 53 return &readLogger{prefix, r} |
55 } | 54 } |
OLD | NEW |