github.com/bcnmy/go-ethereum@v1.10.27/internal/testlog/testlog.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package testlog provides a log handler for unit tests.
    18  package testlog
    19  
    20  import (
    21  	"sync"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/log"
    25  )
    26  
    27  // Handler returns a log handler which logs to the unit test log of t.
    28  func Handler(t *testing.T, level log.Lvl) log.Handler {
    29  	return log.LvlFilterHandler(level, &handler{t, log.TerminalFormat(false)})
    30  }
    31  
    32  type handler struct {
    33  	t   *testing.T
    34  	fmt log.Format
    35  }
    36  
    37  func (h *handler) Log(r *log.Record) error {
    38  	h.t.Logf("%s", h.fmt.Format(r))
    39  	return nil
    40  }
    41  
    42  // logger implements log.Logger such that all output goes to the unit test log via
    43  // t.Logf(). All methods in between logger.Trace, logger.Debug, etc. are marked as test
    44  // helpers, so the file and line number in unit test output correspond to the call site
    45  // which emitted the log message.
    46  type logger struct {
    47  	t  *testing.T
    48  	l  log.Logger
    49  	mu *sync.Mutex
    50  	h  *bufHandler
    51  }
    52  
    53  type bufHandler struct {
    54  	buf []*log.Record
    55  	fmt log.Format
    56  }
    57  
    58  func (h *bufHandler) Log(r *log.Record) error {
    59  	h.buf = append(h.buf, r)
    60  	return nil
    61  }
    62  
    63  // Logger returns a logger which logs to the unit test log of t.
    64  func Logger(t *testing.T, level log.Lvl) log.Logger {
    65  	l := &logger{
    66  		t:  t,
    67  		l:  log.New(),
    68  		mu: new(sync.Mutex),
    69  		h:  &bufHandler{fmt: log.TerminalFormat(false)},
    70  	}
    71  	l.l.SetHandler(log.LvlFilterHandler(level, l.h))
    72  	return l
    73  }
    74  
    75  func (l *logger) Trace(msg string, ctx ...interface{}) {
    76  	l.t.Helper()
    77  	l.mu.Lock()
    78  	defer l.mu.Unlock()
    79  	l.l.Trace(msg, ctx...)
    80  	l.flush()
    81  }
    82  
    83  func (l *logger) Debug(msg string, ctx ...interface{}) {
    84  	l.t.Helper()
    85  	l.mu.Lock()
    86  	defer l.mu.Unlock()
    87  	l.l.Debug(msg, ctx...)
    88  	l.flush()
    89  }
    90  
    91  func (l *logger) Info(msg string, ctx ...interface{}) {
    92  	l.t.Helper()
    93  	l.mu.Lock()
    94  	defer l.mu.Unlock()
    95  	l.l.Info(msg, ctx...)
    96  	l.flush()
    97  }
    98  
    99  func (l *logger) Warn(msg string, ctx ...interface{}) {
   100  	l.t.Helper()
   101  	l.mu.Lock()
   102  	defer l.mu.Unlock()
   103  	l.l.Warn(msg, ctx...)
   104  	l.flush()
   105  }
   106  
   107  func (l *logger) Error(msg string, ctx ...interface{}) {
   108  	l.t.Helper()
   109  	l.mu.Lock()
   110  	defer l.mu.Unlock()
   111  	l.l.Error(msg, ctx...)
   112  	l.flush()
   113  }
   114  
   115  func (l *logger) Crit(msg string, ctx ...interface{}) {
   116  	l.t.Helper()
   117  	l.mu.Lock()
   118  	defer l.mu.Unlock()
   119  	l.l.Crit(msg, ctx...)
   120  	l.flush()
   121  }
   122  
   123  func (l *logger) New(ctx ...interface{}) log.Logger {
   124  	return &logger{l.t, l.l.New(ctx...), l.mu, l.h}
   125  }
   126  
   127  func (l *logger) GetHandler() log.Handler {
   128  	return l.l.GetHandler()
   129  }
   130  
   131  func (l *logger) SetHandler(h log.Handler) {
   132  	l.l.SetHandler(h)
   133  }
   134  
   135  // flush writes all buffered messages and clears the buffer.
   136  func (l *logger) flush() {
   137  	l.t.Helper()
   138  	for _, r := range l.h.buf {
   139  		l.t.Logf("%s", l.h.fmt.Format(r))
   140  	}
   141  	l.h.buf = nil
   142  }