mirror of
https://github.com/tailscale/tailscale.git
synced 2025-01-08 09:07:44 +00:00
tstest: backport MemLogger from main
(it was part of things we don't want to cherry-pick) Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
3ff68e6784
commit
c293be6434
@ -12,6 +12,7 @@
|
|||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"go4.org/mem"
|
||||||
"tailscale.com/types/logger"
|
"tailscale.com/types/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -118,7 +119,30 @@ func (lt *LogLineTracker) Reset() {
|
|||||||
|
|
||||||
// Close closes lt. After calling Close, calls to Logf become no-ops.
|
// Close closes lt. After calling Close, calls to Logf become no-ops.
|
||||||
func (lt *LogLineTracker) Close() {
|
func (lt *LogLineTracker) Close() {
|
||||||
|
|
||||||
lt.mu.Lock()
|
lt.mu.Lock()
|
||||||
defer lt.mu.Unlock()
|
defer lt.mu.Unlock()
|
||||||
lt.closed = true
|
lt.closed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MemLogger is a bytes.Buffer with a Logf method for tests that want
|
||||||
|
// to log to a buffer.
|
||||||
|
type MemLogger struct {
|
||||||
|
sync.Mutex
|
||||||
|
bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ml *MemLogger) Logf(format string, args ...interface{}) {
|
||||||
|
ml.Lock()
|
||||||
|
defer ml.Unlock()
|
||||||
|
fmt.Fprintf(&ml.Buffer, format, args...)
|
||||||
|
if !mem.HasSuffix(mem.B(ml.Buffer.Bytes()), mem.S("\n")) {
|
||||||
|
ml.Buffer.WriteByte('\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ml *MemLogger) String() string {
|
||||||
|
ml.Lock()
|
||||||
|
defer ml.Unlock()
|
||||||
|
return ml.Buffer.String()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user