84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
mod logging_config {
|
|
include!(concat!(env!("OUT_DIR"), "/log_filter.rs"));
|
|
}
|
|
|
|
struct EnvLogger;
|
|
|
|
impl log::Log for EnvLogger {
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
|
let level = metadata.level();
|
|
let target = metadata.target();
|
|
logging_config::is_enabled(level, target)
|
|
}
|
|
|
|
fn log(&self, record: &log::Record) {
|
|
if !self.enabled(record.metadata()) {
|
|
return;
|
|
}
|
|
|
|
// print with color? default to true if env var unset or set but empty.
|
|
let with_color = 1
|
|
== option_env!("ESP_LOG_COLOR")
|
|
.unwrap_or("1")
|
|
.parse()
|
|
.unwrap_or(1);
|
|
let (color, reset) = if with_color {
|
|
const RESET: &str = "\u{001B}[0m";
|
|
const RED: &str = "\u{001B}[31m";
|
|
const GREEN: &str = "\u{001B}[32m";
|
|
const YELLOW: &str = "\u{001B}[33m";
|
|
const BLUE: &str = "\u{001B}[34m";
|
|
const CYAN: &str = "\u{001B}[35m";
|
|
|
|
let color = match record.level() {
|
|
log::Level::Error => RED,
|
|
log::Level::Warn => YELLOW,
|
|
log::Level::Info => GREEN,
|
|
log::Level::Debug => BLUE,
|
|
log::Level::Trace => CYAN,
|
|
};
|
|
let reset = RESET;
|
|
(color, reset)
|
|
} else {
|
|
("", "")
|
|
};
|
|
|
|
let [now_s, now_ms] = {
|
|
let now = esp_hal::time::Instant::now()
|
|
.duration_since_epoch()
|
|
.as_millis();
|
|
|
|
[now / 1000, now % 1000]
|
|
};
|
|
|
|
let level = match record.level() {
|
|
log::Level::Error => "E",
|
|
log::Level::Warn => "W",
|
|
log::Level::Info => "I",
|
|
log::Level::Debug => "D",
|
|
log::Level::Trace => "T",
|
|
};
|
|
let args = record.args();
|
|
let line = record.line().unwrap_or(0);
|
|
let target = record.target();
|
|
let module = record.module_path().unwrap_or("???");
|
|
|
|
let (module, divider) = if module == target {
|
|
("", "")
|
|
} else {
|
|
(module, " ")
|
|
};
|
|
|
|
esp_println::println!(
|
|
"{color}{now_s:>4}.{now_ms:03}s {level} {target}{divider}{module}:{line} - {args}{reset}"
|
|
);
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
}
|
|
|
|
pub fn init() {
|
|
log::set_logger(&EnvLogger).expect("Failed to init logger.");
|
|
log::set_max_level(logging_config::FILTER_MAX);
|
|
}
|