kubi/kubi-logging/src/lib.rs

64 lines
1.7 KiB
Rust
Raw Normal View History

//! Custom env_logger options and styling
2023-02-10 13:44:34 -06:00
/// Custom env_logger options and styling
2023-02-10 13:44:34 -06:00
#[inline]
#[cfg(not(target_os = "android"))]
2023-01-14 18:41:29 -06:00
pub fn init() {
use log::Level;
use std::io::Write;
use env_logger::{fmt::Color, Builder, Env};
let env = Env::default()
.filter_or("RUST_LOG", "trace,gilrs=warn,rusty_xinput=warn");
2023-01-15 11:11:49 -06:00
Builder::from_env(env)
.format(|buf, record| {
let mut level_style = buf.style();
level_style.set_color(match record.level() {
Level::Error => Color::Red,
Level::Warn => Color::Yellow,
_ => Color::Blue
}).set_bold(true);
2023-01-14 18:41:29 -06:00
2023-02-12 14:31:21 -06:00
let mut bold_style = buf.style();
bold_style.set_bold(true);
2023-01-15 11:11:49 -06:00
let mut location_style = buf.style();
location_style.set_bold(true);
location_style.set_dimmed(true);
2023-01-14 18:41:29 -06:00
2023-01-15 11:11:49 -06:00
let mut location_line_style = buf.style();
location_line_style.set_dimmed(true);
2023-02-12 14:31:21 -06:00
let text = format!("{}", record.args());
2023-01-15 11:11:49 -06:00
writeln!(
buf,
2023-02-12 14:31:21 -06:00
"{} {:<50}\t{}{}{}{}",
2023-01-15 11:11:49 -06:00
level_style.value(match record.level() {
Level::Error => "[e]",
Level::Warn => "[w]",
Level::Info => "[i]",
Level::Debug => "[d]",
Level::Trace => "[t]",
}),
2023-02-12 14:31:21 -06:00
text,
bold_style.value((text.len() > 50).then_some("\n ╰─ ").unwrap_or_default()),
2023-01-15 11:11:49 -06:00
location_style.value(record.target()),
location_line_style.value(" :"),
location_line_style.value(record.line().unwrap_or(0))
)
})
.init();
2023-01-14 18:41:29 -06:00
}
/// Custom env_logger options and styling
#[inline]
#[cfg(target_os = "android")]
pub fn init() {
use log::LevelFilter;
use android_logger::Config;
android_logger::init_once(
Config::default().with_max_level(LevelFilter::Trace),
);
}