2020-04-21 05:40:01 +08:00
|
|
|
//! A logger for the `log` crate
|
|
|
|
|
2020-04-25 09:01:19 +08:00
|
|
|
use libboard_zynq::{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()
|
|
|
|
}.get_us();
|
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();
|
|
|
|
while !uart.tx_fifo_empty() {}
|
|
|
|
}
|
|
|
|
}
|