2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-06 22:14:30 +00:00
|
|
|
|
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-19 18:21:07 +00:00
|
|
|
"sync"
|
2020-08-06 22:14:30 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TranscriptLogger struct {
|
|
|
|
t *testing.T
|
2020-08-19 18:21:07 +00:00
|
|
|
lock sync.Mutex
|
|
|
|
transcript []TranscriptLogMessage
|
2020-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ logr.Logger = &TranscriptLogger{}
|
|
|
|
|
|
|
|
type TranscriptLogMessage struct {
|
|
|
|
Level string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTranscriptLogger(t *testing.T) *TranscriptLogger {
|
|
|
|
return &TranscriptLogger{t: t}
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:21:07 +00:00
|
|
|
func (log *TranscriptLogger) Transcript() []TranscriptLogMessage {
|
|
|
|
log.lock.Lock()
|
|
|
|
defer log.lock.Unlock()
|
|
|
|
result := make([]TranscriptLogMessage, 0, len(log.transcript))
|
|
|
|
result = append(result, log.transcript...)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:14:30 +00:00
|
|
|
func (log *TranscriptLogger) Info(msg string, keysAndValues ...interface{}) {
|
2020-08-19 18:21:07 +00:00
|
|
|
log.lock.Lock()
|
|
|
|
defer log.lock.Unlock()
|
|
|
|
log.transcript = append(log.transcript, TranscriptLogMessage{
|
2020-08-06 22:14:30 +00:00
|
|
|
Level: "info",
|
|
|
|
Message: fmt.Sprintf(msg, keysAndValues...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-26 01:22:53 +00:00
|
|
|
func (log *TranscriptLogger) Error(_ error, msg string, _ ...interface{}) {
|
2020-08-19 18:21:07 +00:00
|
|
|
log.lock.Lock()
|
|
|
|
defer log.lock.Unlock()
|
|
|
|
log.transcript = append(log.transcript, TranscriptLogMessage{
|
2020-08-06 22:14:30 +00:00
|
|
|
Level: "error",
|
2020-08-26 01:22:53 +00:00
|
|
|
Message: msg,
|
2020-08-06 22:14:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*TranscriptLogger) Enabled() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *TranscriptLogger) V(_ int) logr.Logger {
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *TranscriptLogger) WithName(_ string) logr.Logger {
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *TranscriptLogger) WithValues(_ ...interface{}) logr.Logger {
|
|
|
|
return log
|
|
|
|
}
|