Add a way to customize how logging is done.
Each architecture may have a different method to enable logging. Now that can be customized with a function passed to the init_logger function. Also, provide a minimal x86_64 implementation. This is the first commit where you can see logging coming from the log crate. Signed-off-by: Wren Turkal <wt@penguintechs.org>
This commit is contained in:
@@ -84,7 +84,7 @@ pub unsafe extern fn kstart(args_ptr: *const KernelArgs) -> ! {
|
||||
KERNEL_SIZE.store(kernel_size, Ordering::SeqCst);
|
||||
|
||||
// Initialize logger
|
||||
log::init_logger();
|
||||
log::init_logger(|r| println!("{}:{} -- {}", r.target(), r.level(), r.args()));
|
||||
|
||||
info!("Redox OS starting...");
|
||||
println!("Kernel: {:X}:{:X}", kernel_base, kernel_base + kernel_size);
|
||||
|
||||
26
src/log.rs
26
src/log.rs
@@ -1,4 +1,5 @@
|
||||
use alloc::collections::VecDeque;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use spin::Mutex;
|
||||
|
||||
pub static LOG: Mutex<Option<Log>> = Mutex::new(None);
|
||||
@@ -36,6 +37,7 @@ impl Log {
|
||||
|
||||
struct RedoxLogger {
|
||||
log_func: fn(&log::Record),
|
||||
pub initialized: AtomicBool,
|
||||
}
|
||||
|
||||
impl ::log::Log for RedoxLogger {
|
||||
@@ -48,14 +50,26 @@ impl ::log::Log for RedoxLogger {
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
pub fn init_logger() {
|
||||
::log::set_max_level(::log::LevelFilter::Info);
|
||||
match ::log::set_logger(&LOGGER) {
|
||||
Ok(_) => ::log::info!("Logger initialized."),
|
||||
Err(e) => println!("Logger setup failed! error: {}", e),
|
||||
pub fn init_logger(func: fn(&log::Record)) {
|
||||
unsafe {
|
||||
match LOGGER.initialized.load(Ordering::SeqCst) {
|
||||
false => {
|
||||
::log::set_max_level(::log::LevelFilter::Info);
|
||||
LOGGER.log_func = func;
|
||||
match ::log::set_logger(&LOGGER) {
|
||||
Ok(_) => ::log::info!("Logger initialized."),
|
||||
Err(e) => println!("Logger setup failed! error: {}", e),
|
||||
}
|
||||
LOGGER.initialized.store(true, Ordering::SeqCst);
|
||||
},
|
||||
true => ::log::info!("Tried to reinitialize the logger, which is not possible. Ignoring."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static LOGGER: RedoxLogger = RedoxLogger { log_func: |_| {} };
|
||||
static mut LOGGER: RedoxLogger = RedoxLogger {
|
||||
log_func: |_| {},
|
||||
initialized: AtomicBool::new(false),
|
||||
};
|
||||
|
||||
pub use log::{debug, error, info, set_max_level, warn};
|
||||
|
||||
Reference in New Issue
Block a user