Skip to content

Commit dfddeec

Browse files
committed
protofsm: use structured logging
1 parent 575ea7a commit dfddeec

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

protofsm/state_machine.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ func (s *StateMachine[Event, Env]) Stop() {
235235
//
236236
// TODO(roasbeef): bool if processed?
237237
func (s *StateMachine[Event, Env]) SendEvent(ctx context.Context, event Event) {
238-
s.log.Debugf("Sending event: %v", lnutils.SpewLogClosure(event))
238+
s.log.DebugS(ctx, "Sending event",
239+
"event", lnutils.SpewLogClosure(event))
239240

240241
select {
241242
case s.events <- event:
@@ -273,7 +274,7 @@ func (s *StateMachine[Event, Env]) SendMessage(ctx context.Context,
273274
return false
274275
}
275276

276-
s.log.Debugf("Sending msg: %v", lnutils.SpewLogClosure(msg))
277+
s.log.DebugS(ctx, "Sending msg", "msg", lnutils.SpewLogClosure(msg))
277278

278279
// Otherwise, try to map the message using the default message mapper.
279280
// If we can't extract an event, then we'll return false to indicate
@@ -344,10 +345,9 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
344345
// any preconditions as well as post-send events.
345346
case *SendMsgEvent[Event]:
346347
sendAndCleanUp := func() error {
347-
s.log.Debugf("Sending message to target(%x): "+
348-
"%v",
349-
daemonEvent.TargetPeer.SerializeCompressed(),
350-
lnutils.SpewLogClosure(daemonEvent.Msgs))
348+
s.log.DebugS(ctx, "Sending message to target",
349+
btclog.Hex6("target", daemonEvent.TargetPeer.SerializeCompressed()),
350+
"messages", lnutils.SpewLogClosure(daemonEvent.Msgs))
351351

352352
err := s.cfg.Daemon.SendMessages(
353353
daemonEvent.TargetPeer, daemonEvent.Msgs,
@@ -362,9 +362,8 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
362362
return fn.MapOptionZ(daemonEvent.PostSendEvent, func(event Event) error { //nolint:ll
363363
launched := s.wg.Go(
364364
ctx, func(ctx context.Context) {
365-
s.log.Debugf("Sending "+
366-
"post-send event: %v",
367-
lnutils.SpewLogClosure(event))
365+
s.log.DebugS(ctx, "Sending post-send event",
366+
"event", lnutils.SpewLogClosure(event))
368367

369368
s.SendEvent(ctx, event)
370369
},
@@ -393,7 +392,7 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
393392
)
394393
defer predicateTicker.Stop()
395394

396-
s.log.Infof("Waiting for send predicate to be true")
395+
s.log.InfoS(ctx, "Waiting for send predicate to be true")
397396

398397
for {
399398
select {
@@ -406,13 +405,11 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
406405
)
407406

408407
if canSend {
409-
s.log.Infof("Send active " +
410-
"predicate")
408+
s.log.InfoS(ctx, "Send active predicate")
411409

412410
err := sendAndCleanUp()
413411
if err != nil {
414-
//nolint:ll
415-
s.log.Errorf("Unable to send message: %v", err)
412+
s.log.ErrorS(ctx, "Unable to send message", err)
416413
}
417414

418415
return
@@ -433,8 +430,8 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
433430
// If this is a broadcast transaction event, then we'll broadcast with
434431
// the label attached.
435432
case *BroadcastTxn:
436-
s.log.Debugf("Broadcasting txn, txid=%v",
437-
daemonEvent.Tx.TxHash())
433+
s.log.DebugS(ctx, "Broadcasting txn",
434+
"txid", daemonEvent.Tx.TxHash())
438435

439436
err := s.cfg.Daemon.BroadcastTransaction(
440437
daemonEvent.Tx, daemonEvent.Label,
@@ -448,7 +445,8 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
448445
// The state machine has requested a new event to be sent once a
449446
// transaction spending a specified outpoint has confirmed.
450447
case *RegisterSpend[Event]:
451-
s.log.Debugf("Registering spend: %v", daemonEvent.OutPoint)
448+
s.log.DebugS(ctx, "Registering spend",
449+
"outpoint", daemonEvent.OutPoint)
452450

453451
spendEvent, err := s.cfg.Daemon.RegisterSpendNtfn(
454452
&daemonEvent.OutPoint, daemonEvent.PkScript,
@@ -492,7 +490,8 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
492490
// The state machine has requested a new event to be sent once a
493491
// specified txid+pkScript pair has confirmed.
494492
case *RegisterConf[Event]:
495-
s.log.Debugf("Registering conf: %v", daemonEvent.Txid)
493+
s.log.DebugS(ctx, "Registering conf",
494+
"txid", daemonEvent.Txid)
496495

497496
numConfs := daemonEvent.NumConfs.UnwrapOr(1)
498497
confEvent, err := s.cfg.Daemon.RegisterConfirmationsNtfn(
@@ -543,8 +542,8 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
543542
currentState State[Event, Env], newEvent Event) (State[Event, Env],
544543
error) {
545544

546-
s.log.Debugf("Applying new event: %v",
547-
lnutils.SpewLogClosure(newEvent))
545+
s.log.DebugS(ctx, "Applying new event",
546+
"event", lnutils.SpewLogClosure(newEvent))
548547

549548
eventQueue := fn.NewQueue(newEvent)
550549

@@ -556,8 +555,8 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
556555
//nolint:ll
557556
for nextEvent := eventQueue.Dequeue(); nextEvent.IsSome(); nextEvent = eventQueue.Dequeue() {
558557
err := fn.MapOptionZ(nextEvent, func(event Event) error {
559-
s.log.Debugf("Processing event: %v",
560-
lnutils.SpewLogClosure(event))
558+
s.log.DebugS(ctx, "Processing event",
559+
"event", lnutils.SpewLogClosure(event))
561560

562561
// Apply the state transition function of the current
563562
// state given this new event and our existing env.
@@ -587,12 +586,8 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
587586
//
588587
//nolint:ll
589588
for _, inEvent := range events.InternalEvent {
590-
s.log.Debugf("Adding "+
591-
"new internal event "+
592-
"to queue: %v",
593-
lnutils.SpewLogClosure(
594-
inEvent,
595-
))
589+
s.log.DebugS(ctx, "Adding new internal event to queue",
590+
"event", lnutils.SpewLogClosure(inEvent))
596591

597592
eventQueue.Enqueue(inEvent)
598593
}
@@ -603,9 +598,9 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
603598
return err
604599
}
605600

606-
s.log.Infof("State transition: from_state=%T, "+
607-
"to_state=%T", currentState,
608-
transition.NextState)
601+
s.log.InfoS(ctx, "State transition",
602+
btclog.Fmt("from_state", "%T", currentState),
603+
btclog.Fmt("to_state", "%T", transition.NextState))
609604

610605
// With our events processed, we'll now update our
611606
// internal state.
@@ -631,7 +626,7 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
631626
// incoming events, and then drives the state machine forward until it reaches
632627
// a terminal state.
633628
func (s *StateMachine[Event, Env]) driveMachine(ctx context.Context) {
634-
s.log.Debugf("Starting state machine")
629+
s.log.DebugS(ctx, "Starting state machine")
635630

636631
currentState := s.cfg.InitialState
637632

@@ -641,7 +636,7 @@ func (s *StateMachine[Event, Env]) driveMachine(ctx context.Context) {
641636
return s.executeDaemonEvent(ctx, event)
642637
})
643638
if err != nil {
644-
s.log.Errorf("Unable to execute init event: %v", err)
639+
s.log.ErrorS(ctx, "Unable to execute init event", err)
645640
return
646641
}
647642

@@ -661,7 +656,7 @@ func (s *StateMachine[Event, Env]) driveMachine(ctx context.Context) {
661656
if err != nil {
662657
s.cfg.ErrorReporter.ReportError(err)
663658

664-
s.log.Errorf("Unable to apply event: %v", err)
659+
s.log.ErrorS(ctx, "Unable to apply event", err)
665660

666661
// An error occurred, so we'll tear down the
667662
// entire state machine as we can't proceed.

0 commit comments

Comments
 (0)