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

     1  package htlcswitch
     2  
     3  import (
     4  	"github.com/decred/dcrlnd/build"
     5  	"github.com/decred/dcrlnd/htlcswitch/hop"
     6  	"github.com/decred/slog"
     7  )
     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 log slog.Logger
    13  
    14  // The default amount of logging is none.
    15  func init() {
    16  	logger := build.NewSubLogger("HSWC", nil)
    17  
    18  	UseLogger(logger)
    19  }
    20  
    21  // DisableLog disables all library log output.  Logging output is disabled
    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.
    28  // This should be used in preference to SetLogWriter if the caller is also
    29  // using slog.
    30  func UseLogger(logger slog.Logger) {
    31  	log = logger
    32  	hop.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  }