Format logger output
This commit is contained in:
		
							parent
							
								
									cf56bce57c
								
							
						
					
					
						commit
						608eba369c
					
				|  | @ -1097,6 +1097,7 @@ dependencies = [ | |||
|  "num-derive", | ||||
|  "num-traits", | ||||
|  "ostd-macros", | ||||
|  "owo-colors", | ||||
|  "pod", | ||||
|  "rsdp", | ||||
|  "spin 0.9.8", | ||||
|  |  | |||
|  | @ -36,8 +36,15 @@ tdx-guest = { version = "0.1.0", optional = true } | |||
| trapframe = { git = "https://github.com/asterinas/trapframe-rs", rev = "4739428" } | ||||
| # This version is in the upstream trunk branch which fixes a `r#try` usage. | ||||
| # We could switch back to "crates.io" when they publish a new version. | ||||
| unwinding = { git = "https://github.com/nbdd0121/unwinding.git", rev = "d7cd46e", default-features = false, features = ["fde-gnu-eh-frame-hdr", "hide-trace", "panic", "personality", "unwinder"] } | ||||
| unwinding = { git = "https://github.com/nbdd0121/unwinding.git", rev = "d7cd46e", default-features = false, features = [ | ||||
|     "fde-gnu-eh-frame-hdr", | ||||
|     "hide-trace", | ||||
|     "panic", | ||||
|     "personality", | ||||
|     "unwinder", | ||||
| ] } | ||||
| volatile = { version = "0.4.5", features = ["unstable"] } | ||||
| owo-colors = { version = "3", optional = true } | ||||
| 
 | ||||
| [target.x86_64-unknown-none.dependencies] | ||||
| x86_64 = "0.14.2" | ||||
|  | @ -46,7 +53,13 @@ acpi = "4.1.1" | |||
| aml = "0.16.3" | ||||
| multiboot2 = "0.20.2" | ||||
| rsdp = "2.0.0" | ||||
| iced-x86 = { version = "1.21.0", default-features = false, features = [ "no_std", "decoder", "gas" ], optional = true } | ||||
| iced-x86 = { version = "1.21.0", default-features = false, features = [ | ||||
|     "no_std", | ||||
|     "decoder", | ||||
|     "gas", | ||||
| ], optional = true } | ||||
| 
 | ||||
| [features] | ||||
| default = ["log_color"] | ||||
| log_color = ["dep:owo-colors"] | ||||
| intel_tdx = ["dep:tdx-guest", "dep:iced-x86"] | ||||
|  |  | |||
|  | @ -2,9 +2,12 @@ | |||
| 
 | ||||
| //! Logging support.
 | ||||
| 
 | ||||
| use alloc::format; | ||||
| 
 | ||||
| use log::{LevelFilter, Metadata, Record}; | ||||
| 
 | ||||
| use crate::{ | ||||
|     arch::timer::Jiffies, | ||||
|     boot::{kcmdline::ModuleArg, kernel_cmdline}, | ||||
|     early_println, | ||||
| }; | ||||
|  | @ -19,9 +22,33 @@ impl log::Log for Logger { | |||
|     } | ||||
| 
 | ||||
|     fn log(&self, record: &Record) { | ||||
|         if self.enabled(record.metadata()) { | ||||
|             early_println!("[{}]: {}", record.level(), record.args()); | ||||
|         if !self.enabled(record.metadata()) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         let timestamp = format!("[{:>10?}]", Jiffies::elapsed().as_duration().as_secs_f64()); | ||||
|         let level = format!("{:<5}", record.level()); | ||||
|         let record_str = format!("{}", record.args()); | ||||
| 
 | ||||
|         #[cfg(feature = "log_color")] | ||||
|         let (timestamp, level, record_str) = { | ||||
|             use alloc::string::ToString; | ||||
| 
 | ||||
|             use owo_colors::OwoColorize; | ||||
| 
 | ||||
|             let timestamp = timestamp.green(); | ||||
|             let level = match record.level() { | ||||
|                 log::Level::Error => level.red().to_string(), | ||||
|                 log::Level::Warn => level.bright_yellow().to_string(), | ||||
|                 log::Level::Info => level.blue().to_string(), | ||||
|                 log::Level::Debug => level.bright_green().to_string(), | ||||
|                 log::Level::Trace => level.bright_black().to_string(), | ||||
|             }; | ||||
|             let record_str = record_str.default_color(); | ||||
|             (timestamp, level, record_str) | ||||
|         }; | ||||
| 
 | ||||
|         early_println!("{} {}: {}", timestamp, level, record_str); | ||||
|     } | ||||
| 
 | ||||
|     fn flush(&self) {} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue