-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testingx): add log collector (#1562)
Closes ooni/probe#2713. We developed this diff in #1560, which is work in the ooni/probe#2700 context.
- Loading branch information
1 parent
d12a3c2
commit 6bd35a2
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |