-
-
Save habnabit/b16aadf203383cb66af9f359b735652e to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use linux; | |
use log; | |
use core::fmt::Write; | |
use core::fmt; | |
use log::{LogRecord, LogLevel, LogMetadata}; | |
#[derive(Default)] | |
struct DMesgLogger; | |
impl fmt::Write for DMesgLogger { | |
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { | |
unsafe { | |
linux::printk(cstring!("%s\n"), s.as_ptr()); | |
} | |
Ok(()) | |
} | |
} | |
impl log::Log for DMesgLogger { | |
fn enabled(&self, metadata: &LogMetadata) -> bool { | |
metadata.level() <= LogLevel::Info | |
} | |
fn log(&self, record: &LogRecord) { | |
if self.enabled(record.metadata()) { | |
let _ = write!(&mut DMesgLogger, "{:?}\0", record); | |
} | |
} | |
} | |
static LOGGER: DMesgLogger = DMesgLogger; | |
pub fn init() -> Result<(), log::SetLoggerError> { | |
unsafe { | |
log::set_logger_raw(|max_log_level| { | |
max_log_level.set(log::LogLevelFilter::Info); | |
&LOGGER | |
}) | |
} | |
} | |
pub fn fini() { } | |
pub fn shutdown() -> Result<(), log::ShutdownLoggerError> { | |
log::shutdown_logger_raw().map(|_logger| {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment