Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testingx): add log collector #1562

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions internal/testingx/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package testingx

import (
"fmt"
"sync"

"github.com/ooni/probe-cli/v3/internal/logmodel"
)

// Logger implements [logmodel.Logger] and collects all the log lines.
//
// The zero value of this struct is ready to use.
type Logger struct {
// debug contains debug lines.
debug []string

// info contains info lines.
info []string

// mu provides mutual exclusion.
mu sync.Mutex

// warning contains warning lines.
warning []string
}

var _ logmodel.Logger = &Logger{}

// Debug implements logmodel.Logger.
func (l *Logger) Debug(msg string) {
l.mu.Lock()
l.debug = append(l.debug, msg)
l.mu.Unlock()
}

// Debugf implements logmodel.Logger.
func (l *Logger) Debugf(format string, v ...interface{}) {
l.Debug(fmt.Sprintf(format, v...))
}

// Info implements logmodel.Logger.
func (l *Logger) Info(msg string) {
l.mu.Lock()
l.info = append(l.info, msg)
l.mu.Unlock()
}

// Infof implements logmodel.Logger.
func (l *Logger) Infof(format string, v ...interface{}) {
l.Info(fmt.Sprintf(format, v...))
}

// Warn implements logmodel.Logger.
func (l *Logger) Warn(msg string) {
l.mu.Lock()
l.warning = append(l.warning, msg)
l.mu.Unlock()
}

// Warnf implements logmodel.Logger.
func (l *Logger) Warnf(format string, v ...interface{}) {
l.Warn(fmt.Sprintf(format, v...))
}

// DebugLines returns a copy of the observed debug lines.
func (l *Logger) DebugLines() []string {
l.mu.Lock()
out := append([]string{}, l.debug...)
l.mu.Unlock()
return out
}

// InfoLines returns a copy of the observed info lines.
func (l *Logger) InfoLines() []string {
l.mu.Lock()
out := append([]string{}, l.info...)
l.mu.Unlock()
return out
}

// WarnLines returns a copy of the observed warn lines.
func (l *Logger) WarnLines() []string {
l.mu.Lock()
out := append([]string{}, l.warning...)
l.mu.Unlock()
return out
}
33 changes: 33 additions & 0 deletions internal/testingx/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package testingx

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestLogger(t *testing.T) {
logger := &Logger{}

logger.Debug("foobar")
logger.Debugf("foo%s", "baz")
expectDebug := []string{"foobar", "foobaz"}

logger.Info("barfoo")
logger.Infof("bar%s", "baz")
expectInfo := []string{"barfoo", "barbaz"}

logger.Warn("jarjar")
logger.Warnf("jar%s", "baz")
expectWarn := []string{"jarjar", "jarbaz"}

if diff := cmp.Diff(expectDebug, logger.DebugLines()); diff != "" {
t.Fatal(diff)
}
if diff := cmp.Diff(expectInfo, logger.InfoLines()); diff != "" {
t.Fatal(diff)
}
if diff := cmp.Diff(expectWarn, logger.WarnLines()); diff != "" {
t.Fatal(diff)
}
}
Loading