github.com/decred/dcrlnd@v0.7.6/routing/log.go (about)

     1  package routing
     2  
     3  import (
     4  	"github.com/decred/dcrlnd/build"
     5  	"github.com/decred/dcrlnd/routing/chainview"
     6  	"github.com/decred/slog"
     7  )
     8  
     9  // log is a logger that is initialized with no output filters.  This means the
    10  // package will not perform any logging by default until the caller requests
    11  // it.
    12  var log slog.Logger
    13  
    14  const Subsystem = "CRTR"
    15  
    16  // The default amount of logging is none.
    17  func init() {
    18  	UseLogger(build.NewSubLogger(Subsystem, nil))
    19  }
    20  
    21  // DisableLog disables all library log output.  Logging output is disabled by
    22  // by default until UseLogger is called.
    23  func DisableLog() {
    24  	UseLogger(slog.Disabled)
    25  }
    26  
    27  // UseLogger uses a specified Logger to output package logging info.  This
    28  // should be used in preference to SetLogWriter if the caller is also using
    29  // slog.
    30  func UseLogger(logger slog.Logger) {
    31  	log = logger
    32  	chainview.UseLogger(logger)
    33  }
    34  
    35  // logClosure is used to provide a closure over expensive logging operations so
    36  // don't have to be performed when the logging level doesn't warrant it.
    37  type logClosure func() string
    38  
    39  // String invokes the underlying function and returns the result.
    40  func (c logClosure) String() string {
    41  	return c()
    42  }
    43  
    44  // newLogClosure returns a new closure over a function that returns a string
    45  // which itself provides a Stringer interface so that it can be used with the
    46  // logging system.
    47  func newLogClosure(c func() string) logClosure {
    48  	return logClosure(c)
    49  }