-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstream_logger.go
66 lines (56 loc) · 1.33 KB
/
stream_logger.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
63
64
65
66
package terraform_module_test_helper
import (
"bytes"
"fmt"
"io"
"os"
"sync"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
)
var _ logger.TestLogger = new(StreamLogger)
var serializedLogger = func() *StreamLogger {
l := NewStreamLogger(os.Stdout)
l.outputProgress = false
return l
}()
type StreamLogger struct {
stream io.ReadWriter
mu *sync.Mutex
logCount int
outputProgress bool
}
func NewMemoryLogger() *StreamLogger {
buff := new(bytes.Buffer)
return NewStreamLogger(buff)
}
func NewStreamLogger(stream io.ReadWriter) *StreamLogger {
return &StreamLogger{
stream: stream,
mu: new(sync.Mutex),
outputProgress: true,
}
}
func (s *StreamLogger) Logf(t testing.TestingT, format string, args ...interface{}) {
log := fmt.Sprintf(format, args...)
logger.DoLog(t, 3, s.stream, log)
s.logCount++
if s.outputProgress && s.logCount%50 == 0 {
logger.Log(t, fmt.Sprintf("logging sample: %s", log))
}
}
func (s *StreamLogger) PipeFrom(srcLogger *StreamLogger) error {
s.mu.Lock()
defer s.mu.Unlock()
_, err := io.Copy(s.stream, srcLogger.stream)
return err
}
func (s *StreamLogger) Close() error {
defer func() {
c, ok := s.stream.(io.Closer)
if ok {
_ = c.Close()
}
}()
return serializedLogger.PipeFrom(s)
}