11use core:: fmt;
22use core:: sync:: atomic:: { AtomicBool , Ordering } ;
3+ use core:: time:: Duration ;
34
45use anstyle:: AnsiColor ;
56use log:: { Level , LevelFilter , Metadata , Record } ;
67
78pub static KERNEL_LOGGER : KernelLogger = KernelLogger :: new ( ) ;
89
9- const TIME_SEC_WIDTH : usize = 5 ;
10- const TIME_SUBSEC_WIDTH : usize = 6 ;
11-
1210/// Data structure to filter kernel messages
1311pub struct KernelLogger {
1412 time : AtomicBool ,
@@ -44,14 +42,10 @@ impl log::Log for KernelLogger {
4442 return ;
4543 }
4644
47- // FIXME: Use `super let` once stable
48- let time;
49- let format_time = if self . time ( ) {
50- time = Microseconds ( crate :: processor:: get_timer_ticks ( ) ) ;
51- format_args ! ( "[{time}]" )
52- } else {
53- format_args ! ( "[{:1$}]" , "" , TIME_SEC_WIDTH + 1 + TIME_SUBSEC_WIDTH )
54- } ;
45+ let format_time = LogTime (
46+ self . time ( )
47+ . then ( || Duration :: from_micros ( crate :: processor:: get_timer_ticks ( ) ) ) ,
48+ ) ;
5549 let core_id = crate :: arch:: core_local:: core_id ( ) ;
5650 let level = ColorLevel ( record. level ( ) ) ;
5751
@@ -66,20 +60,27 @@ impl log::Log for KernelLogger {
6660 let format_target = format_args ! ( " {target:<10}" ) ;
6761
6862 let args = record. args ( ) ;
69- println ! ( "{format_time}[{core_id}][{level}{format_target}] {args}" ) ;
63+ println ! ( "[ {format_time}] [{core_id}][{level}{format_target}] {args}" ) ;
7064 }
7165}
7266
73- struct Microseconds ( u64 ) ;
67+ struct LogTime ( Option < Duration > ) ;
7468
75- impl fmt:: Display for Microseconds {
69+ impl fmt:: Display for LogTime {
7670 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
77- let seconds = self . 0 / 1_000_000 ;
78- let microseconds = self . 0 % 1_000_000 ;
79- write ! (
80- f,
81- "{seconds:TIME_SEC_WIDTH$}.{microseconds:0TIME_SUBSEC_WIDTH$}"
82- )
71+ const TIME_SEC_WIDTH : usize = 5 ;
72+ const TIME_SUBSEC_WIDTH : usize = 6 ;
73+
74+ if let Some ( time) = self . 0 {
75+ let seconds = time. as_secs ( ) ;
76+ let microseconds = time. subsec_micros ( ) ;
77+ write ! (
78+ f,
79+ "{seconds:TIME_SEC_WIDTH$}.{microseconds:0TIME_SUBSEC_WIDTH$}"
80+ )
81+ } else {
82+ write ! ( f, "{:1$}" , "" , TIME_SEC_WIDTH + 1 + TIME_SUBSEC_WIDTH )
83+ }
8384 }
8485}
8586
0 commit comments