Skip to content

Commit 6e884c7

Browse files
committed
feat(logging): print time
1 parent e112dc5 commit 6e884c7

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/arch/aarch64/kernel/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pub fn boot_processor_init() {
151151
env::init();
152152
interrupts::init();
153153
processor::detect_frequency();
154+
crate::logging::KERNEL_LOGGER.set_time(true);
154155
processor::print_information();
155156
systemtime::init();
156157
#[cfg(feature = "pci")]

src/arch/riscv64/kernel/start.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ unsafe extern "C" fn pre_init(hart_id: usize, boot_info: Option<&'static RawBoot
5050
CURRENT_BOOT_ID.store(hart_id as u32, Ordering::Relaxed);
5151

5252
if CPU_ONLINE.load(Ordering::Acquire) == 0 {
53+
crate::logging::KERNEL_LOGGER.set_time(true);
54+
5355
env::set_boot_info(*boot_info.unwrap());
5456
let fdt = unsafe { Fdt::from_ptr(get_dtb_ptr()).expect("FDT is invalid") };
5557
// Init HART_MASK

src/arch/x86_64/kernel/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ pub fn boot_processor_init() {
154154
crate::mm::print_information();
155155
CoreLocal::get().add_irq_counter();
156156
env::init();
157+
crate::logging::KERNEL_LOGGER.set_time(true);
157158
gdt::add_current_core();
158159
interrupts::load_idt();
159160
pic::init();

src/logging.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
use core::fmt;
2+
use core::sync::atomic::{AtomicBool, Ordering};
23

34
use anstyle::AnsiColor;
45
use log::{Level, LevelFilter, Metadata, Record};
56

6-
pub static KERNEL_LOGGER: KernelLogger = KernelLogger;
7+
pub static KERNEL_LOGGER: KernelLogger = KernelLogger::new();
78

89
/// Data structure to filter kernel messages
9-
pub struct KernelLogger;
10+
pub struct KernelLogger {
11+
time: AtomicBool,
12+
}
13+
14+
impl KernelLogger {
15+
pub const fn new() -> Self {
16+
Self {
17+
time: AtomicBool::new(false),
18+
}
19+
}
20+
21+
pub fn time(&self) -> bool {
22+
self.time.load(Ordering::Relaxed)
23+
}
24+
25+
pub fn set_time(&self, time: bool) {
26+
self.time.store(time, Ordering::Relaxed);
27+
}
28+
}
1029

1130
impl log::Log for KernelLogger {
1231
fn enabled(&self, _: &Metadata<'_>) -> bool {
@@ -22,10 +41,28 @@ impl log::Log for KernelLogger {
2241
return;
2342
}
2443

44+
// FIXME: Use `super let` once stable
45+
let time;
46+
let format_time = if self.time() {
47+
time = Microseconds(crate::processor::get_timer_ticks());
48+
format_args!("[{time}]")
49+
} else {
50+
format_args!("[ ]")
51+
};
2552
let core_id = crate::arch::core_local::core_id();
2653
let level = ColorLevel(record.level());
2754
let args = record.args();
28-
println!("[{core_id}][{level}] {args}");
55+
println!("{format_time}[{core_id}][{level}] {args}");
56+
}
57+
}
58+
59+
struct Microseconds(u64);
60+
61+
impl fmt::Display for Microseconds {
62+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63+
let seconds = self.0 / 1_000_000;
64+
let microseconds = self.0 % 1_000_000;
65+
write!(f, "{seconds:5}.{microseconds:06}")
2966
}
3067
}
3168

0 commit comments

Comments
 (0)