github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/logger.go (about)

     1  // Copyright 2017 The celo Authors
     2  // This file is part of the celo library.
     3  //
     4  // The celo 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 celo 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 celo library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package istanbul
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/log"
    22  	"math/big"
    23  )
    24  
    25  type istLogger struct {
    26  	logger log.Logger
    27  	round  func() *big.Int
    28  }
    29  
    30  // NewIstLogger creates an Istanbul Logger with custom logic for exposing logs
    31  func NewIstLogger(fn func() *big.Int, ctx ...interface{}) log.Logger {
    32  	return &istLogger{logger: log.New(ctx...), round: fn}
    33  }
    34  
    35  func (l *istLogger) New(ctx ...interface{}) log.Logger {
    36  	childLogger := l.logger.New(ctx...)
    37  	return &istLogger{logger: childLogger, round: l.round}
    38  }
    39  
    40  func (l *istLogger) Trace(msg string, ctx ...interface{}) {
    41  	// If the current round > 1, then upgrade this message to Info
    42  	if l.round != nil && l.round() != nil && l.round().Cmp(common.Big1) > 0 {
    43  		l.Info(msg, ctx...)
    44  	} else {
    45  		l.logger.Trace(msg, ctx...)
    46  	}
    47  }
    48  
    49  func (l *istLogger) Debug(msg string, ctx ...interface{}) {
    50  	// If the current round > 1, then upgrade this message to Info
    51  	if l.round != nil && l.round() != nil && l.round().Cmp(common.Big1) > 0 {
    52  		l.Info(msg, ctx...)
    53  	} else {
    54  		l.logger.Debug(msg, ctx...)
    55  	}
    56  }
    57  
    58  func (l *istLogger) Info(msg string, ctx ...interface{}) {
    59  	l.logger.Info(msg, ctx...)
    60  }
    61  
    62  func (l *istLogger) Warn(msg string, ctx ...interface{}) {
    63  	l.logger.Warn(msg, ctx...)
    64  }
    65  
    66  func (l *istLogger) Error(msg string, ctx ...interface{}) {
    67  	l.logger.Error(msg, ctx...)
    68  }
    69  
    70  func (l *istLogger) Crit(msg string, ctx ...interface{}) {
    71  	l.logger.Crit(msg, ctx...)
    72  }
    73  
    74  func (l *istLogger) GetHandler() log.Handler {
    75  	return l.logger.GetHandler()
    76  }
    77  
    78  func (l *istLogger) SetHandler(h log.Handler) {
    79  	l.logger.SetHandler(h)
    80  }