Skip to content

Commit

Permalink
feat(testingx): add log collector (#1562)
Browse files Browse the repository at this point in the history
Closes ooni/probe#2713.

We developed this diff in #1560,
which is work in the ooni/probe#2700 context.
  • Loading branch information
bassosimone authored Apr 23, 2024
1 parent d12a3c2 commit 6bd35a2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
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)
}
}

0 comments on commit 6bd35a2

Please sign in to comment.