mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-26 10:53:25 +00:00
Pass kernel command line option to logger
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
edc1412cc8
commit
cf56bce57c
@ -2,23 +2,20 @@
|
||||
|
||||
//! Logging support.
|
||||
|
||||
use log::{Level, Metadata, Record};
|
||||
use log::{LevelFilter, Metadata, Record};
|
||||
|
||||
use crate::early_println;
|
||||
use crate::{
|
||||
boot::{kcmdline::ModuleArg, kernel_cmdline},
|
||||
early_println,
|
||||
};
|
||||
|
||||
const LOGGER: Logger = Logger {};
|
||||
|
||||
/// The log level.
|
||||
///
|
||||
/// FIXME: The logs should be able to be read from files in the userspace,
|
||||
/// and the log level should be configurable.
|
||||
pub const INIT_LOG_LEVEL: Level = Level::Error;
|
||||
|
||||
struct Logger {}
|
||||
|
||||
impl log::Log for Logger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= INIT_LOG_LEVEL
|
||||
metadata.level() <= log::max_level()
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
@ -30,8 +27,33 @@ impl log::Log for Logger {
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
/// Initialize the logger. Users should avoid using the log macros before this function is called.
|
||||
pub(crate) fn init() {
|
||||
log::set_logger(&LOGGER)
|
||||
.map(|()| log::set_max_level(INIT_LOG_LEVEL.to_level_filter()))
|
||||
.unwrap();
|
||||
let level = get_log_level().unwrap_or(LevelFilter::Off);
|
||||
|
||||
log::set_max_level(level);
|
||||
log::set_logger(&LOGGER).unwrap();
|
||||
}
|
||||
|
||||
fn get_log_level() -> Option<LevelFilter> {
|
||||
let module_args = kernel_cmdline().get_module_args("ostd")?;
|
||||
|
||||
let arg = module_args.iter().find(|arg| match arg {
|
||||
ModuleArg::Arg(_) => false,
|
||||
ModuleArg::KeyVal(name, _) => name.as_bytes() == "log_level".as_bytes(),
|
||||
})?;
|
||||
|
||||
let ModuleArg::KeyVal(_, value) = arg else {
|
||||
unreachable!()
|
||||
};
|
||||
let value = value.as_c_str().to_str().unwrap_or("off");
|
||||
Some(match value {
|
||||
"error" => LevelFilter::Error,
|
||||
"warn" => LevelFilter::Warn,
|
||||
"info" => LevelFilter::Info,
|
||||
"debug" => LevelFilter::Debug,
|
||||
"trace" => LevelFilter::Trace,
|
||||
// Otherwise, OFF
|
||||
_ => LevelFilter::Off,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user