@@ -21,6 +21,7 @@ use core::fmt;
2121use core:: ops:: Deref ;
2222
2323use crate :: ln:: types:: ChannelId ;
24+ use crate :: ln:: PaymentHash ;
2425#[ cfg( c_bindings) ]
2526use crate :: prelude:: * ; // Needed for String
2627
@@ -120,6 +121,11 @@ pub struct Record<$($args)?> {
120121 pub file: & ' static str ,
121122 /// The line containing the message.
122123 pub line: u32 ,
124+ /// The payment hash.
125+ ///
126+ /// Note that this is only filled in for logs pertaining to a specific payment, and will be
127+ /// `None` for logs which are not directly related to a payment.
128+ pub payment_hash: Option <PaymentHash >,
123129}
124130
125131impl <$( $args) ?> Record <$( $args) ?> {
@@ -129,7 +135,8 @@ impl<$($args)?> Record<$($args)?> {
129135 #[ inline]
130136 pub fn new<$( $nonstruct_args) ?>(
131137 level: Level , peer_id: Option <PublicKey >, channel_id: Option <ChannelId >,
132- args: fmt:: Arguments <' a>, module_path: & ' static str , file: & ' static str , line: u32
138+ args: fmt:: Arguments <' a>, module_path: & ' static str , file: & ' static str , line: u32 ,
139+ payment_hash: Option <PaymentHash >
133140 ) -> Record <$( $args) ?> {
134141 Record {
135142 level,
@@ -142,6 +149,7 @@ impl<$($args)?> Record<$($args)?> {
142149 module_path,
143150 file,
144151 line,
152+ payment_hash,
145153 }
146154 }
147155}
@@ -168,6 +176,8 @@ pub struct WithContext<'a, L: Deref> where L::Target: Logger {
168176 peer_id : Option < PublicKey > ,
169177 /// The channel id of the channel pertaining to the logged record.
170178 channel_id : Option < ChannelId > ,
179+ /// The payment hash of the payment pertaining to the logged record.
180+ payment_hash : Option < PaymentHash >
171181}
172182
173183impl < ' a , L : Deref > Logger for WithContext < ' a , L > where L :: Target : Logger {
@@ -178,17 +188,21 @@ impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
178188 if self . channel_id . is_some ( ) {
179189 record. channel_id = self . channel_id ;
180190 }
191+ if self . payment_hash . is_some ( ) {
192+ record. payment_hash = self . payment_hash ;
193+ }
181194 self . logger . log ( record)
182195 }
183196}
184197
185198impl < ' a , L : Deref > WithContext < ' a , L > where L :: Target : Logger {
186199 /// Wraps the given logger, providing additional context to any logged records.
187- pub fn from ( logger : & ' a L , peer_id : Option < PublicKey > , channel_id : Option < ChannelId > ) -> Self {
200+ pub fn from ( logger : & ' a L , peer_id : Option < PublicKey > , channel_id : Option < ChannelId > , payment_hash : Option < PaymentHash > ) -> Self {
188201 WithContext {
189202 logger,
190203 peer_id,
191204 channel_id,
205+ payment_hash,
192206 }
193207 }
194208}
@@ -245,6 +259,7 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
245259mod tests {
246260 use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
247261 use crate :: ln:: types:: ChannelId ;
262+ use crate :: ln:: PaymentHash ;
248263 use crate :: util:: logger:: { Logger , Level , WithContext } ;
249264 use crate :: util:: test_utils:: TestLogger ;
250265 use crate :: sync:: Arc ;
@@ -291,7 +306,8 @@ mod tests {
291306 let logger = & TestLogger :: new ( ) ;
292307 let secp_ctx = Secp256k1 :: new ( ) ;
293308 let pk = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
294- let context_logger = WithContext :: from ( & logger, Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) ) ;
309+ let payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
310+ let context_logger = WithContext :: from ( & logger, Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
295311 log_error ! ( context_logger, "This is an error" ) ;
296312 log_warn ! ( context_logger, "This is an error" ) ;
297313 log_debug ! ( context_logger, "This is an error" ) ;
@@ -308,8 +324,9 @@ mod tests {
308324 let logger = & TestLogger :: new ( ) ;
309325 let secp_ctx = Secp256k1 :: new ( ) ;
310326 let pk = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
311- let context_logger = & WithContext :: from ( & logger, None , Some ( ChannelId ( [ 0 ; 32 ] ) ) ) ;
312- let full_context_logger = WithContext :: from ( & context_logger, Some ( pk) , None ) ;
327+ let payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
328+ let context_logger = & WithContext :: from ( & logger, None , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
329+ let full_context_logger = WithContext :: from ( & context_logger, Some ( pk) , None , None ) ;
313330 log_error ! ( full_context_logger, "This is an error" ) ;
314331 log_warn ! ( full_context_logger, "This is an error" ) ;
315332 log_debug ! ( full_context_logger, "This is an error" ) ;
0 commit comments