]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add WithChannelContext
authorhenghonglee <henghong.lee@gmail.com>
Tue, 5 Sep 2023 03:35:51 +0000 (11:35 +0800)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 1 Dec 2023 17:30:19 +0000 (11:30 -0600)
lightning/src/ln/channel.rs

index f3f46fe3b0a427f2cccabd37755fd26e3709569d..6c645f85feb1135ff49e081dd625b3fe72cd0f7a 100644 (file)
@@ -42,7 +42,7 @@ use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Reci
 use crate::events::ClosureReason;
 use crate::routing::gossip::NodeId;
 use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
-use crate::util::logger::{Logger, WithContext};
+use crate::util::logger::{Logger, Record, WithContext};
 use crate::util::errors::APIError;
 use crate::util::config::{UserConfig, ChannelConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits, MaxDustHTLCExposure};
 use crate::util::scid_utils::scid_from_parts;
@@ -414,6 +414,33 @@ impl fmt::Display for ChannelError {
        }
 }
 
+pub(super) struct WithChannelContext<'a, L: Deref> where L::Target: Logger {
+       pub logger: &'a L,
+       pub peer_id: Option<PublicKey>,
+       pub channel_id: Option<ChannelId>,
+}
+
+impl<'a, L: Deref> Logger for WithChannelContext<'a, L> where L::Target: Logger {
+       fn log(&self, mut record: Record) {
+               record.peer_id = self.peer_id;
+               record.channel_id = self.channel_id;
+               self.logger.log(record)
+       }
+}
+
+impl<'a, 'b, L: Deref> WithChannelContext<'a, L>
+where L::Target: Logger {
+       pub(super) fn from<S: Deref>(logger: &'a L, context: &'b ChannelContext<S>) -> Self
+       where S::Target: SignerProvider
+       {
+               WithChannelContext {
+                       logger,
+                       peer_id: Some(context.counterparty_node_id),
+                       channel_id: Some(context.channel_id),
+               }
+       }
+}
+
 macro_rules! secp_check {
        ($res: expr, $err: expr) => {
                match $res {