kata-logger: pass level through LoggerInterface and filter in DebugConsole

Components can reduce the number of rpc's if they set_max_level but by
default pass everything and let the DebugConsole decide what is logged.
With this the shell loglevel command now controls logging from all
components.

Change-Id: I72b77bbf02882ffdba0aaf0b9b88126bfd2e62a1
GitOrigin-RevId: d451e72cf67e41d6ee25ea2995098b5009cf8852
This commit is contained in:
Sam Leffler 2021-08-17 11:16:33 -07:00
parent 4043c8b5b7
commit 8ace3eb6f2
6 changed files with 24 additions and 9 deletions

View File

@ -15,7 +15,7 @@ impl log::Log for KataLogger {
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
extern "C" {
fn logger_log(msg: *const cstr_core::c_char);
fn logger_log(level: u8, msg: *const cstr_core::c_char);
}
use bare_io::{Cursor, Write};
let mut buf = [0 as u8; MAX_MSG_LEN];
@ -55,6 +55,7 @@ impl log::Log for KataLogger {
// NB: this releases the ref on buf held by the Cursor
let pos = cur.position() as usize;
logger_log(
record.level() as u8,
match CStr::from_bytes_with_nul(&buf[..pos]) {
Ok(cstr) => cstr,
Err(_) => embedded_nul_cstr(&mut buf, record),
@ -85,7 +86,7 @@ mod tests {
}
#[no_mangle]
pub extern "C" fn logger_log(msg: *const cstr_core::c_char) {
pub extern "C" fn logger_log(_level: u8, msg: *const cstr_core::c_char) {
unsafe {
// NB: this depends on msg pointing to the caller's array
MSGS.push(*(msg as *const [u8; MAX_MSG_LEN]));

View File

@ -117,7 +117,6 @@ fn echo_command(cmdline: &str, output: &mut dyn io::Write) -> Result<(), Command
}
// Set/display the max log level for the DebugConsole.
// TODO(sleffler): support setting the log level in other components
fn loglevel_command(
args: &mut dyn Iterator<Item = &str>,
output: &mut dyn io::Write,

View File

@ -9,3 +9,4 @@ edition = "2018"
cstr_core = { version = "0.2.3", default-features = false }
cty = "0.2.1"
kata-io = { path = "../kata-io" }
log = "0.4"

View File

@ -19,10 +19,23 @@ extern "C" {
// Console logging interface.
#[no_mangle]
pub extern "C" fn logger_log(msg: *const cstr_core::c_char) {
let output: &mut dyn io::Write = &mut self::Tx {};
unsafe {
let _ = writeln!(output, "{}", CStr::from_ptr(msg).to_str().unwrap());
pub extern "C" fn logger_log(level: u8, msg: *const cstr_core::c_char) {
use log::Level;
// TODO(sleffler): seems like this should be try_from?
let l = match level {
x if x == Level::Error as u8 => Level::Error,
x if x == Level::Warn as u8 => Level::Warn,
x if x == Level::Info as u8 => Level::Info,
x if x == Level::Debug as u8 => Level::Debug,
x if x == Level::Trace as u8 => Level::Trace,
_ => { return }, // TODO(sleffler): accept or not?
};
if l <= log::max_level() {
// TODO(sleffler): is the uart driver ok w/ multiple writers?
let output: &mut dyn io::Write = &mut self::Tx {};
unsafe {
let _ = writeln!(output, "{}", CStr::from_ptr(msg).to_str().unwrap());
}
}
}

View File

@ -15,7 +15,8 @@ use log::{info, trace};
pub extern "C" fn pre_init() {
static KATA_LOGGER: KataLogger = KataLogger;
log::set_logger(&KATA_LOGGER).unwrap();
log::set_max_level(log::LevelFilter::Debug);
// NB: set to max; the LoggerInterface will filter
log::set_max_level(log::LevelFilter::Trace);
// TODO(sleffler): temp until we integrate with seL4
static mut HEAP_MEMORY: [u8; 16 * 1024] = [0; 16 * 1024];

View File

@ -1,3 +1,3 @@
procedure LoggerInterface {
void log(in string msg);
void log(in u_char level, in string msg);
};