@@ -20,7 +20,7 @@ use core::cmp;
2020use core:: fmt;
2121use core:: ops:: Deref ;
2222
23- use crate :: ln:: ChannelId ;
23+ use crate :: ln:: { ChannelId , PaymentHash } ;
2424#[ cfg( c_bindings) ]
2525use crate :: prelude:: * ; // Needed for String
2626
@@ -120,6 +120,11 @@ pub struct Record<$($args)?> {
120120 pub file: & ' static str ,
121121 /// The line containing the message.
122122 pub line: u32 ,
123+ /// The payment hash.
124+ ///
125+ /// Note that this is only filled in for logs pertaining to a specific payment, and will be
126+ /// `None` for logs which are not directly related to a payment.
127+ pub payment_hash: Option <PaymentHash >,
123128}
124129
125130impl <$( $args) ?> Record <$( $args) ?> {
@@ -129,7 +134,8 @@ impl<$($args)?> Record<$($args)?> {
129134 #[ inline]
130135 pub fn new<$( $nonstruct_args) ?>(
131136 level: Level , peer_id: Option <PublicKey >, channel_id: Option <ChannelId >,
132- args: fmt:: Arguments <' a>, module_path: & ' static str , file: & ' static str , line: u32
137+ args: fmt:: Arguments <' a>, module_path: & ' static str , file: & ' static str , line: u32 ,
138+ payment_hash: Option <PaymentHash >
133139 ) -> Record <$( $args) ?> {
134140 Record {
135141 level,
@@ -142,6 +148,7 @@ impl<$($args)?> Record<$($args)?> {
142148 module_path,
143149 file,
144150 line,
151+ payment_hash,
145152 }
146153 }
147154}
@@ -168,6 +175,8 @@ pub struct WithContext<'a, L: Deref> where L::Target: Logger {
168175 peer_id : Option < PublicKey > ,
169176 /// The channel id of the channel pertaining to the logged record.
170177 channel_id : Option < ChannelId > ,
178+ /// The payment hash of the payment pertaining to the logged record.
179+ payment_hash : Option < PaymentHash >
171180}
172181
173182impl < ' a , L : Deref > Logger for WithContext < ' a , L > where L :: Target : Logger {
@@ -178,17 +187,21 @@ impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
178187 if self . channel_id . is_some ( ) {
179188 record. channel_id = self . channel_id ;
180189 }
190+ if self . payment_hash . is_some ( ) {
191+ record. payment_hash = self . payment_hash ;
192+ }
181193 self . logger . log ( record)
182194 }
183195}
184196
185197impl < ' a , L : Deref > WithContext < ' a , L > where L :: Target : Logger {
186198 /// 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 {
199+ pub fn from ( logger : & ' a L , peer_id : Option < PublicKey > , channel_id : Option < ChannelId > , payment_hash : Option < PaymentHash > ) -> Self {
188200 WithContext {
189201 logger,
190202 peer_id,
191203 channel_id,
204+ payment_hash,
192205 }
193206 }
194207}
@@ -244,7 +257,7 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
244257#[ cfg( test) ]
245258mod tests {
246259 use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
247- use crate :: ln:: ChannelId ;
260+ use crate :: ln:: { ChannelId , PaymentHash } ;
248261 use crate :: util:: logger:: { Logger , Level , WithContext } ;
249262 use crate :: util:: test_utils:: TestLogger ;
250263 use crate :: sync:: Arc ;
@@ -291,7 +304,8 @@ mod tests {
291304 let logger = & TestLogger :: new ( ) ;
292305 let secp_ctx = Secp256k1 :: new ( ) ;
293306 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 ] ) ) ) ;
307+ let payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
308+ let context_logger = WithContext :: from ( & logger, Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
295309 log_error ! ( context_logger, "This is an error" ) ;
296310 log_warn ! ( context_logger, "This is an error" ) ;
297311 log_debug ! ( context_logger, "This is an error" ) ;
@@ -308,8 +322,9 @@ mod tests {
308322 let logger = & TestLogger :: new ( ) ;
309323 let secp_ctx = Secp256k1 :: new ( ) ;
310324 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 ) ;
325+ let payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
326+ let context_logger = & WithContext :: from ( & logger, None , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
327+ let full_context_logger = WithContext :: from ( & context_logger, Some ( pk) , None , None ) ;
313328 log_error ! ( full_context_logger, "This is an error" ) ;
314329 log_warn ! ( full_context_logger, "This is an error" ) ;
315330 log_debug ! ( full_context_logger, "This is an error" ) ;
0 commit comments