You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

50 lines
1.2 KiB

#![cfg(feature = "color-log")]
use crate::log::{Level, Log, Mode, Record};
use termcolor::{ColorSpec, WriteColor};
pub struct Color;
impl Color {
fn write_level(
&self, level: Level, out: &mut impl WriteColor,
) -> std::io::Result<()> {
use termcolor::Color;
let (color, intense) = match level {
| Level::Critical => (Color::Red, true),
| Level::Error => (Color::Red, false),
| Level::Warning => (Color::Yellow, true),
| Level::Notice => (Color::Yellow, false),
| Level::Info => (Color::Cyan, true),
| Level::Debug => (Color::Cyan, false),
#[cfg(debug_assertions)]
| Level::Trace => (Color::Magenta, true),
};
let mut spec = ColorSpec::new();
spec.set_fg(Some(color)).set_intense(intense);
out.write_all(b"[")?;
out.set_color(&spec)?;
write!(out, "{:^8}", level)?;
out.reset()?;
out.write_all(b"]")?;
Ok(())
}
}
impl Mode for Color {
fn log(&self, args: std::fmt::Arguments, record: Record, log: &Log) {
let color = termcolor::ColorChoice::Auto;
let mut out = if record.level <= Level::Warning {
termcolor::StandardStream::stderr(color)
} else {
termcolor::StandardStream::stdout(color)
};
let _ = self.write_level(record.level, &mut out);
let _ = log.write_into(args, record, &mut out);
}
}
// EOF