Browse Source

simplify log module

master
Alison Watson 2 years ago
parent
commit
4b3965b94b
  1. 22
      source/framework/log/color.rs
  2. 40
      source/framework/log/level.rs
  3. 2
      source/framework/log/standard.rs

22
source/framework/log/color.rs

@ -1,7 +1,7 @@
#![cfg(feature = "color-log")]
use crate::log::{Level, Log, Mode, Record};
use termcolor::WriteColor;
use termcolor::{ColorSpec, WriteColor};
pub struct Color;
@ -11,9 +11,25 @@ impl Color {
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(&level.color())?;
write!(out, "{:^8}", level.name())?;
out.set_color(&spec)?;
write!(out, "{:^8}", level)?;
out.reset()?;
out.write_all(b"]")?;
Ok(())

40
source/framework/log/level.rs

@ -1,3 +1,5 @@
use std::fmt;
#[blonkus_ma::serialize_config]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Level {
@ -28,39 +30,19 @@ impl Default for Level {
}
}
impl Level {
pub const fn name(self) -> &'static str {
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Critical => "Critical",
Self::Error => "Error",
Self::Warning => "Warning",
Self::Notice => "Notice",
Self::Info => "Info",
Self::Debug => "Debug",
Self::Critical => f.write_str("Critical"),
Self::Error => f.write_str("Error"),
Self::Warning => f.write_str("Warning"),
Self::Notice => f.write_str("Notice"),
Self::Info => f.write_str("Info"),
Self::Debug => f.write_str("Debug"),
#[cfg(debug_assertions)]
Self::Trace => "Trace",
Self::Trace => f.write_str("Trace"),
}
}
#[cfg(feature = "color-log")]
pub fn color(self) -> termcolor::ColorSpec {
use termcolor::Color;
let mut color = termcolor::ColorSpec::new();
match self {
Self::Critical => color.set_fg(Some(Color::Red)).set_intense(true),
Self::Error => color.set_fg(Some(Color::Red)),
Self::Warning => color.set_fg(Some(Color::Yellow)).set_intense(true),
Self::Notice => color.set_fg(Some(Color::Yellow)),
Self::Info => color.set_fg(Some(Color::Cyan)).set_intense(true),
Self::Debug => color.set_fg(Some(Color::Cyan)),
#[cfg(debug_assertions)]
Self::Trace => color.set_fg(Some(Color::Magenta)).set_intense(true),
};
color
}
}
// EOF

2
source/framework/log/standard.rs

@ -11,7 +11,7 @@ impl Std {
log: &Log,
out: &mut impl Write,
) -> std::io::Result<()> {
write!(out, "[{:^8}]", record.level.name())?;
write!(out, "[{:^8}]", record.level)?;
log.write_into(args, record, out)?;
Ok(())
}

Loading…
Cancel
Save