From 608eba369c87a37f8ce9bc4ebc10b1d30d7ddea5 Mon Sep 17 00:00:00 2001 From: Yuke Peng Date: Wed, 3 Jul 2024 16:32:35 +0800 Subject: [PATCH] Format logger output --- Cargo.lock | 1 + ostd/Cargo.toml | 17 +++++++++++++++-- ostd/src/logger.rs | 31 +++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f41e9f121..3060bf5d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1097,6 +1097,7 @@ dependencies = [ "num-derive", "num-traits", "ostd-macros", + "owo-colors", "pod", "rsdp", "spin 0.9.8", diff --git a/ostd/Cargo.toml b/ostd/Cargo.toml index 6ff4bdca1..b57eff1cc 100644 --- a/ostd/Cargo.toml +++ b/ostd/Cargo.toml @@ -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"] diff --git a/ostd/src/logger.rs b/ostd/src/logger.rs index b37a1ae7c..edf9403ec 100644 --- a/ostd/src/logger.rs +++ b/ostd/src/logger.rs @@ -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) {}