X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Futils.rs;h=1248221a32ee3b800d8ec3fec9964d82649d9791;hb=c7f19bef249ad28bf0adc1ef1ec89d86468841da;hp=ed016be3f8eded216c4a35dafe648a44fc7c0550;hpb=f07f4b90f8de76d594328e11e36d094cdb936097;p=rust-lightning diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index ed016be3f..1248221a3 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -14,7 +14,7 @@ use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; use lightning::routing::gossip::RoutingFees; use lightning::routing::router::{RouteHint, RouteHintHop, Router}; -use lightning::util::logger::Logger; +use lightning::util::logger::{Logger, Record}; use secp256k1::PublicKey; use core::ops::Deref; use core::time::Duration; @@ -626,6 +626,7 @@ where log_trace!(logger, "Considering {} channels for invoice route hints", channels.len()); for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) { + let logger = WithChannelDetails::from(logger, &channel); if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() { log_trace!(logger, "Ignoring channel {} for invoice route hints", &channel.channel_id); continue; @@ -710,6 +711,7 @@ where .into_iter() .map(|(_, channel)| channel) .filter(|channel| { + let logger = WithChannelDetails::from(logger, &channel); let has_enough_capacity = channel.inbound_capacity_msat >= min_inbound_capacity; let include_channel = if has_pub_unconf_chan { // 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, current_channe current_channel > candidate_channel } +/// Adds relevant context to a [`Record`] before passing it to the wrapped [`Logger`]. +struct WithChannelDetails<'a, 'b, L: Deref> where L::Target: Logger { + /// The logger to delegate to after adding context to the record. + logger: &'a L, + /// The [`ChannelDetails`] for adding relevant context to the logged record. + details: &'b ChannelDetails +} + +impl<'a, 'b, L: Deref> Logger for WithChannelDetails<'a, 'b, L> where L::Target: Logger { + fn log(&self, mut record: Record) { + record.peer_id = Some(self.details.counterparty.node_id); + record.channel_id = Some(self.details.channel_id); + self.logger.log(record) + } +} + +impl<'a, 'b, L: Deref> WithChannelDetails<'a, 'b, L> where L::Target: Logger { + fn from(logger: &'a L, details: &'b ChannelDetails) -> Self { + Self { logger, details } + } +} + #[cfg(test)] mod test { use core::cell::RefCell;