Commit aeb8125b authored by protolambda's avatar protolambda Committed by GitHub

op-node: testlog now uses interface, to swap testing backend easily (#3600)

Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent d7f8d89b
...@@ -22,18 +22,24 @@ import ( ...@@ -22,18 +22,24 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"testing"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
// Testing interface to log to. Some functions are marked as Helper function to log the call site accurately.
// Standard Go testing.TB implements this, as well as Hive and other Go-like test frameworks.
type Testing interface {
Logf(format string, args ...any)
Helper()
}
// Handler returns a log handler which logs to the unit test log of t. // Handler returns a log handler which logs to the unit test log of t.
func Handler(t *testing.T, level log.Lvl) log.Handler { func Handler(t Testing, level log.Lvl) log.Handler {
return log.LvlFilterHandler(level, &handler{t, log.TerminalFormat(false)}) return log.LvlFilterHandler(level, &handler{t, log.TerminalFormat(false)})
} }
type handler struct { type handler struct {
t *testing.T t Testing
fmt log.Format fmt log.Format
} }
...@@ -47,7 +53,7 @@ func (h *handler) Log(r *log.Record) error { ...@@ -47,7 +53,7 @@ func (h *handler) Log(r *log.Record) error {
// helpers, so the file and line number in unit test output correspond to the call site // helpers, so the file and line number in unit test output correspond to the call site
// which emitted the log message. // which emitted the log message.
type logger struct { type logger struct {
t *testing.T t Testing
l log.Logger l log.Logger
mu *sync.Mutex mu *sync.Mutex
h *bufHandler h *bufHandler
...@@ -64,7 +70,7 @@ func (h *bufHandler) Log(r *log.Record) error { ...@@ -64,7 +70,7 @@ func (h *bufHandler) Log(r *log.Record) error {
} }
// Logger returns a logger which logs to the unit test log of t. // Logger returns a logger which logs to the unit test log of t.
func Logger(t *testing.T, level log.Lvl) log.Logger { func Logger(t Testing, level log.Lvl) log.Logger {
l := &logger{ l := &logger{
t: t, t: t,
l: log.New(), l: log.New(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment