2023-02-01 09:10:03 +03:00
|
|
|
package terraform_module_test_helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2023-02-01 14:35:12 +03:00
|
|
|
|
|
|
|
"github.com/gruntwork-io/terratest/modules/logger"
|
|
|
|
"github.com/gruntwork-io/terratest/modules/testing"
|
2023-02-01 09:10:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ logger.TestLogger = new(StreamLogger)
|
|
|
|
|
2023-02-02 06:51:42 +03:00
|
|
|
var serializedLogger = func() *StreamLogger {
|
|
|
|
l := NewStreamLogger(os.Stdout)
|
|
|
|
l.outputProgress = false
|
|
|
|
return l
|
|
|
|
}()
|
2023-02-01 09:10:03 +03:00
|
|
|
|
|
|
|
type StreamLogger struct {
|
2023-02-02 06:51:42 +03:00
|
|
|
stream io.ReadWriter
|
|
|
|
mu *sync.Mutex
|
|
|
|
logCount int
|
|
|
|
outputProgress bool
|
2023-02-01 09:10:03 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 06:51:42 +03:00
|
|
|
func NewMemoryLogger() *StreamLogger {
|
2023-02-01 09:10:03 +03:00
|
|
|
buff := new(bytes.Buffer)
|
2023-02-02 06:51:42 +03:00
|
|
|
return NewStreamLogger(buff)
|
2023-02-01 09:10:03 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 06:51:42 +03:00
|
|
|
func NewStreamLogger(stream io.ReadWriter) *StreamLogger {
|
2023-02-01 09:10:03 +03:00
|
|
|
return &StreamLogger{
|
2023-02-02 06:51:42 +03:00
|
|
|
stream: stream,
|
|
|
|
mu: new(sync.Mutex),
|
|
|
|
outputProgress: true,
|
2023-02-01 09:10:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StreamLogger) Logf(t testing.TestingT, format string, args ...interface{}) {
|
2023-02-02 06:54:12 +03:00
|
|
|
log := fmt.Sprintf(format, args...)
|
|
|
|
logger.DoLog(t, 3, s.stream, log)
|
2023-02-02 06:23:36 +03:00
|
|
|
s.logCount++
|
2023-02-02 06:51:42 +03:00
|
|
|
if s.outputProgress && s.logCount%50 == 0 {
|
2023-02-02 06:54:12 +03:00
|
|
|
logger.Log(t, fmt.Sprintf("logging sample: %s", log))
|
2023-02-02 06:23:36 +03:00
|
|
|
}
|
2023-02-01 09:10:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-02-01 14:35:12 +03:00
|
|
|
defer func() {
|
|
|
|
c, ok := s.stream.(io.Closer)
|
|
|
|
if ok {
|
|
|
|
_ = c.Close()
|
|
|
|
}
|
|
|
|
}()
|
2023-02-01 09:10:03 +03:00
|
|
|
return serializedLogger.PipeFrom(s)
|
|
|
|
}
|