From e14a4f9be799ca40749bf220843df8812a84dd84 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 1 Apr 2019 11:40:32 +0900 Subject: [PATCH] add synchronization for calls to the buffer used for logging --- integrationtests/tools/testlog/testlog.go | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/integrationtests/tools/testlog/testlog.go b/integrationtests/tools/testlog/testlog.go index 0d86597be..0632ab764 100644 --- a/integrationtests/tools/testlog/testlog.go +++ b/integrationtests/tools/testlog/testlog.go @@ -15,10 +15,36 @@ import ( const logBufSize = 100 * 1 << 20 // initial size of the log buffer: 100 MB +type syncedBuffer struct { + mutex sync.Mutex + + *bytes.Buffer +} + +func (b *syncedBuffer) Write(p []byte) (int, error) { + b.mutex.Lock() + n, err := b.Buffer.Write(p) + b.mutex.Unlock() + return n, err +} + +func (b *syncedBuffer) Bytes() []byte { + b.mutex.Lock() + p := b.Buffer.Bytes() + b.mutex.Unlock() + return p +} + +func (b *syncedBuffer) Reset() { + b.mutex.Lock() + b.Buffer.Reset() + b.mutex.Unlock() +} + var ( logFileName string // the log file set in the ginkgo flags logBufOnce sync.Once - logBuf *bytes.Buffer + logBuf *syncedBuffer ) // read the logfile command line flag @@ -32,7 +58,7 @@ var _ = BeforeEach(func() { if Debug() { logBufOnce.Do(func() { - logBuf = bytes.NewBuffer(make([]byte, 0, logBufSize)) + logBuf = &syncedBuffer{Buffer: bytes.NewBuffer(make([]byte, 0, logBufSize))} }) utils.DefaultLogger.SetLogLevel(utils.LogLevelDebug) log.SetOutput(logBuf)