@@ -78,7 +78,7 @@ struct UefiOSImpl {
7878 vga : ScopedProtocol < Output > ,
7979 serial : ScopedProtocol < Serial > ,
8080 net : Option < NetworkInterface > ,
81- messages : VecDeque < ( String , MessageKind ) > ,
81+ messages : VecDeque < ( f64 , log :: Level , String , String ) > ,
8282 ui_buf : Vec < ( String , Color , Color ) > ,
8383 ui_pos : usize ,
8484 ui_drawer : Option < Box < dyn Fn ( UefiOS ) + ' static > > ,
@@ -149,9 +149,6 @@ pub struct UefiOS {
149149 cant_build : ( ) ,
150150}
151151
152- impl !Send for UefiOS { }
153- impl !Sync for UefiOS { }
154-
155152unsafe extern "efiapi" fn exit_boot_services ( _e : Event , _ctx : Option < NonNull < c_void > > ) {
156153 panic ! ( "You must never exit boot services" ) ;
157154}
@@ -210,13 +207,16 @@ impl UefiOS {
210207
211208 let os = UefiOS { cant_build : ( ) } ;
212209
210+ log:: set_logger ( & UefiOS { cant_build : ( ) } ) . unwrap ( ) ;
211+ log:: set_max_level ( log:: LevelFilter :: Trace ) ;
212+
213213 let net = NetworkInterface :: new ( os) ;
214214 os. borrow_mut ( ) . net = Some ( net) ;
215215
216216 os. spawn ( "init" , async move {
217217 loop {
218218 let err = f ( os) . await . unwrap_err ( ) ;
219- os . append_message ( format ! ( "Error: {:?}" , err) , MessageKind :: Error ) ;
219+ log :: error !( "Error: {:?}" , err) ;
220220 }
221221 } ) ;
222222
@@ -226,10 +226,7 @@ impl UefiOS {
226226
227227 if let Err ( err) = err {
228228 if err. status ( ) != Status :: UNSUPPORTED {
229- os. append_message (
230- format ! ( "Error disabling watchdog: {:?}" , err) ,
231- MessageKind :: Error ,
232- ) ;
229+ log:: error!( "Error disabling watchdog: {:?}" , err) ;
233230 }
234231
235232 break ;
@@ -503,10 +500,17 @@ impl UefiOS {
503500 // TODO(veluca): find a better solution.
504501 let messages: Vec < _ > = os. messages . iter ( ) . cloned ( ) . collect ( ) ;
505502
506- for ( line, kind) in messages {
507- let fg_color = kind. color ( ) ;
508- os. write_with_color ( & line, fg_color, Color :: Black ) ;
509- os. write_with_color ( "\n " , Color :: Black , Color :: Black ) ;
503+ for ( time, level, target, msg) in messages {
504+ let fg_color = match level {
505+ log:: Level :: Trace => Color :: Cyan ,
506+ log:: Level :: Debug => Color :: Blue ,
507+ log:: Level :: Info => Color :: Green ,
508+ log:: Level :: Warn => Color :: Yellow ,
509+ log:: Level :: Error => Color :: Red ,
510+ } ;
511+ os. write_with_color ( & format ! ( "[{time:.1}s " ) , Color :: White , Color :: Black ) ;
512+ os. write_with_color ( & format ! ( "{level:5}" ) , fg_color, Color :: Black ) ;
513+ os. write_with_color ( & format ! ( " {target}] {msg}\n " ) , Color :: White , Color :: Black ) ;
510514 }
511515 os. write_with_color ( "\n " , Color :: Black , Color :: Black ) ;
512516 }
@@ -533,13 +537,24 @@ impl UefiOS {
533537 self . borrow_mut ( ) . ui_drawer = Some ( Box :: new ( f) ) ;
534538 }
535539
536- pub fn append_message ( & self , msg : String , kind : MessageKind ) {
540+ fn append_message ( & self , time : f64 , level : log :: Level , target : & str , msg : String ) {
537541 {
538542 let mut os = self . borrow_mut ( ) ;
539543
540- write ! ( os. serial, "{:5} {}\r \n " , kind. as_str( ) , msg) . unwrap ( ) ;
544+ let style = match level {
545+ log:: Level :: Trace => anstyle:: AnsiColor :: Cyan . on_default ( ) ,
546+ log:: Level :: Debug => anstyle:: AnsiColor :: Blue . on_default ( ) ,
547+ log:: Level :: Info => anstyle:: AnsiColor :: Green . on_default ( ) ,
548+ log:: Level :: Warn => anstyle:: AnsiColor :: Yellow . on_default ( ) ,
549+ log:: Level :: Error => anstyle:: AnsiColor :: Red . on_default ( ) . bold ( ) ,
550+ } ;
551+ write ! (
552+ os. serial,
553+ "[{time:.1}s {style}{level:5}{style:#} {target}] {msg}\r \n "
554+ )
555+ . unwrap ( ) ;
541556
542- os. messages . push_back ( ( msg , kind ) ) ;
557+ os. messages . push_back ( ( time , level , target . into ( ) , msg ) ) ;
543558 const MAX_MESSAGES : usize = 5 ;
544559 if os. messages . len ( ) > MAX_MESSAGES {
545560 os. messages . pop_front ( ) ;
@@ -567,30 +582,22 @@ impl UefiOS {
567582 }
568583}
569584
570- #[ derive( Clone , Copy ) ]
571- pub enum MessageKind {
572- Debug ,
573- Info ,
574- Warn ,
575- Error ,
576- }
585+ impl log:: Log for UefiOS {
586+ fn enabled ( & self , _metadata : & log:: Metadata ) -> bool {
587+ true
588+ }
577589
578- impl MessageKind {
579- fn color ( self ) -> Color {
580- match self {
581- MessageKind :: Debug => Color :: LightGray ,
582- MessageKind :: Info => Color :: White ,
583- MessageKind :: Warn => Color :: Yellow ,
584- MessageKind :: Error => Color :: Red ,
585- }
590+ fn log ( & self , record : & log :: Record ) {
591+ let now = self . timer ( ) . micros ( ) as f64 * 0.000_001 ;
592+ self . append_message (
593+ now ,
594+ record . level ( ) ,
595+ record . target ( ) ,
596+ format ! ( "{}" , record . args ( ) ) ,
597+ ) ;
586598 }
587599
588- fn as_str ( self ) -> & ' static str {
589- match self {
590- MessageKind :: Debug => "DEBUG" ,
591- MessageKind :: Info => "INFO" ,
592- MessageKind :: Warn => "WARN" ,
593- MessageKind :: Error => "ERROR" ,
594- }
600+ fn flush ( & self ) {
601+ // no-op
595602 }
596603}
0 commit comments