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

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