/
log_writer.go
62 lines (52 loc) · 1.01 KB
/
log_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package fastesting
import (
"bufio"
"io"
"sync"
)
type logWriter struct {
bpr *bufio.Reader
pw io.WriteCloser
wg sync.WaitGroup
out logf
}
type logf interface {
Logf(string, ...interface{})
}
// newLogWriter returns a io.WriteCloser which will take all lines written to
// it and call out.Logf with it. This is currently used with the FAST DumpLastOutput
// to print the output of a command to the test logger.
func newLogWriter(out logf) io.WriteCloser {
pr, pw := io.Pipe()
bpr := bufio.NewReader(pr)
p := &logWriter{
pw: pw,
bpr: bpr,
out: out,
}
p.wg.Add(1)
go p.writeOut()
return p
}
func (p *logWriter) writeOut() {
defer p.wg.Done()
for {
l, err := p.bpr.ReadBytes('\n')
if len(l) != 0 {
p.out.Logf(string(l))
}
if err != nil {
break
}
}
}
// Write the bytes b using t.Logf on each full line
func (p *logWriter) Write(b []byte) (int, error) {
return p.pw.Write(b)
}
// Close the writer
func (p *logWriter) Close() error {
err := p.pw.Close()
p.wg.Wait()
return err
}