Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: downgrade spew log to debug and limit its size #905

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions wallet/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ func pickNoun(n int, singular, plural string) string {
}
return plural
}

// LogClosure is a closure that can be printed with %v 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)
}
20 changes: 14 additions & 6 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3819,15 +3819,23 @@ func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
log.Warnf("Unable to remove invalid transaction %v: %v",
tx.TxHash(), dbErr)
} else {
// The spew output is pretty nice to directly see how many
// inputs/outputs of what type there are.
log.Infof("Removed invalid transaction: %v", spew.Sdump(tx))
log.Infof("Removed invalid transaction: %v", tx.TxHash())

// The serialized transaction is for logging only, don't fail on
// the error.
// The serialized transaction is for logging only, don't fail
// on the error.
var txRaw bytes.Buffer
_ = tx.Serialize(&txRaw)
log.Infof("Removed invalid transaction: %x", txRaw.Bytes())

// Optionally log the tx in debug when the size is manageable.
if txRaw.Len() < 1_000_000 {
log.Debugf("Removed invalid transaction: %v \n hex=%x",
newLogClosure(func() string {
return spew.Sdump(tx)
}), txRaw.Bytes())
} else {
log.Debug("Removed invalid transaction due to size " +
"too large")
}
}

return nil, returnErr
Expand Down
Loading