[NOD-590] Export newLogClosure (#557)

This commit is contained in:
Ori Newman 2019-12-26 18:26:22 +02:00 committed by Svarog
parent fbaf360a42
commit 4a88eea57e
6 changed files with 25 additions and 52 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)

View File

@ -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)
}

View File

@ -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.

View File

@ -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)
}