tstest: add MemLogger bytes.Buffer wrapper with Logf method

We use it tons of places. Updated three at least in this PR.

Another use in next commit.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2021-09-07 15:17:39 -07:00
committed by Brad Fitzpatrick
parent 30a614f9b9
commit 4c68b7df7c
4 changed files with 26 additions and 20 deletions

View File

@ -12,6 +12,7 @@ import (
"sync"
"testing"
"go4.org/mem"
"tailscale.com/types/logger"
)
@ -122,3 +123,16 @@ func (lt *LogLineTracker) Close() {
defer lt.mu.Unlock()
lt.closed = true
}
// MemLogger is a bytes.Buffer with a Logf method for tests that want
// to log to a buffer.
type MemLogger struct {
bytes.Buffer
}
func (ml *MemLogger) Logf(format string, args ...interface{}) {
fmt.Fprintf(&ml.Buffer, format, args...)
if !mem.HasSuffix(mem.B(ml.Buffer.Bytes()), mem.S("\n")) {
ml.Buffer.WriteByte('\n')
}
}