From 039d5185e4593e4bace68f5d8b5f58a745f020aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Fatay=20Yi=C4=9Fit=20=C5=9Eahin?= Date: Wed, 18 Mar 2026 10:28:49 +0100 Subject: [PATCH 1/2] refactor(logging): avoid magic numbers --- src/logging.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/logging.rs b/src/logging.rs index 2887322666..45ba47efc7 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -6,6 +6,9 @@ use log::{Level, LevelFilter, Metadata, Record}; pub static KERNEL_LOGGER: KernelLogger = KernelLogger::new(); +const TIME_SEC_WIDTH: usize = 5; +const TIME_SUBSEC_WIDTH: usize = 6; + /// Data structure to filter kernel messages pub struct KernelLogger { time: AtomicBool, @@ -47,7 +50,7 @@ impl log::Log for KernelLogger { time = Microseconds(crate::processor::get_timer_ticks()); format_args!("[{time}]") } else { - format_args!("[ ]") + format_args!("[{:1$}]", "", TIME_SEC_WIDTH + 1 + TIME_SUBSEC_WIDTH) }; let core_id = crate::arch::core_local::core_id(); let level = ColorLevel(record.level()); @@ -73,7 +76,10 @@ impl fmt::Display for Microseconds { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let seconds = self.0 / 1_000_000; let microseconds = self.0 % 1_000_000; - write!(f, "{seconds:5}.{microseconds:06}") + write!( + f, + "{seconds:TIME_SEC_WIDTH$}.{microseconds:0TIME_SUBSEC_WIDTH$}" + ) } } From 81593585fed2fe22647e71633811283a9b1e445d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Fatay=20Yi=C4=9Fit=20=C5=9Eahin?= Date: Wed, 13 May 2026 17:05:32 +0200 Subject: [PATCH 2/2] refactor(logging): use stdlib Duration to represent time --- src/logging.rs | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/logging.rs b/src/logging.rs index 45ba47efc7..0d14d7de8d 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,14 +1,12 @@ use core::fmt; use core::sync::atomic::{AtomicBool, Ordering}; +use core::time::Duration; use anstyle::AnsiColor; use log::{Level, LevelFilter, Metadata, Record}; pub static KERNEL_LOGGER: KernelLogger = KernelLogger::new(); -const TIME_SEC_WIDTH: usize = 5; -const TIME_SUBSEC_WIDTH: usize = 6; - /// Data structure to filter kernel messages pub struct KernelLogger { time: AtomicBool, @@ -44,14 +42,10 @@ impl log::Log for KernelLogger { return; } - // FIXME: Use `super let` once stable - let time; - let format_time = if self.time() { - time = Microseconds(crate::processor::get_timer_ticks()); - format_args!("[{time}]") - } else { - format_args!("[{:1$}]", "", TIME_SEC_WIDTH + 1 + TIME_SUBSEC_WIDTH) - }; + let time = self + .time() + .then(|| Duration::from_micros(crate::processor::get_timer_ticks())); + let format_time = LogTime(time); let core_id = crate::arch::core_local::core_id(); let level = ColorLevel(record.level()); @@ -66,20 +60,27 @@ impl log::Log for KernelLogger { let format_target = format_args!(" {target:<10}"); let args = record.args(); - println!("{format_time}[{core_id}][{level}{format_target}] {args}"); + println!("[{format_time}][{core_id}][{level}{format_target}] {args}"); } } -struct Microseconds(u64); +struct LogTime(Option); -impl fmt::Display for Microseconds { +impl fmt::Display for LogTime { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let seconds = self.0 / 1_000_000; - let microseconds = self.0 % 1_000_000; - write!( - f, - "{seconds:TIME_SEC_WIDTH$}.{microseconds:0TIME_SUBSEC_WIDTH$}" - ) + const TIME_SEC_WIDTH: usize = 5; + const TIME_SUBSEC_WIDTH: usize = 6; + + if let Some(time) = self.0 { + let seconds = time.as_secs(); + let microseconds = time.subsec_micros(); + write!( + f, + "{seconds:TIME_SEC_WIDTH$}.{microseconds:0TIME_SUBSEC_WIDTH$}" + ) + } else { + write!(f, "{:1$}", "", TIME_SEC_WIDTH + 1 + TIME_SUBSEC_WIDTH) + } } }