mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-05-29 05:40:47 +00:00
f8df02dae7
I read through every file and applied a couple of rustfmt suggestions. The brace placement and alignment of items on similar lines has been made consistent, even if neither are rustfmt's default style (a file has been put in place to enforce this). Other changes are: • Alphabetical imports and modules • Comma placement at the end of match blocks • Use newlines and indentation judiciously • Spaces around associated types • Spaces after negations (it makes it more clear imho) • Comment formatting • Use early-returns and Optional `?` where appropriate
67 lines
1.6 KiB
Rust
67 lines
1.6 KiB
Rust
//! Debug error logging.
|
||
|
||
use std::ffi::OsStr;
|
||
|
||
use ansi_term::{Colour, ANSIString};
|
||
|
||
|
||
/// Sets the internal logger, changing the log level based on the value of an
|
||
/// environment variable.
|
||
pub fn configure<T: AsRef<OsStr>>(ev: Option<T>) {
|
||
let ev = match ev {
|
||
Some(v) => v,
|
||
None => return,
|
||
};
|
||
|
||
let env_var = ev.as_ref();
|
||
if env_var.is_empty() {
|
||
return;
|
||
}
|
||
|
||
if env_var == "trace" {
|
||
log::set_max_level(log::LevelFilter::Trace);
|
||
}
|
||
else {
|
||
log::set_max_level(log::LevelFilter::Debug);
|
||
}
|
||
|
||
let result = log::set_logger(GLOBAL_LOGGER);
|
||
if let Err(e) = result {
|
||
eprintln!("Failed to initialise logger: {}", e);
|
||
}
|
||
}
|
||
|
||
|
||
#[derive(Debug)]
|
||
struct Logger;
|
||
|
||
const GLOBAL_LOGGER: &Logger = &Logger;
|
||
|
||
impl log::Log for Logger {
|
||
fn enabled(&self, _: &log::Metadata<'_>) -> bool {
|
||
true // no need to filter after using ‘set_max_level’.
|
||
}
|
||
|
||
fn log(&self, record: &log::Record<'_>) {
|
||
let open = Colour::Fixed(243).paint("[");
|
||
let level = level(record.level());
|
||
let close = Colour::Fixed(243).paint("]");
|
||
|
||
eprintln!("{}{} {}{} {}", open, level, record.target(), close, record.args());
|
||
}
|
||
|
||
fn flush(&self) {
|
||
// no need to flush with ‘eprintln!’.
|
||
}
|
||
}
|
||
|
||
fn level(level: log::Level) -> ANSIString<'static> {
|
||
match level {
|
||
log::Level::Error => Colour::Red.paint("ERROR"),
|
||
log::Level::Warn => Colour::Yellow.paint("WARN"),
|
||
log::Level::Info => Colour::Cyan.paint("INFO"),
|
||
log::Level::Debug => Colour::Blue.paint("DEBUG"),
|
||
log::Level::Trace => Colour::Fixed(245).paint("TRACE"),
|
||
}
|
||
}
|