2020-04-21 05:40:01 +08:00
|
|
|
//! A logger for the `log` crate
|
|
|
|
|
2020-05-01 07:33:00 +08:00
|
|
|
use crate::{println, stdio, timer::GlobalTimer};
|
2020-04-21 05:40:01 +08:00
|
|
|
|
|
|
|
pub static LOGGER: Logger = Logger;
|
|
|
|
|
|
|
|
pub struct Logger;
|
|
|
|
|
|
|
|
pub fn init() -> Result<(), log::SetLoggerError> {
|
|
|
|
log::set_logger(&LOGGER)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl log::Log for Logger {
|
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
|
|
|
metadata.level() <= log::Level::Trace
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &log::Record) {
|
2020-04-24 12:06:03 +08:00
|
|
|
if self.enabled(record.metadata()) {
|
2020-04-25 09:01:19 +08:00
|
|
|
let timestamp = unsafe {
|
|
|
|
GlobalTimer::get()
|
2020-07-23 05:41:15 +08:00
|
|
|
}.get_us().0;
|
2020-04-24 12:06:03 +08:00
|
|
|
let seconds = timestamp / 1_000_000;
|
|
|
|
let micros = timestamp % 1_000_000;
|
|
|
|
|
|
|
|
println!("[{:6}.{:06}s] {:>5}({}): {}",
|
|
|
|
seconds, micros, record.level(), record.target(), record.args());
|
2020-04-21 05:40:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {
|
|
|
|
let uart = stdio::get_uart();
|
2020-05-03 05:32:01 +08:00
|
|
|
while !uart.tx_idle() {}
|
2020-04-21 05:40:01 +08:00
|
|
|
}
|
|
|
|
}
|