Created
September 28, 2017 02:52
-
-
Save anonymous/e88ff172025a3930d1b7dcdac5810de5 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; | |
// Linux guards the kernel ring buffer with a lock. | |
use core::cell::UnsafeCell; | |
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!(self, "{:?}\0", record); | |
} | |
} | |
} | |
pub fn init() -> Result<(), log::SetLoggerError> { | |
unsafe { | |
log::set_logger_raw(|max_log_level| { | |
static LOGGER: DMesgLogger = DMesgLogger(()); | |
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