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

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