Skip to content

Commit b6049ff

Browse files
committed
multi: add NewLogClosure in lnutils to avoid repetition
And replaces all usage of `logClosure` with `lnutils.LogClosure`.
1 parent 7e60d41 commit b6049ff

File tree

38 files changed

+103
-299
lines changed

38 files changed

+103
-299
lines changed

chanbackup/log.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,3 @@ func DisableLog() {
2727
func UseLogger(logger btclog.Logger) {
2828
log = logger
2929
}
30-
31-
// logClosure is used to provide a closure over expensive logging operations so
32-
// don't have to be performed when the logging level doesn't warrant it.
33-
type logClosure func() string
34-
35-
// String invokes the underlying function and returns the result.
36-
func (c logClosure) String() string {
37-
return c()
38-
}
39-
40-
// newLogClosure returns a new closure over a function that returns a string
41-
// which itself provides a Stringer interface so that it can be used with the
42-
// logging system.
43-
func newLogClosure(c func() string) logClosure {
44-
return logClosure(c)
45-
}

chanbackup/pubsub.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/btcsuite/btcd/wire"
1111
"github.com/lightningnetwork/lnd/channeldb"
1212
"github.com/lightningnetwork/lnd/keychain"
13+
"github.com/lightningnetwork/lnd/lnutils"
1314
)
1415

1516
// Swapper is an interface that allows the chanbackup.SubSwapper to update the
@@ -278,9 +279,8 @@ func (s *SubSwapper) backupUpdater() {
278279
)
279280
for i, closedChan := range chanUpdate.ClosedChans {
280281
log.Debugf("Removing channel %v from backup "+
281-
"state", newLogClosure(func() string {
282-
return chanUpdate.ClosedChans[i].String()
283-
}))
282+
"state", lnutils.NewLogClosure(
283+
chanUpdate.ClosedChans[i].String))
284284

285285
delete(s.backupState, closedChan)
286286

chanbackup/recover.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/davecgh/go-spew/spew"
88
"github.com/lightningnetwork/lnd/channeldb"
99
"github.com/lightningnetwork/lnd/keychain"
10+
"github.com/lightningnetwork/lnd/lnutils"
1011
)
1112

1213
// ChannelRestorer is an interface that allows the Recover method to map the
@@ -63,7 +64,7 @@ func Recover(backups []Single, restorer ChannelRestorer,
6364
log.Infof("Attempting to connect to node=%x (addrs=%v) to "+
6465
"restore ChannelPoint(%v)",
6566
backup.RemoteNodePub.SerializeCompressed(),
66-
newLogClosure(func() string {
67+
lnutils.NewLogClosure(func() string {
6768
return spew.Sdump(backups[i].Addresses)
6869
}), backup.FundingOutpoint)
6970

contractcourt/breach_arbitrator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightningnetwork/lnd/kvdb"
2121
"github.com/lightningnetwork/lnd/labels"
2222
"github.com/lightningnetwork/lnd/lntypes"
23+
"github.com/lightningnetwork/lnd/lnutils"
2324
"github.com/lightningnetwork/lnd/lnwallet"
2425
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
2526
)
@@ -732,9 +733,10 @@ justiceTxBroadcast:
732733
}
733734
finalTx := justiceTxs.spendAll
734735

735-
brarLog.Debugf("Broadcasting justice tx: %v", newLogClosure(func() string {
736-
return spew.Sdump(finalTx)
737-
}))
736+
brarLog.Debugf("Broadcasting justice tx: %v", lnutils.NewLogClosure(
737+
func() string {
738+
return spew.Sdump(finalTx)
739+
}))
738740

739741
// We'll now attempt to broadcast the transaction which finalized the
740742
// channel's retribution against the cheating counter party.
@@ -857,7 +859,7 @@ Loop:
857859

858860
brarLog.Debugf("Broadcasting justice tx "+
859861
"spending commitment outs: %v",
860-
newLogClosure(func() string {
862+
lnutils.NewLogClosure(func() string {
861863
return spew.Sdump(tx)
862864
}))
863865

@@ -874,7 +876,7 @@ Loop:
874876

875877
brarLog.Debugf("Broadcasting justice tx "+
876878
"spending HTLC outs: %v",
877-
newLogClosure(func() string {
879+
lnutils.NewLogClosure(func() string {
878880
return spew.Sdump(tx)
879881
}))
880882

@@ -891,7 +893,7 @@ Loop:
891893

892894
brarLog.Debugf("Broadcasting justice tx "+
893895
"spending second-level HTLC output: %v",
894-
newLogClosure(func() string {
896+
lnutils.NewLogClosure(func() string {
895897
return spew.Sdump(tx)
896898
}))
897899

contractcourt/chain_watcher.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightningnetwork/lnd/channeldb"
2121
"github.com/lightningnetwork/lnd/fn"
2222
"github.com/lightningnetwork/lnd/input"
23+
"github.com/lightningnetwork/lnd/lnutils"
2324
"github.com/lightningnetwork/lnd/lnwallet"
2425
"github.com/lightningnetwork/lnd/lnwire"
2526
)
@@ -1254,7 +1255,7 @@ func (c *chainWatcher) dispatchContractBreach(spendEvent *chainntnfs.SpendDetail
12541255
spendHeight := uint32(spendEvent.SpendingHeight)
12551256

12561257
log.Debugf("Punishment breach retribution created: %v",
1257-
newLogClosure(func() string {
1258+
lnutils.NewLogClosure(func() string {
12581259
retribution.KeyRing.LocalHtlcKey = nil
12591260
retribution.KeyRing.RemoteHtlcKey = nil
12601261
retribution.KeyRing.ToLocalKey = nil

contractcourt/channel_arbitrator.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/lightningnetwork/lnd/kvdb"
2525
"github.com/lightningnetwork/lnd/labels"
2626
"github.com/lightningnetwork/lnd/lntypes"
27+
"github.com/lightningnetwork/lnd/lnutils"
2728
"github.com/lightningnetwork/lnd/lnwallet"
2829
"github.com/lightningnetwork/lnd/lnwire"
2930
"github.com/lightningnetwork/lnd/sweep"
@@ -465,7 +466,7 @@ func (c *ChannelArbitrator) Start(state *chanArbStartState) error {
465466
}
466467

467468
log.Debugf("Starting ChannelArbitrator(%v), htlc_set=%v, state=%v",
468-
c.cfg.ChanPoint, newLogClosure(func() string {
469+
c.cfg.ChanPoint, lnutils.NewLogClosure(func() string {
469470
return spew.Sdump(c.activeHTLCs)
470471
}), state.currentState,
471472
)
@@ -959,7 +960,7 @@ func (c *ChannelArbitrator) stateStep(
959960
// commitment transaction has already been broadcast.
960961
log.Tracef("ChannelArbitrator(%v): logging chain_actions=%v",
961962
c.cfg.ChanPoint,
962-
newLogClosure(func() string {
963+
lnutils.NewLogClosure(func() string {
963964
return spew.Sdump(chainActions)
964965
}))
965966

@@ -1097,7 +1098,7 @@ func (c *ChannelArbitrator) stateStep(
10971098
log.Infof("Broadcasting force close transaction %v, "+
10981099
"ChannelPoint(%v): %v", closeTx.TxHash(),
10991100
c.cfg.ChanPoint,
1100-
newLogClosure(func() string {
1101+
lnutils.NewLogClosure(func() string {
11011102
return spew.Sdump(closeTx)
11021103
}))
11031104

@@ -1224,7 +1225,7 @@ func (c *ChannelArbitrator) stateStep(
12241225
if len(pktsToSend) != 0 {
12251226
log.Debugf("ChannelArbitrator(%v): sending "+
12261227
"resolution message=%v", c.cfg.ChanPoint,
1227-
newLogClosure(func() string {
1228+
lnutils.NewLogClosure(func() string {
12281229
return spew.Sdump(pktsToSend)
12291230
}))
12301231

@@ -2742,7 +2743,7 @@ func (c *ChannelArbitrator) notifyContractUpdate(upd *ContractUpdate) {
27422743

27432744
log.Tracef("ChannelArbitrator(%v): fresh set of htlcs=%v",
27442745
c.cfg.ChanPoint,
2745-
newLogClosure(func() string {
2746+
lnutils.NewLogClosure(func() string {
27462747
return spew.Sdump(upd)
27472748
}),
27482749
)

contractcourt/log.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,3 @@ func UseBreachLogger(logger btclog.Logger) {
5151
func UseNurseryLogger(logger btclog.Logger) {
5252
utxnLog = logger
5353
}
54-
55-
// logClosure is used to provide a closure over expensive logging operations so
56-
// don't have to be performed when the logging level doesn't warrant it.
57-
type logClosure func() string
58-
59-
// String invokes the underlying function and returns the result.
60-
func (c logClosure) String() string {
61-
return c()
62-
}
63-
64-
// newLogClosure returns a new closure over a function that returns a string
65-
// which itself provides a Stringer interface so that it can be used with the
66-
// logging system.
67-
func newLogClosure(c func() string) logClosure {
68-
return logClosure(c)
69-
}

contractcourt/utxonursery.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/lightningnetwork/lnd/fn"
1919
"github.com/lightningnetwork/lnd/input"
2020
"github.com/lightningnetwork/lnd/labels"
21+
"github.com/lightningnetwork/lnd/lnutils"
2122
"github.com/lightningnetwork/lnd/lnwallet"
2223
"github.com/lightningnetwork/lnd/sweep"
2324
)
@@ -947,7 +948,7 @@ func (u *UtxoNursery) waitForSweepConf(classHeight uint32,
947948
func (u *UtxoNursery) sweepCribOutput(classHeight uint32, baby *babyOutput) error {
948949
utxnLog.Infof("Publishing CLTV-delayed HTLC output using timeout tx "+
949950
"(txid=%v): %v", baby.timeoutTx.TxHash(),
950-
newLogClosure(func() string {
951+
lnutils.NewLogClosure(func() string {
951952
return spew.Sdump(baby.timeoutTx)
952953
}),
953954
)

discovery/bootstrapper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/btcsuite/btcd/btcutil/bech32"
1717
"github.com/davecgh/go-spew/spew"
1818
"github.com/lightningnetwork/lnd/autopilot"
19+
"github.com/lightningnetwork/lnd/lnutils"
1920
"github.com/lightningnetwork/lnd/lnwire"
2021
"github.com/lightningnetwork/lnd/tor"
2122
"github.com/miekg/dns"
@@ -431,7 +432,7 @@ search:
431432
}
432433

433434
log.Tracef("Retrieved SRV records from dns seed: %v",
434-
newLogClosure(func() string {
435+
lnutils.NewLogClosure(func() string {
435436
return spew.Sdump(addrs)
436437
}),
437438
)

discovery/log.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,3 @@ func DisableLog() {
2727
func UseLogger(logger btclog.Logger) {
2828
log = logger
2929
}
30-
31-
// logClosure is used to provide a closure over expensive logging operations
32-
// so don't have to be performed when the logging level doesn't warrant it.
33-
type logClosure func() string
34-
35-
// String invokes the underlying function and returns the result.
36-
func (c logClosure) String() string {
37-
return c()
38-
}
39-
40-
// newLogClosure returns a new closure over a function that returns a string
41-
// which itself provides a Stringer interface so that it can be used with the
42-
// logging system.
43-
func newLogClosure(c func() string) logClosure {
44-
return logClosure(c)
45-
}

0 commit comments

Comments
 (0)