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

-16
View File
@@ -27,19 +27,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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)
}
+3 -3
View File
@@ -10,6 +10,7 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnutils"
) )
// Swapper is an interface that allows the chanbackup.SubSwapper to update the // Swapper is an interface that allows the chanbackup.SubSwapper to update the
@@ -278,9 +279,8 @@ func (s *SubSwapper) backupUpdater() {
) )
for i, closedChan := range chanUpdate.ClosedChans { for i, closedChan := range chanUpdate.ClosedChans {
log.Debugf("Removing channel %v from backup "+ log.Debugf("Removing channel %v from backup "+
"state", newLogClosure(func() string { "state", lnutils.NewLogClosure(
return chanUpdate.ClosedChans[i].String() chanUpdate.ClosedChans[i].String))
}))
delete(s.backupState, closedChan) delete(s.backupState, closedChan)
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnutils"
) )
// ChannelRestorer is an interface that allows the Recover method to map the // ChannelRestorer is an interface that allows the Recover method to map the
@@ -63,7 +64,7 @@ func Recover(backups []Single, restorer ChannelRestorer,
log.Infof("Attempting to connect to node=%x (addrs=%v) to "+ log.Infof("Attempting to connect to node=%x (addrs=%v) to "+
"restore ChannelPoint(%v)", "restore ChannelPoint(%v)",
backup.RemoteNodePub.SerializeCompressed(), backup.RemoteNodePub.SerializeCompressed(),
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(backups[i].Addresses) return spew.Sdump(backups[i].Addresses)
}), backup.FundingOutpoint) }), backup.FundingOutpoint)
+8 -6
View File
@@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/labels" "github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
) )
@@ -732,9 +733,10 @@ justiceTxBroadcast:
} }
finalTx := justiceTxs.spendAll finalTx := justiceTxs.spendAll
brarLog.Debugf("Broadcasting justice tx: %v", newLogClosure(func() string { brarLog.Debugf("Broadcasting justice tx: %v", lnutils.NewLogClosure(
return spew.Sdump(finalTx) func() string {
})) return spew.Sdump(finalTx)
}))
// We'll now attempt to broadcast the transaction which finalized the // We'll now attempt to broadcast the transaction which finalized the
// channel's retribution against the cheating counter party. // channel's retribution against the cheating counter party.
@@ -857,7 +859,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+ brarLog.Debugf("Broadcasting justice tx "+
"spending commitment outs: %v", "spending commitment outs: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(tx) return spew.Sdump(tx)
})) }))
@@ -874,7 +876,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+ brarLog.Debugf("Broadcasting justice tx "+
"spending HTLC outs: %v", "spending HTLC outs: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(tx) return spew.Sdump(tx)
})) }))
@@ -891,7 +893,7 @@ Loop:
brarLog.Debugf("Broadcasting justice tx "+ brarLog.Debugf("Broadcasting justice tx "+
"spending second-level HTLC output: %v", "spending second-level HTLC output: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(tx) return spew.Sdump(tx)
})) }))
+2 -1
View File
@@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
@@ -1254,7 +1255,7 @@ func (c *chainWatcher) dispatchContractBreach(spendEvent *chainntnfs.SpendDetail
spendHeight := uint32(spendEvent.SpendingHeight) spendHeight := uint32(spendEvent.SpendingHeight)
log.Debugf("Punishment breach retribution created: %v", log.Debugf("Punishment breach retribution created: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
retribution.KeyRing.LocalHtlcKey = nil retribution.KeyRing.LocalHtlcKey = nil
retribution.KeyRing.RemoteHtlcKey = nil retribution.KeyRing.RemoteHtlcKey = nil
retribution.KeyRing.ToLocalKey = nil retribution.KeyRing.ToLocalKey = nil
+6 -5
View File
@@ -24,6 +24,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/labels" "github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/sweep" "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", 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) return spew.Sdump(c.activeHTLCs)
}), state.currentState, }), state.currentState,
) )
@@ -959,7 +960,7 @@ func (c *ChannelArbitrator) stateStep(
// commitment transaction has already been broadcast. // commitment transaction has already been broadcast.
log.Tracef("ChannelArbitrator(%v): logging chain_actions=%v", log.Tracef("ChannelArbitrator(%v): logging chain_actions=%v",
c.cfg.ChanPoint, c.cfg.ChanPoint,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(chainActions) return spew.Sdump(chainActions)
})) }))
@@ -1097,7 +1098,7 @@ func (c *ChannelArbitrator) stateStep(
log.Infof("Broadcasting force close transaction %v, "+ log.Infof("Broadcasting force close transaction %v, "+
"ChannelPoint(%v): %v", closeTx.TxHash(), "ChannelPoint(%v): %v", closeTx.TxHash(),
c.cfg.ChanPoint, c.cfg.ChanPoint,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(closeTx) return spew.Sdump(closeTx)
})) }))
@@ -1224,7 +1225,7 @@ func (c *ChannelArbitrator) stateStep(
if len(pktsToSend) != 0 { if len(pktsToSend) != 0 {
log.Debugf("ChannelArbitrator(%v): sending "+ log.Debugf("ChannelArbitrator(%v): sending "+
"resolution message=%v", c.cfg.ChanPoint, "resolution message=%v", c.cfg.ChanPoint,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(pktsToSend) return spew.Sdump(pktsToSend)
})) }))
@@ -2742,7 +2743,7 @@ func (c *ChannelArbitrator) notifyContractUpdate(upd *ContractUpdate) {
log.Tracef("ChannelArbitrator(%v): fresh set of htlcs=%v", log.Tracef("ChannelArbitrator(%v): fresh set of htlcs=%v",
c.cfg.ChanPoint, c.cfg.ChanPoint,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(upd) return spew.Sdump(upd)
}), }),
) )
-16
View File
@@ -51,19 +51,3 @@ func UseBreachLogger(logger btclog.Logger) {
func UseNurseryLogger(logger btclog.Logger) { func UseNurseryLogger(logger btclog.Logger) {
utxnLog = 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/fn"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/labels" "github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/sweep" "github.com/lightningnetwork/lnd/sweep"
) )
@@ -947,7 +948,7 @@ func (u *UtxoNursery) waitForSweepConf(classHeight uint32,
func (u *UtxoNursery) sweepCribOutput(classHeight uint32, baby *babyOutput) error { func (u *UtxoNursery) sweepCribOutput(classHeight uint32, baby *babyOutput) error {
utxnLog.Infof("Publishing CLTV-delayed HTLC output using timeout tx "+ utxnLog.Infof("Publishing CLTV-delayed HTLC output using timeout tx "+
"(txid=%v): %v", baby.timeoutTx.TxHash(), "(txid=%v): %v", baby.timeoutTx.TxHash(),
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(baby.timeoutTx) return spew.Sdump(baby.timeoutTx)
}), }),
) )
+2 -1
View File
@@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/btcutil/bech32" "github.com/btcsuite/btcd/btcutil/bech32"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor" "github.com/lightningnetwork/lnd/tor"
"github.com/miekg/dns" "github.com/miekg/dns"
@@ -431,7 +432,7 @@ search:
} }
log.Tracef("Retrieved SRV records from dns seed: %v", log.Tracef("Retrieved SRV records from dns seed: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(addrs) return spew.Sdump(addrs)
}), }),
) )
-16
View File
@@ -27,19 +27,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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)
}
+3 -1
View File
@@ -1432,7 +1432,9 @@ func (b *Builder) processUpdate(msg interface{},
} }
log.Tracef("New channel update applied: %v", log.Tracef("New channel update applied: %v",
newLogClosure(func() string { return spew.Sdump(msg) })) lnutils.NewLogClosure(func() string {
return spew.Sdump(msg)
}))
b.stats.incNumChannelUpdates() b.stats.incNumChannelUpdates()
default: default:
-16
View File
@@ -29,19 +29,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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)
}
+13 -9
View File
@@ -10,6 +10,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
@@ -800,9 +801,10 @@ func (cm *circuitMap) CommitCircuits(circuits ...*PaymentCircuit) (
inKeys = append(inKeys, circuit.Incoming) inKeys = append(inKeys, circuit.Incoming)
} }
log.Tracef("Committing fresh circuits: %v", newLogClosure(func() string { log.Tracef("Committing fresh circuits: %v", lnutils.NewLogClosure(
return spew.Sdump(inKeys) func() string {
})) return spew.Sdump(inKeys)
}))
actions := &CircuitFwdActions{} actions := &CircuitFwdActions{}
@@ -948,9 +950,10 @@ func (cm *circuitMap) OpenCircuits(keystones ...Keystone) error {
return nil return nil
} }
log.Tracef("Opening finalized circuits: %v", newLogClosure(func() string { log.Tracef("Opening finalized circuits: %v", lnutils.NewLogClosure(
return spew.Sdump(keystones) func() string {
})) return spew.Sdump(keystones)
}))
// Check that all keystones correspond to committed-but-unopened // Check that all keystones correspond to committed-but-unopened
// circuits. // circuits.
@@ -1078,9 +1081,10 @@ func (cm *circuitMap) CloseCircuit(outKey CircuitKey) (*PaymentCircuit, error) {
// circuit was already cleaned up at a different point in time. // circuit was already cleaned up at a different point in time.
func (cm *circuitMap) DeleteCircuits(inKeys ...CircuitKey) error { func (cm *circuitMap) DeleteCircuits(inKeys ...CircuitKey) error {
log.Tracef("Deleting resolved circuits: %v", newLogClosure(func() string { log.Tracef("Deleting resolved circuits: %v", lnutils.NewLogClosure(
return spew.Sdump(inKeys) func() string {
})) return spew.Sdump(inKeys)
}))
var ( var (
closingCircuits = make(map[CircuitKey]struct{}) closingCircuits = make(map[CircuitKey]struct{})
+3 -2
View File
@@ -25,6 +25,7 @@ import (
"github.com/lightningnetwork/lnd/invoices" "github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -2536,10 +2537,10 @@ func (l *channelLink) updateCommitTx() error {
l.log.Tracef("revocation window exhausted, unable to send: "+ l.log.Tracef("revocation window exhausted, unable to send: "+
"%v, pend_updates=%v, dangling_closes%v", "%v, pend_updates=%v, dangling_closes%v",
l.channel.PendingLocalUpdateCount(), l.channel.PendingLocalUpdateCount(),
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(l.openedCircuits) return spew.Sdump(l.openedCircuits)
}), }),
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(l.closedCircuits) return spew.Sdump(l.closedCircuits)
}), }),
) )
-16
View File
@@ -31,19 +31,3 @@ func UseLogger(logger btclog.Logger) {
log = logger log = logger
hop.UseLogger(logger) hop.UseLogger(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)
}
+10 -6
View File
@@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -613,9 +614,10 @@ func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, attemptID uint64,
func (s *Switch) UpdateForwardingPolicies( func (s *Switch) UpdateForwardingPolicies(
chanPolicies map[wire.OutPoint]models.ForwardingPolicy) { chanPolicies map[wire.OutPoint]models.ForwardingPolicy) {
log.Tracef("Updating link policies: %v", newLogClosure(func() string { log.Tracef("Updating link policies: %v", lnutils.NewLogClosure(
return spew.Sdump(chanPolicies) func() string {
})) return spew.Sdump(chanPolicies)
}))
s.indexMtx.RLock() s.indexMtx.RLock()
@@ -1213,7 +1215,8 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
) )
log.Warnf("unable to find err source for "+ log.Warnf("unable to find err source for "+
"outgoing_link=%v, errors=%v", "outgoing_link=%v, errors=%v",
packet.outgoingChanID, newLogClosure(func() string { packet.outgoingChanID,
lnutils.NewLogClosure(func() string {
return spew.Sdump(linkErrs) return spew.Sdump(linkErrs)
})) }))
} }
@@ -1994,8 +1997,9 @@ out:
continue continue
} }
log.Tracef("Acked %d settle fails: %v", len(s.pendingSettleFails), log.Tracef("Acked %d settle fails: %v",
newLogClosure(func() string { len(s.pendingSettleFails),
lnutils.NewLogClosure(func() string {
return spew.Sdump(s.pendingSettleFails) return spew.Sdump(s.pendingSettleFails)
})) }))
-16
View File
@@ -27,19 +27,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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 // nolint:unused
// 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 { // nolint:unused
return logClosure(c)
}
+2 -1
View File
@@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/invoices" "github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/netann" "github.com/lightningnetwork/lnd/netann"
"github.com/lightningnetwork/lnd/routing" "github.com/lightningnetwork/lnd/routing"
@@ -473,7 +474,7 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
} }
log.Tracef("[addinvoice] adding new invoice %v", log.Tracef("[addinvoice] adding new invoice %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(newInvoice) return spew.Sdump(newInvoice)
}), }),
) )
-16
View File
@@ -27,19 +27,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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)
}
+17
View File
@@ -0,0 +1,17 @@
package lnutils
// 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
@@ -15,6 +15,7 @@ import (
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/rpcclient"
"github.com/lightningnetwork/lnd/lnutils"
) )
const ( const (
@@ -1004,7 +1005,7 @@ func (w *WebAPIEstimator) updateFeeEstimates() {
return return
} }
log.Debugf("Received response from source: %s", newLogClosure( log.Debugf("Received response from source: %s", lnutils.NewLogClosure(
func() string { func() string {
resp, _ := json.Marshal(resp) resp, _ := json.Marshal(resp)
return string(resp) return string(resp)
-16
View File
@@ -27,19 +27,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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
@@ -15,6 +15,7 @@ import (
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/labels" "github.com/lightningnetwork/lnd/labels"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -869,7 +870,7 @@ func (c *ChanCloser) ReceiveClosingSigned( //nolint:funlen
// With the closing transaction crafted, we'll now broadcast it // With the closing transaction crafted, we'll now broadcast it
// to the network. // to the network.
chancloserLog.Infof("Broadcasting cooperative close tx: %v", chancloserLog.Infof("Broadcasting cooperative close tx: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(closeTx) return spew.Sdump(closeTx)
}), }),
) )
-16
View File
@@ -23,19 +23,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
chancloserLog = logger chancloserLog = logger
} }
// logClosure is used to provide a closure over expensive logging operations
// so they aren't 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)
}
+8 -7
View File
@@ -29,6 +29,7 @@ import (
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain" "github.com/lightningnetwork/lnd/shachain"
@@ -1869,7 +1870,7 @@ func (lc *LightningChannel) restoreCommitState(
lc.localCommitChain.addCommitment(localCommit) lc.localCommitChain.addCommitment(localCommit)
lc.log.Tracef("starting local commitment: %v", lc.log.Tracef("starting local commitment: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(lc.localCommitChain.tail()) return spew.Sdump(lc.localCommitChain.tail())
}), }),
) )
@@ -1885,7 +1886,7 @@ func (lc *LightningChannel) restoreCommitState(
lc.remoteCommitChain.addCommitment(remoteCommit) lc.remoteCommitChain.addCommitment(remoteCommit)
lc.log.Tracef("starting remote commitment: %v", lc.log.Tracef("starting remote commitment: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(lc.remoteCommitChain.tail()) return spew.Sdump(lc.remoteCommitChain.tail())
}), }),
) )
@@ -1920,7 +1921,7 @@ func (lc *LightningChannel) restoreCommitState(
lc.remoteCommitChain.addCommitment(pendingRemoteCommit) lc.remoteCommitChain.addCommitment(pendingRemoteCommit)
lc.log.Debugf("pending remote commitment: %v", lc.log.Debugf("pending remote commitment: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(lc.remoteCommitChain.tip()) return spew.Sdump(lc.remoteCommitChain.tip())
}), }),
) )
@@ -3245,9 +3246,9 @@ func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor,
return nil, fmt.Errorf("unable to find parent entry "+ return nil, fmt.Errorf("unable to find parent entry "+
"%d in %v update log: %v\nUpdatelog: %v", "%d in %v update log: %v\nUpdatelog: %v",
entry.ParentIndex, logName, entry.ParentIndex, logName,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(entry) return spew.Sdump(entry)
}), newLogClosure(func() string { }), lnutils.NewLogClosure(func() string {
return spew.Sdump(updateLog) return spew.Sdump(updateLog)
}), }),
) )
@@ -4249,7 +4250,7 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
"their_balance=%v, commit_tx: %v", "their_balance=%v, commit_tx: %v",
newCommitView.ourBalance, newCommitView.ourBalance,
newCommitView.theirBalance, newCommitView.theirBalance,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(newCommitView.txn) return spew.Sdump(newCommitView.txn)
}), }),
) )
@@ -5209,7 +5210,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
lc.log.Tracef("local chain: our_balance=%v, "+ lc.log.Tracef("local chain: our_balance=%v, "+
"their_balance=%v, commit_tx: %v", "their_balance=%v, commit_tx: %v",
localCommitmentView.ourBalance, localCommitmentView.theirBalance, localCommitmentView.ourBalance, localCommitmentView.theirBalance,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(localCommitmentView.txn) return spew.Sdump(localCommitmentView.txn)
}), }),
) )
-16
View File
@@ -36,19 +36,3 @@ func UseLogger(logger btclog.Logger) {
chain.UseLogger(logger) chain.UseLogger(logger)
chainfee.UseLogger(logger) chainfee.UseLogger(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)
}
-16
View File
@@ -208,19 +208,3 @@ func SetSubLogger(root *build.RotatingLogWriter, subsystem string,
useLogger(logger) useLogger(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 -2
View File
@@ -2156,7 +2156,7 @@ func (p *Brontide) logWireMessage(msg lnwire.Message, read bool) {
summaryPrefix = "Sending" summaryPrefix = "Sending"
} }
p.log.Debugf("%v", newLogClosure(func() string { p.log.Debugf("%v", lnutils.NewLogClosure(func() string {
// Debug summary of message. // Debug summary of message.
summary := messageSummary(msg) summary := messageSummary(msg)
if len(summary) > 0 { if len(summary) > 0 {
@@ -2184,7 +2184,7 @@ func (p *Brontide) logWireMessage(msg lnwire.Message, read bool) {
prefix = "writeMessage to peer" prefix = "writeMessage to peer"
} }
p.log.Tracef(prefix+": %v", newLogClosure(func() string { p.log.Tracef(prefix+": %v", lnutils.NewLogClosure(func() string {
return spew.Sdump(msg) return spew.Sdump(msg)
})) }))
} }
-16
View File
@@ -22,19 +22,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
peerLog = logger peerLog = logger
} }
// logClosure is used to provide a closure over expensive logging operations
// so they aren't 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)
}
-16
View File
@@ -31,19 +31,3 @@ func UseLogger(logger btclog.Logger) {
log = logger log = logger
chainview.UseLogger(logger) chainview.UseLogger(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
@@ -13,6 +13,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/feature" "github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record" "github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/routing/route" "github.com/lightningnetwork/lnd/routing/route"
@@ -728,7 +729,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
edge.capacity, edge.capacity,
) )
log.Trace(newLogClosure(func() string { log.Trace(lnutils.NewLogClosure(func() string {
return fmt.Sprintf("path finding probability: fromnode=%v,"+ return fmt.Sprintf("path finding probability: fromnode=%v,"+
" tonode=%v, amt=%v, cap=%v, probability=%v", " tonode=%v, amt=%v, cap=%v, probability=%v",
fromVertex, toNodeDist.node, amountToSend, fromVertex, toNodeDist.node, amountToSend,
+2 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/graph" "github.com/lightningnetwork/lnd/graph"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route" "github.com/lightningnetwork/lnd/routing/route"
) )
@@ -432,7 +433,7 @@ func (p *paymentSession) UpdateAdditionalEdge(msg *lnwire.ChannelUpdate,
policy.FeeProportionalMillionths = lnwire.MilliSatoshi(msg.FeeRate) policy.FeeProportionalMillionths = lnwire.MilliSatoshi(msg.FeeRate)
log.Debugf("New private channel update applied: %v", log.Debugf("New private channel update applied: %v",
newLogClosure(func() string { return spew.Sdump(msg) })) lnutils.NewLogClosure(func() string { return spew.Sdump(msg) }))
return true return true
} }
+7 -6
View File
@@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record" "github.com/lightningnetwork/lnd/record"
@@ -669,7 +670,7 @@ func (r *ChannelRouter) FindRoute(req *RouteRequest) (*route.Route, float64,
} }
go log.Tracef("Obtained path to send %v to %x: %v", go log.Tracef("Obtained path to send %v to %x: %v",
req.Amount, req.Target, newLogClosure(func() string { req.Amount, req.Target, lnutils.NewLogClosure(func() string {
return spew.Sdump(route) return spew.Sdump(route)
}), }),
) )
@@ -704,7 +705,7 @@ func generateSphinxPacket(rt *route.Route, paymentHash []byte,
} }
log.Tracef("Constructed per-hop payloads for payment_hash=%x: %v", log.Tracef("Constructed per-hop payloads for payment_hash=%x: %v",
paymentHash[:], newLogClosure(func() string { paymentHash, lnutils.NewLogClosure(func() string {
path := make( path := make(
[]sphinx.OnionHop, sphinxPath.TrueRouteLength(), []sphinx.OnionHop, sphinxPath.TrueRouteLength(),
) )
@@ -734,7 +735,7 @@ func generateSphinxPacket(rt *route.Route, paymentHash []byte,
} }
log.Tracef("Generated sphinx packet: %v", log.Tracef("Generated sphinx packet: %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
// We make a copy of the ephemeral key and unset the // We make a copy of the ephemeral key and unset the
// internal curve here in order to keep the logs from // internal curve here in order to keep the logs from
// getting noisy. // getting noisy.
@@ -945,8 +946,8 @@ func (r *ChannelRouter) SendPaymentAsync(ctx context.Context,
// spewPayment returns a log closures that provides a spewed string // spewPayment returns a log closures that provides a spewed string
// representation of the passed payment. // representation of the passed payment.
func spewPayment(payment *LightningPayment) logClosure { func spewPayment(payment *LightningPayment) lnutils.LogClosure {
return newLogClosure(func() string { return lnutils.NewLogClosure(func() string {
// Make a copy of the payment with a nilled Curve // Make a copy of the payment with a nilled Curve
// before spewing. // before spewing.
var routeHints [][]zpay32.HopHint var routeHints [][]zpay32.HopHint
@@ -1091,7 +1092,7 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
} }
log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v", log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v",
htlcHash, newLogClosure(func() string { htlcHash, lnutils.NewLogClosure(func() string {
return spew.Sdump(rt) return spew.Sdump(rt)
}), }),
) )
+2 -1
View File
@@ -63,6 +63,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc" "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet" "github.com/lightningnetwork/lnd/lnwallet/btcwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -5845,7 +5846,7 @@ func (r *rpcServer) LookupInvoice(ctx context.Context,
} }
rpcsLog.Tracef("[lookupinvoice] located invoice %v", rpcsLog.Tracef("[lookupinvoice] located invoice %v",
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(invoice) return spew.Sdump(invoice)
})) }))
+1 -1
View File
@@ -313,7 +313,7 @@ func (t *TxPublisher) isNeutrinoBackend() bool {
// //
// NOTE: part of the Bumper interface. // NOTE: part of the Bumper interface.
func (t *TxPublisher) Broadcast(req *BumpRequest) (<-chan *BumpResult, error) { func (t *TxPublisher) Broadcast(req *BumpRequest) (<-chan *BumpResult, error) {
log.Tracef("Received broadcast request: %s", newLogClosure( log.Tracef("Received broadcast request: %s", lnutils.NewLogClosure(
func() string { func() string {
return spew.Sdump(req) return spew.Sdump(req)
})()) })())
-16
View File
@@ -27,19 +27,3 @@ func DisableLog() {
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = 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
@@ -13,6 +13,7 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chainfee"
) )
@@ -1376,7 +1377,7 @@ func (s *UtxoSweeper) handleInputSpent(spend *chainntnfs.SpendDetail) {
log.Debugf("Detected third party spend related to in flight "+ log.Debugf("Detected third party spend related to in flight "+
"inputs (is_ours=%v): %v", isOurTx, "inputs (is_ours=%v): %v", isOurTx,
newLogClosure(func() string { lnutils.NewLogClosure(func() string {
return spew.Sdump(spend.SpendingTx) return spew.Sdump(spend.SpendingTx)
}), }),
) )
-16
View File
@@ -43,19 +43,3 @@ func UseLogger(logger btclog.Logger) {
migration7.UseLogger(logger) migration7.UseLogger(logger)
migration8.UseLogger(logger) migration8.UseLogger(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 // nolint:unused
// 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 { // nolint:unused
return logClosure(c)
}