Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use core::sync::atomic::{AtomicBool, Ordering};
use core::time::Duration;

use anstyle::AnsiColor;
use log::{Level, LevelFilter, Metadata, Record};
Expand Down Expand Up @@ -41,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!("[ ]")
};
let format_time = LogTime(
self.time()
.then(|| Duration::from_micros(crate::processor::get_timer_ticks())),
);
let core_id = crate::arch::core_local::core_id();
let level = ColorLevel(record.level());

Expand All @@ -63,17 +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<Duration>);

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:5}.{microseconds:06}")
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)
}
}
}

Expand Down
Loading