multi: add NewLogClosure in lnutils to avoid repetition

And replaces all usage of `logClosure` with `lnutils.LogClosure`.
This commit is contained in:
yyforyongyu committed 2024-07-25 21:25:23 +08:00
1 parent 7e60d41898
commit b6049ff94b
38 files changed
+103 -299

No files matched your search

+8 -6
View File
@@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
@@ -732,9 +733,10 @@ justiceTxBroadcast:
}
finalTx := justiceTxs.spendAll
brarLog.Debugf("Broadcasting justice tx: %v", newLogClosure(func() string {
return spew.Sdump(finalTx)
}))
brarLog.Debugf("Broadcasting justice tx: %v", lnutils.NewLogClosure(
func() string {
return spew.Sdump(finalTx)
}))
// We'll now attempt to broadcast the transaction which finalized the
// channel's retribution against the cheating counter party.
@@ -857,7 +859,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+
"spending commitment outs: %v",
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(tx)
}))
@@ -874,7 +876,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+
"spending HTLC outs: %v",
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(tx)
}))
@@ -891,7 +893,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+
"spending second-level HTLC output: %v",
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(tx)
}))
+2 -1
View File
@@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
)
@@ -1254,7 +1255,7 @@ func (c *chainWatcher) dispatchContractBreach(spendEvent *chainntnfs.SpendDetail
spendHeight := uint32(spendEvent.SpendingHeight)
log.Debugf("Punishment breach retribution created: %v",
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
retribution.KeyRing.LocalHtlcKey = nil
retribution.KeyRing.RemoteHtlcKey = nil
retribution.KeyRing.ToLocalKey = nil
+6 -5
View File
@@ -24,6 +24,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/sweep"
@@ -465,7 +466,7 @@ func (c *ChannelArbitrator) Start(state *chanArbStartState) error {
}
log.Debugf("Starting ChannelArbitrator(%v), htlc_set=%v, state=%v",
c.cfg.ChanPoint, newLogClosure(func() string {
c.cfg.ChanPoint, lnutils.NewLogClosure(func() string {
return spew.Sdump(c.activeHTLCs)
}), state.currentState,
)
@@ -959,7 +960,7 @@ func (c *ChannelArbitrator) stateStep(
// commitment transaction has already been broadcast.
log.Tracef("ChannelArbitrator(%v): logging chain_actions=%v",
c.cfg.ChanPoint,
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(chainActions)
}))
@@ -1097,7 +1098,7 @@ func (c *ChannelArbitrator) stateStep(
log.Infof("Broadcasting force close transaction %v, "+
"ChannelPoint(%v): %v", closeTx.TxHash(),
c.cfg.ChanPoint,
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(closeTx)
}))
@@ -1224,7 +1225,7 @@ func (c *ChannelArbitrator) stateStep(
if len(pktsToSend) != 0 {
log.Debugf("ChannelArbitrator(%v): sending "+
"resolution message=%v", c.cfg.ChanPoint,
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(pktsToSend)
}))
@@ -2742,7 +2743,7 @@ func (c *ChannelArbitrator) notifyContractUpdate(upd *ContractUpdate) {
log.Tracef("ChannelArbitrator(%v): fresh set of htlcs=%v",
c.cfg.ChanPoint,
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(upd)
}),
)
-16
View File
@@ -51,19 +51,3 @@ func UseBreachLogger(logger btclog.Logger) {
func UseNurseryLogger(logger btclog.Logger) {
utxnLog = logger
}
// logClosure is used to provide a closure over expensive logging operations so
// don't have to be performed when the logging level doesn't warrant it.
type logClosure func() string
// String invokes the underlying function and returns the result.
func (c logClosure) String() string {
return c()
}
// newLogClosure returns a new closure over a function that returns a string
// which itself provides a Stringer interface so that it can be used with the
// logging system.
func newLogClosure(c func() string) logClosure {
return logClosure(c)
}
+2 -1
View File
@@ -18,6 +18,7 @@ import (
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/sweep"
)
@@ -947,7 +948,7 @@ func (u *UtxoNursery) waitForSweepConf(classHeight uint32,
func (u *UtxoNursery) sweepCribOutput(classHeight uint32, baby *babyOutput) error {
utxnLog.Infof("Publishing CLTV-delayed HTLC output using timeout tx "+
"(txid=%v): %v", baby.timeoutTx.TxHash(),
newLogClosure(func() string {
lnutils.NewLogClosure(func() string {
return spew.Sdump(baby.timeoutTx)
}),
)