github.com/mavryk-network/mvgo@v1.19.9/rpc/log.go (about)

     1  // Copyright (c) 2020-2021 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  //nolint:unused,deadcode
     5  package rpc
     6  
     7  import "github.com/echa/log"
     8  
     9  // log is a logger that is initialized with no output filters.  This
    10  // means the package will not perform any logging by default until the caller
    11  // requests it.
    12  var logger log.Logger = log.Log
    13  
    14  // The default amount of logging is none.
    15  func init() {
    16  	DisableLog()
    17  }
    18  
    19  // DisableLog disables all library log output.  Logging output is disabled
    20  // by default until either UseLogger or SetLogWriter are called.
    21  func DisableLog() {
    22  	logger = log.Disabled
    23  }
    24  
    25  // UseLogger uses a specified Logger to output package logging info.
    26  // This should be used in preference to SetLogWriter if the caller is also
    27  // using logpkg.
    28  func UseLogger(l log.Logger) {
    29  	logger = l
    30  }
    31  
    32  // LogClosure is a closure that can be printed with %v to be used to
    33  // generate expensive-to-create data for a detailed log level and avoid doing
    34  // the work if the data isn't printed.
    35  type logClosure func() string
    36  
    37  // String invokes the log closure and returns the results string.
    38  func (c logClosure) String() string {
    39  	return c()
    40  }
    41  
    42  // newLogClosure returns a new closure over the passed function which allows
    43  // it to be used as a parameter in a logging function that is only invoked when
    44  // the logging level is such that the message will actually be logged.
    45  func newLogClosure(c func() string) logClosure {
    46  	return logClosure(c)
    47  }
    48  
    49  func (c *Client) logDebug(fn func()) {
    50  	if c.Log.Level() <= log.LevelDebug {
    51  		fn()
    52  	}
    53  }
    54  
    55  func (c *Client) logDebugOnly(fn func()) {
    56  	if c.Log.Level() == log.LevelDebug {
    57  		fn()
    58  	}
    59  }
    60  
    61  func (c *Client) logTrace(fn func()) {
    62  	if c.Log.Level() <= log.LevelTrace {
    63  		fn()
    64  	}
    65  }
    66  
    67  func (c *Client) logTraceOnly(fn func()) {
    68  	if c.Log.Level() == log.LevelTrace {
    69  		fn()
    70  	}
    71  }