diff --git a/logger/logger.go b/logger/logger.go index 0f5350b09..8c3038cc0 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -254,3 +254,18 @@ func validLogLevel(logLevel string) bool { } return false } + +// LogClosure is a closure that can be printed with %s to be used to +// generate expensive-to-create data for a detailed log level and avoid doing +// the work if the data isn't printed. +type LogClosure func() string + +func (c LogClosure) String() string { + return c() +} + +// NewLogClosure casts a function to a LogClosure. +// See LogClosure for details. +func NewLogClosure(c func() string) LogClosure { + return c +} diff --git a/peer/log.go b/peer/log.go index 500ce6a07..23cea4fdd 100644 --- a/peer/log.go +++ b/peer/log.go @@ -24,19 +24,6 @@ const ( var log, _ = logger.Get(logger.SubsystemTags.PEER) var spawn = panics.GoroutineWrapperFunc(log) -// LogClosure is a closure that can be printed with %s to be used to -// generate expensive-to-create data for a detailed log level and avoid doing -// the work if the data isn't printed. -type logClosure func() string - -func (c logClosure) String() string { - return c() -} - -func newLogClosure(c func() string) logClosure { - return logClosure(c) -} - // formatLockTime returns a transaction lock time as a human-readable string. func formatLockTime(lockTime uint64) string { // The lock time field of a transaction is either a block height at diff --git a/peer/peer.go b/peer/peer.go index 29e554e04..8a6e30160 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -1029,7 +1029,7 @@ func (p *Peer) readMessage() (wire.Message, []byte, error) { // Use closures to log expensive operations so they are only run when // the logging level requires it. - log.Debugf("%s", newLogClosure(func() string { + log.Debugf("%s", logger.NewLogClosure(func() string { // Debug summary of message. summary := messageSummary(msg) if len(summary) > 0 { @@ -1038,10 +1038,10 @@ func (p *Peer) readMessage() (wire.Message, []byte, error) { return fmt.Sprintf("Received %s%s from %s", msg.Command(), summary, p) })) - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { return spew.Sdump(msg) })) - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { return spew.Sdump(buf) })) @@ -1057,7 +1057,7 @@ func (p *Peer) writeMessage(msg wire.Message) error { // Use closures to log expensive operations so they are only run when // the logging level requires it. - log.Debugf("%s", newLogClosure(func() string { + log.Debugf("%s", logger.NewLogClosure(func() string { // Debug summary of message. summary := messageSummary(msg) if len(summary) > 0 { @@ -1066,10 +1066,10 @@ func (p *Peer) writeMessage(msg wire.Message) error { return fmt.Sprintf("Sending %s%s to %s", msg.Command(), summary, p) })) - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { return spew.Sdump(msg) })) - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { var buf bytes.Buffer _, err := wire.WriteMessageN(&buf, msg, p.ProtocolVersion(), p.cfg.DAGParams.Net) diff --git a/rpcclient/log.go b/rpcclient/log.go index 1b96a0646..f7dcdd4b9 100644 --- a/rpcclient/log.go +++ b/rpcclient/log.go @@ -32,20 +32,3 @@ func UseLogger(logger logs.Logger) { log = logger spawn = panics.GoroutineWrapperFunc(log) } - -// LogClosure is a closure that can be printed with %s to be used to -// generate expensive-to-create data for a detailed log level and avoid doing -// the work if the data isn't printed. -type logClosure func() string - -// String invokes the log closure and returns the results string. -func (c logClosure) String() string { - return c() -} - -// newLogClosure returns a new closure over the passed function which allows -// it to be used as a parameter in a logging function that is only invoked when -// the logging level is such that the message will actually be logged. -func newLogClosure(c func() string) logClosure { - return logClosure(c) -} diff --git a/txscript/engine.go b/txscript/engine.go index f9ef45e1c..65d4e9a0a 100644 --- a/txscript/engine.go +++ b/txscript/engine.go @@ -6,6 +6,7 @@ package txscript import ( "fmt" + "github.com/kaspanet/kaspad/logger" "math/big" "github.com/kaspanet/kaspad/ecc" @@ -221,7 +222,7 @@ func (vm *Engine) CheckErrorCondition(finalScript bool) error { } if !v { // Log interesting data. - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { dis0, _ := vm.DisasmScript(0) dis1, _ := vm.DisasmScript(1) return fmt.Sprintf("scripts failed: script0: %s\n"+ @@ -320,7 +321,7 @@ func (vm *Engine) Step() (done bool, err error) { func (vm *Engine) Execute() (err error) { done := false for !done { - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { dis, err := vm.DisasmPC() if err != nil { return fmt.Sprintf("stepping (%s)", err) @@ -332,7 +333,7 @@ func (vm *Engine) Execute() (err error) { if err != nil { return err } - log.Tracef("%s", newLogClosure(func() string { + log.Tracef("%s", logger.NewLogClosure(func() string { var dstr, astr string // if we're tracing, dump the stacks. diff --git a/txscript/log.go b/txscript/log.go index b95819a10..4ddc666fc 100644 --- a/txscript/log.go +++ b/txscript/log.go @@ -9,16 +9,3 @@ import ( ) var log, _ = logger.Get(logger.SubsystemTags.SCRP) - -// LogClosure is a closure that can be printed with %s to be used to -// generate expensive-to-create data for a detailed log level and avoid doing -// the work if the data isn't printed. -type logClosure func() string - -func (c logClosure) String() string { - return c() -} - -func newLogClosure(c func() string) logClosure { - return logClosure(c) -}