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

     1  package peer
     2  
     3  import (
     4  	"github.com/decred/dcrlnd/build"
     5  	"github.com/decred/slog"
     6  )
     7  
     8  // peerLog is a logger that is initialized with the slog.Disabled logger.
     9  var peerLog slog.Logger
    10  
    11  // pingLog is a logger that is used to log sent and received ping/pong
    12  // messages.
    13  var pingLog slog.Logger
    14  
    15  // The default amount of logging is none.
    16  func init() {
    17  	UseLogger(build.NewSubLogger("PEER", nil))
    18  	UseLogger(build.NewSubLogger("PING", nil))
    19  }
    20  
    21  // DisableLog disables all logging output.
    22  func DisableLog() {
    23  	UseLogger(slog.Disabled)
    24  	UsePingLogger(slog.Disabled)
    25  }
    26  
    27  // UseLogger uses a specified Logger to output package logging info.
    28  func UseLogger(logger slog.Logger) {
    29  	peerLog = logger
    30  }
    31  
    32  func UsePingLogger(logger slog.Logger) {
    33  	pingLog = logger
    34  }
    35  
    36  // logClosure is used to provide a closure over expensive logging operations
    37  // so they aren't performed when the logging level doesn't warrant it.
    38  type logClosure func() string
    39  
    40  // String invokes the underlying function and returns the result.
    41  func (c logClosure) String() string {
    42  	return c()
    43  }
    44  
    45  // newLogClosure returns a new closure over a function that returns a string
    46  // which itself provides a Stringer interface so that it can be used with the
    47  // logging system.
    48  func newLogClosure(c func() string) logClosure {
    49  	return logClosure(c)
    50  }