From b3954e3533de1f09f870b49fc30f3dd3f1eaf8e1 Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 8 Dec 2022 00:07:02 +0100 Subject: [PATCH] Logging --- Cargo.lock | 2 +- kernel/Cargo.toml | 2 +- kernel/src/arch/x86_64/mod.rs | 8 +++----- kernel/src/debug.rs | 1 - kernel/src/lib.rs | 2 +- kernel/src/logger.rs | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 39 insertions(+), 9 deletions(-) delete mode 100644 kernel/src/debug.rs create mode 100644 kernel/src/logger.rs diff --git a/Cargo.lock b/Cargo.lock index c423b49..3a8d5b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -585,9 +585,9 @@ dependencies = [ "crossbeam-queue", "limine", "linked_list_allocator", + "log", "slab", "spin 0.9.4", - "tracing", "uart_16550", "versioning", "x86_64", diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 33baa99..2a111c8 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -8,7 +8,7 @@ linked_list_allocator = "0.9" slab = { version = "0.4", default-features = false } spin = "0.9" versioning = { git = "https://git.ablecorp.us/able/aos_userland" } -tracing = { version = "0.1", default-features = false, features = ["attributes"] } +log = "0.4" [dependencies.crossbeam-queue] version = "0.3" diff --git a/kernel/src/arch/x86_64/mod.rs b/kernel/src/arch/x86_64/mod.rs index 222af1d..60b567f 100644 --- a/kernel/src/arch/x86_64/mod.rs +++ b/kernel/src/arch/x86_64/mod.rs @@ -13,10 +13,9 @@ unsafe extern "C" fn _kernel_start() -> ! { static HDHM_REQ: LimineHhdmRequest = LimineHhdmRequest::new(0); static MMAP_REQ: LimineMmapRequest = LimineMmapRequest::new(0); - let _ = serial_fmt(format_args!( - "Initialising AbleOS Kernel {}\r\n", - crate::VERSION - )); + SERIAL_CONSOLE.lock().init(); + crate::logger::init().expect("failed to set logger"); + log::info!("Initialising AKern {}", crate::VERSION); memory::init_pt(VirtAddr::new( HDHM_REQ @@ -35,7 +34,6 @@ unsafe extern "C" fn _kernel_start() -> ! { ); allocator::init_alloc().expect("tried to initialise allocator"); - SERIAL_CONSOLE.lock().init(); crate::kmain::kmain() } diff --git a/kernel/src/debug.rs b/kernel/src/debug.rs deleted file mode 100644 index 981558f..0000000 --- a/kernel/src/debug.rs +++ /dev/null @@ -1 +0,0 @@ -// TODO: Tracing Subscriber serial print implementation diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index a96c0cc..6b17431 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -7,7 +7,7 @@ extern crate alloc; pub mod allocator; pub mod arch; -pub mod debug; +pub mod logger; pub mod kmain; pub mod task; diff --git a/kernel/src/logger.rs b/kernel/src/logger.rs new file mode 100644 index 0000000..cc894eb --- /dev/null +++ b/kernel/src/logger.rs @@ -0,0 +1,33 @@ +use log::{Level, SetLoggerError}; + +pub fn init() -> Result<(), SetLoggerError> { + log::set_logger(&crate::logger::Logger)?; + log::set_max_level(log::LevelFilter::Trace); + Ok(()) +} + +struct Logger; +impl log::Log for Logger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + true + } + + fn log(&self, record: &log::Record) { + let lvl = record.level(); + crate::arch::serial_fmt(format_args!( + "\x1b[38;5;{}m{lvl}\x1b[0m [{}]: {}\r\n", + match lvl { + Level::Error => "160", + Level::Warn => "172", + Level::Info => "47", + Level::Debug => "25", + Level::Trace => "103", + }, + record.module_path().unwrap_or_default(), + record.args(), + )) + .expect("write to serial console"); + } + + fn flush(&self) {} +}