@@ -235,7 +235,9 @@ pub trait Logger {
235
235
fn log ( & mut self , record : & LogRecord ) ;
236
236
}
237
237
238
- struct DefaultLogger { handle : Stderr }
238
+ struct DefaultLogger {
239
+ handle : Stderr ,
240
+ }
239
241
240
242
/// Wraps the log level with fmt implementations.
241
243
#[ derive( Copy , Clone , PartialEq , PartialOrd , Debug ) ]
@@ -246,7 +248,7 @@ impl fmt::Display for LogLevel {
246
248
let LogLevel ( level) = * self ;
247
249
match LOG_LEVEL_NAMES . get ( level as usize - 1 ) {
248
250
Some ( ref name) => fmt:: Display :: fmt ( name, fmt) ,
249
- None => fmt:: Display :: fmt ( & level, fmt)
251
+ None => fmt:: Display :: fmt ( & level, fmt) ,
250
252
}
251
253
}
252
254
}
@@ -301,11 +303,10 @@ pub fn log(level: u32, loc: &'static LogLocation, args: fmt::Arguments) {
301
303
// Completely remove the local logger from TLS in case anyone attempts to
302
304
// frob the slot while we're doing the logging. This will destroy any logger
303
305
// set during logging.
304
- let mut logger: Box < Logger + Send > = LOCAL_LOGGER . with ( |s| {
305
- s. borrow_mut ( ) . take ( )
306
- } ) . unwrap_or_else ( || {
307
- box DefaultLogger { handle : io:: stderr ( ) }
308
- } ) ;
306
+ let mut logger: Box < Logger + Send > = LOCAL_LOGGER . with ( |s| s. borrow_mut ( ) . take ( ) )
307
+ . unwrap_or_else ( || {
308
+ box DefaultLogger { handle : io:: stderr ( ) }
309
+ } ) ;
309
310
logger. log ( & LogRecord {
310
311
level : LogLevel ( level) ,
311
312
args : args,
@@ -320,22 +321,21 @@ pub fn log(level: u32, loc: &'static LogLocation, args: fmt::Arguments) {
320
321
/// safely
321
322
#[ doc( hidden) ]
322
323
#[ inline( always) ]
323
- pub fn log_level ( ) -> u32 { unsafe { LOG_LEVEL } }
324
+ pub fn log_level ( ) -> u32 {
325
+ unsafe { LOG_LEVEL }
326
+ }
324
327
325
328
/// Replaces the thread-local logger with the specified logger, returning the old
326
329
/// logger.
327
330
pub fn set_logger ( logger : Box < Logger + Send > ) -> Option < Box < Logger + Send > > {
328
331
let mut l = Some ( logger) ;
329
- LOCAL_LOGGER . with ( |slot| {
330
- mem:: replace ( & mut * slot. borrow_mut ( ) , l. take ( ) )
331
- } )
332
+ LOCAL_LOGGER . with ( |slot| mem:: replace ( & mut * slot. borrow_mut ( ) , l. take ( ) ) )
332
333
}
333
334
334
335
/// A LogRecord is created by the logging macros, and passed as the only
335
336
/// argument to Loggers.
336
337
#[ derive( Debug ) ]
337
338
pub struct LogRecord < ' a > {
338
-
339
339
/// The module path of where the LogRecord originated.
340
340
pub module_path : & ' a str ,
341
341
@@ -373,7 +373,9 @@ pub fn mod_enabled(level: u32, module: &str) -> bool {
373
373
// again to whether they should really be here or not. Hence, despite this
374
374
// check being expanded manually in the logging macro, this function checks
375
375
// the log level again.
376
- if level > unsafe { LOG_LEVEL } { return false }
376
+ if level > unsafe { LOG_LEVEL } {
377
+ return false
378
+ }
377
379
378
380
// This assertion should never get tripped unless we're in an at_exit
379
381
// handler after logging has been torn down and a logging attempt was made.
@@ -385,14 +387,11 @@ pub fn mod_enabled(level: u32, module: &str) -> bool {
385
387
}
386
388
}
387
389
388
- fn enabled ( level : u32 ,
389
- module : & str ,
390
- iter : slice:: Iter < directive:: LogDirective > )
391
- -> bool {
390
+ fn enabled ( level : u32 , module : & str , iter : slice:: Iter < directive:: LogDirective > ) -> bool {
392
391
// Search for the longest match, the vector is assumed to be pre-sorted.
393
392
for directive in iter. rev ( ) {
394
393
match directive. name {
395
- Some ( ref name) if !module. starts_with ( & name[ ..] ) => { } ,
394
+ Some ( ref name) if !module. starts_with ( & name[ ..] ) => { }
396
395
Some ( ..) | None => {
397
396
return level <= directive. level
398
397
}
@@ -445,16 +444,14 @@ mod tests {
445
444
446
445
#[ test]
447
446
fn match_full_path ( ) {
448
- let dirs = [
449
- LogDirective {
450
- name : Some ( "crate2" . to_string ( ) ) ,
451
- level : 3
452
- } ,
453
- LogDirective {
454
- name : Some ( "crate1::mod1" . to_string ( ) ) ,
455
- level : 2
456
- }
457
- ] ;
447
+ let dirs = [ LogDirective {
448
+ name : Some ( "crate2" . to_string ( ) ) ,
449
+ level : 3 ,
450
+ } ,
451
+ LogDirective {
452
+ name : Some ( "crate1::mod1" . to_string ( ) ) ,
453
+ level : 2 ,
454
+ } ] ;
458
455
assert ! ( enabled( 2 , "crate1::mod1" , dirs. iter( ) ) ) ;
459
456
assert ! ( !enabled( 3 , "crate1::mod1" , dirs. iter( ) ) ) ;
460
457
assert ! ( enabled( 3 , "crate2" , dirs. iter( ) ) ) ;
@@ -463,49 +460,72 @@ mod tests {
463
460
464
461
#[ test]
465
462
fn no_match ( ) {
466
- let dirs = [
467
- LogDirective { name : Some ( "crate2" . to_string ( ) ) , level : 3 } ,
468
- LogDirective { name : Some ( "crate1::mod1" . to_string ( ) ) , level : 2 }
469
- ] ;
463
+ let dirs = [ LogDirective {
464
+ name : Some ( "crate2" . to_string ( ) ) ,
465
+ level : 3 ,
466
+ } ,
467
+ LogDirective {
468
+ name : Some ( "crate1::mod1" . to_string ( ) ) ,
469
+ level : 2 ,
470
+ } ] ;
470
471
assert ! ( !enabled( 2 , "crate3" , dirs. iter( ) ) ) ;
471
472
}
472
473
473
474
#[ test]
474
475
fn match_beginning ( ) {
475
- let dirs = [
476
- LogDirective { name : Some ( "crate2" . to_string ( ) ) , level : 3 } ,
477
- LogDirective { name : Some ( "crate1::mod1" . to_string ( ) ) , level : 2 }
478
- ] ;
476
+ let dirs = [ LogDirective {
477
+ name : Some ( "crate2" . to_string ( ) ) ,
478
+ level : 3 ,
479
+ } ,
480
+ LogDirective {
481
+ name : Some ( "crate1::mod1" . to_string ( ) ) ,
482
+ level : 2 ,
483
+ } ] ;
479
484
assert ! ( enabled( 3 , "crate2::mod1" , dirs. iter( ) ) ) ;
480
485
}
481
486
482
487
#[ test]
483
488
fn match_beginning_longest_match ( ) {
484
- let dirs = [
485
- LogDirective { name : Some ( "crate2" . to_string ( ) ) , level : 3 } ,
486
- LogDirective { name : Some ( "crate2::mod" . to_string ( ) ) , level : 4 } ,
487
- LogDirective { name : Some ( "crate1::mod1" . to_string ( ) ) , level : 2 }
488
- ] ;
489
+ let dirs = [ LogDirective {
490
+ name : Some ( "crate2" . to_string ( ) ) ,
491
+ level : 3 ,
492
+ } ,
493
+ LogDirective {
494
+ name : Some ( "crate2::mod" . to_string ( ) ) ,
495
+ level : 4 ,
496
+ } ,
497
+ LogDirective {
498
+ name : Some ( "crate1::mod1" . to_string ( ) ) ,
499
+ level : 2 ,
500
+ } ] ;
489
501
assert ! ( enabled( 4 , "crate2::mod1" , dirs. iter( ) ) ) ;
490
502
assert ! ( !enabled( 4 , "crate2" , dirs. iter( ) ) ) ;
491
503
}
492
504
493
505
#[ test]
494
506
fn match_default ( ) {
495
- let dirs = [
496
- LogDirective { name : None , level : 3 } ,
497
- LogDirective { name : Some ( "crate1::mod1" . to_string ( ) ) , level : 2 }
498
- ] ;
507
+ let dirs = [ LogDirective {
508
+ name : None ,
509
+ level : 3 ,
510
+ } ,
511
+ LogDirective {
512
+ name : Some ( "crate1::mod1" . to_string ( ) ) ,
513
+ level : 2 ,
514
+ } ] ;
499
515
assert ! ( enabled( 2 , "crate1::mod1" , dirs. iter( ) ) ) ;
500
516
assert ! ( enabled( 3 , "crate2::mod2" , dirs. iter( ) ) ) ;
501
517
}
502
518
503
519
#[ test]
504
520
fn zero_level ( ) {
505
- let dirs = [
506
- LogDirective { name : None , level : 3 } ,
507
- LogDirective { name : Some ( "crate1::mod1" . to_string ( ) ) , level : 0 }
508
- ] ;
521
+ let dirs = [ LogDirective {
522
+ name : None ,
523
+ level : 3 ,
524
+ } ,
525
+ LogDirective {
526
+ name : Some ( "crate1::mod1" . to_string ( ) ) ,
527
+ level : 0 ,
528
+ } ] ;
509
529
assert ! ( !enabled( 1 , "crate1::mod1" , dirs. iter( ) ) ) ;
510
530
assert ! ( enabled( 3 , "crate2::mod2" , dirs. iter( ) ) ) ;
511
531
}
0 commit comments