summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorEtienne Perot <eperot@google.com>2021-01-13 15:07:24 -0800
committergVisor bot <gvisor-bot@google.com>2021-01-13 15:09:50 -0800
commitf34aaf7ef17aa10c7ba1923d0694347e47634192 (patch)
treead2f01cdd0685ad1a64f7d9b91f6d79bf7772186
parent64bff178b8a3091a79d7a3cc7c255542fdd3fd80 (diff)
testutil: Create a `multiLogger` that logs to multiple `Loggers`.
This is useful when using the shell library in order to log to both the test log and the standard logs. PiperOrigin-RevId: 351673465
-rw-r--r--pkg/test/testutil/testutil.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/pkg/test/testutil/testutil.go b/pkg/test/testutil/testutil.go
index fdd416b5e..2b2f95c13 100644
--- a/pkg/test/testutil/testutil.go
+++ b/pkg/test/testutil/testutil.go
@@ -111,6 +111,30 @@ func (d DefaultLogger) Logf(fmt string, args ...interface{}) {
log.Printf(fmt, args...)
}
+// multiLogger logs to multiple Loggers.
+type multiLogger []Logger
+
+// Name implements Logger.Name.
+func (m multiLogger) Name() string {
+ names := make([]string, len(m))
+ for i, l := range m {
+ names[i] = l.Name()
+ }
+ return strings.Join(names, "+")
+}
+
+// Logf implements Logger.Logf.
+func (m multiLogger) Logf(fmt string, args ...interface{}) {
+ for _, l := range m {
+ l.Logf(fmt, args...)
+ }
+}
+
+// NewMultiLogger returns a new Logger that logs on multiple Loggers.
+func NewMultiLogger(loggers ...Logger) Logger {
+ return multiLogger(loggers)
+}
+
// Cmd is a simple wrapper.
type Cmd struct {
logger Logger