@@ -169,18 +169,24 @@ pub trait Logger {
169
169
///
170
170
/// This is not exported to bindings users as lifetimes are problematic and there's little reason
171
171
/// for this to be used downstream anyway.
172
- pub struct WithContext < ' a , L : Deref > where L :: Target : Logger {
172
+ pub struct WithContext < ' a , L : Deref >
173
+ where
174
+ L :: Target : Logger ,
175
+ {
173
176
/// The logger to delegate to after adding context to the record.
174
177
logger : & ' a L ,
175
178
/// The node id of the peer pertaining to the logged record.
176
179
peer_id : Option < PublicKey > ,
177
180
/// The channel id of the channel pertaining to the logged record.
178
181
channel_id : Option < ChannelId > ,
179
182
/// The payment hash of the payment pertaining to the logged record.
180
- payment_hash : Option < PaymentHash >
183
+ payment_hash : Option < PaymentHash > ,
181
184
}
182
185
183
- impl < ' a , L : Deref > Logger for WithContext < ' a , L > where L :: Target : Logger {
186
+ impl < ' a , L : Deref > Logger for WithContext < ' a , L >
187
+ where
188
+ L :: Target : Logger ,
189
+ {
184
190
fn log ( & self , mut record : Record ) {
185
191
if self . peer_id . is_some ( ) {
186
192
record. peer_id = self . peer_id
@@ -195,15 +201,16 @@ impl<'a, L: Deref> Logger for WithContext<'a, L> where L::Target: Logger {
195
201
}
196
202
}
197
203
198
- impl < ' a , L : Deref > WithContext < ' a , L > where L :: Target : Logger {
204
+ impl < ' a , L : Deref > WithContext < ' a , L >
205
+ where
206
+ L :: Target : Logger ,
207
+ {
199
208
/// Wraps the given logger, providing additional context to any logged records.
200
- pub fn from ( logger : & ' a L , peer_id : Option < PublicKey > , channel_id : Option < ChannelId > , payment_hash : Option < PaymentHash > ) -> Self {
201
- WithContext {
202
- logger,
203
- peer_id,
204
- channel_id,
205
- payment_hash,
206
- }
209
+ pub fn from (
210
+ logger : & ' a L , peer_id : Option < PublicKey > , channel_id : Option < ChannelId > ,
211
+ payment_hash : Option < PaymentHash > ,
212
+ ) -> Self {
213
+ WithContext { logger, peer_id, channel_id, payment_hash }
207
214
}
208
215
}
209
216
@@ -257,12 +264,12 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo
257
264
258
265
#[ cfg( test) ]
259
266
mod tests {
260
- use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
261
267
use crate :: ln:: types:: ChannelId ;
262
268
use crate :: ln:: PaymentHash ;
263
- use crate :: util:: logger:: { Logger , Level , WithContext } ;
264
- use crate :: util:: test_utils:: TestLogger ;
265
269
use crate :: sync:: Arc ;
270
+ use crate :: util:: logger:: { Level , Logger , WithContext } ;
271
+ use crate :: util:: test_utils:: TestLogger ;
272
+ use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
266
273
267
274
#[ test]
268
275
fn test_level_show ( ) {
@@ -272,14 +279,12 @@ mod tests {
272
279
}
273
280
274
281
struct WrapperLog {
275
- logger : Arc < dyn Logger >
282
+ logger : Arc < dyn Logger > ,
276
283
}
277
284
278
285
impl WrapperLog {
279
286
fn new ( logger : Arc < dyn Logger > ) -> WrapperLog {
280
- WrapperLog {
281
- logger,
282
- }
287
+ WrapperLog { logger }
283
288
}
284
289
285
290
fn call_macros ( & self ) {
@@ -295,7 +300,7 @@ mod tests {
295
300
#[ test]
296
301
fn test_logging_macros ( ) {
297
302
let logger = TestLogger :: new ( ) ;
298
- let logger : Arc < dyn Logger > = Arc :: new ( logger) ;
303
+ let logger: Arc < dyn Logger > = Arc :: new ( logger) ;
299
304
let wrapper = WrapperLog :: new ( Arc :: clone ( & logger) ) ;
300
305
wrapper. call_macros ( ) ;
301
306
}
@@ -306,15 +311,19 @@ mod tests {
306
311
let secp_ctx = Secp256k1 :: new ( ) ;
307
312
let pk = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
308
313
let payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
309
- let context_logger = WithContext :: from ( & logger, Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
314
+ let context_logger =
315
+ WithContext :: from ( & logger, Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
310
316
log_error ! ( context_logger, "This is an error" ) ;
311
317
log_warn ! ( context_logger, "This is an error" ) ;
312
318
log_debug ! ( context_logger, "This is an error" ) ;
313
319
log_trace ! ( context_logger, "This is an error" ) ;
314
320
log_gossip ! ( context_logger, "This is an error" ) ;
315
321
log_info ! ( context_logger, "This is an error" ) ;
316
322
logger. assert_log_context_contains (
317
- "lightning::util::logger::tests" , Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) , 6
323
+ "lightning::util::logger::tests" ,
324
+ Some ( pk) ,
325
+ Some ( ChannelId ( [ 0 ; 32 ] ) ) ,
326
+ 6 ,
318
327
) ;
319
328
}
320
329
@@ -324,7 +333,8 @@ mod tests {
324
333
let secp_ctx = Secp256k1 :: new ( ) ;
325
334
let pk = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
326
335
let payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
327
- let context_logger = & WithContext :: from ( & logger, None , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
336
+ let context_logger =
337
+ & WithContext :: from ( & logger, None , Some ( ChannelId ( [ 0 ; 32 ] ) ) , Some ( payment_hash) ) ;
328
338
let full_context_logger = WithContext :: from ( & context_logger, Some ( pk) , None , None ) ;
329
339
log_error ! ( full_context_logger, "This is an error" ) ;
330
340
log_warn ! ( full_context_logger, "This is an error" ) ;
@@ -333,7 +343,10 @@ mod tests {
333
343
log_gossip ! ( full_context_logger, "This is an error" ) ;
334
344
log_info ! ( full_context_logger, "This is an error" ) ;
335
345
logger. assert_log_context_contains (
336
- "lightning::util::logger::tests" , Some ( pk) , Some ( ChannelId ( [ 0 ; 32 ] ) ) , 6
346
+ "lightning::util::logger::tests" ,
347
+ Some ( pk) ,
348
+ Some ( ChannelId ( [ 0 ; 32 ] ) ) ,
349
+ 6 ,
337
350
) ;
338
351
}
339
352
0 commit comments