Skip to content

Commit 745c1e8

Browse files
henghongleejkczyz
authored andcommitted
Add WithChannelDetails
1 parent 3ab8d32 commit 745c1e8

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lightning-invoice/src/utils.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
1414
use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
1515
use lightning::routing::gossip::RoutingFees;
1616
use lightning::routing::router::{RouteHint, RouteHintHop, Router};
17-
use lightning::util::logger::Logger;
17+
use lightning::util::logger::{Logger, Record};
1818
use secp256k1::PublicKey;
1919
use core::ops::Deref;
2020
use core::time::Duration;
@@ -626,6 +626,7 @@ where
626626

627627
log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
628628
for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
629+
let logger = WithChannelDetails::from(logger, &channel);
629630
if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() {
630631
log_trace!(logger, "Ignoring channel {} for invoice route hints", &channel.channel_id);
631632
continue;
@@ -710,6 +711,7 @@ where
710711
.into_iter()
711712
.map(|(_, channel)| channel)
712713
.filter(|channel| {
714+
let logger = WithChannelDetails::from(logger, &channel);
713715
let has_enough_capacity = channel.inbound_capacity_msat >= min_inbound_capacity;
714716
let include_channel = if has_pub_unconf_chan {
715717
// If we have a public channel, but it doesn't have enough confirmations to (yet)
@@ -790,6 +792,28 @@ fn prefer_current_channel(min_inbound_capacity_msat: Option<u64>, current_channe
790792
current_channel > candidate_channel
791793
}
792794

795+
/// Adds relevant context to a [`Record`] before passing it to the wrapped [`Logger`].
796+
struct WithChannelDetails<'a, 'b, L: Deref> where L::Target: Logger {
797+
/// The logger to delegate to after adding context to the record.
798+
logger: &'a L,
799+
/// The [`ChannelDetails`] for adding relevant context to the logged record.
800+
details: &'b ChannelDetails
801+
}
802+
803+
impl<'a, 'b, L: Deref> Logger for WithChannelDetails<'a, 'b, L> where L::Target: Logger {
804+
fn log(&self, mut record: Record) {
805+
record.peer_id = Some(self.details.counterparty.node_id);
806+
record.channel_id = Some(self.details.channel_id);
807+
self.logger.log(record)
808+
}
809+
}
810+
811+
impl<'a, 'b, L: Deref> WithChannelDetails<'a, 'b, L> where L::Target: Logger {
812+
fn from(logger: &'a L, details: &'b ChannelDetails) -> Self {
813+
Self { logger, details }
814+
}
815+
}
816+
793817
#[cfg(test)]
794818
mod test {
795819
use core::cell::RefCell;

0 commit comments

Comments
 (0)