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.

58 lines
1.2 KiB

//! Logging level for filtering and such
use super::*;
/// Logging level
#[derive(PartialEq,Copy,Eq,Debug,Clone,Hash,Ord,PartialOrd)]
pub enum Level
{
Silent,
Error,
Warn,
Info,
Debug,
}
/// Append this trait to allow you to
pub trait AsLevel: fmt::Display + Borrow<Level>{
/// Used for derived levels
fn prefix(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{}/", Borrow::<Level>::borrow(self))
}
}
impl AsLevel for Level{
#[inline] fn prefix(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result{Ok(())}
}
impl AsLevel for &Level{
#[inline] fn prefix(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result{Ok(())}
}
impl Default for Level
{
#[inline]
fn default() -> Level
{
#[cfg(debug_assertions)]
return Level::Debug;
#[cfg(not(debug_assertions))]
return Level::Warn;
}
}
impl std::fmt::Display for Level
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
use recolored::Colorize;
write!(f, "{}", match &self {
Self::Silent => "Fatal".bright_red().bold(),
Self::Error => "Error".red(),
Self::Warn => "Warning".yellow(),
Self::Info => "Info".normal(),
Self::Debug => "Debug".dimmed(),
})
}
}