Skip to content

Instantly share code, notes, and snippets.

@habnabit
Forked from anonymous/playground.rs
Last active September 28, 2017 02:57
Show Gist options
  • Save habnabit/b16aadf203383cb66af9f359b735652e to your computer and use it in GitHub Desktop.
Save habnabit/b16aadf203383cb66af9f359b735652e to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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