urs 0.5.1

Rust utility library
Documentation
//! # log
//!
//! `log` logging function and macro
use super::console;
use chrono;
use std::fmt::Display;

/// Log levels
#[derive(PartialEq, Eq, Clone)]
pub enum LogType {
    Info,
    Warn,
    Error,
    Fatal,
}
use LogType::*;

/// Logs to stderr, used by the log! macro.
///
/// If Fatal is given with `true` then
/// the program exits after the logging.
pub fn log<T>(t: LogType, s: T)
where
    T: Display,
{
    eprint!(
        "{}{}{:?}{} ",
        console::bold(),
        console::color_fg(console::Color::Gray),
        chrono::offset::Local::now(),
        console::reset()
    );

    match t {
        Info => eprint!("{}INFO: ", console::color_fg(console::Color::BrightGreen)),
        Warn => eprint!("{}WARN: ", console::color_fg(console::Color::BrightYellow)),
        Error => eprint!("{}ERROR: ", console::color_fg(console::Color::BrightRed)),
        Fatal => eprint!(
            "{}{}FATAL: ",
            console::bold(),
            console::color_fg(console::Color::BrightRed)
        ),
    }

    eprintln!("{}{s}", console::reset());
}

/// Logs to stderr, accepts multiple arguments.
///
/// Usage example:
///
/// ```
/// use urs::log;
/// urs::log!(log::LogType::WARN, "This number is odd: {}!", 5);
/// urs::log!([info] "Info attribute");
/// urs::log!([fatal] "This log will be logged with the {} type {}", "Fatal",
///     "using [fatal] in the log! macro and exit afterwards");
/// ```
///
/// There are attributes for each logging method:
///
/// - [info]
/// - [warn]
/// - [error]
/// - [fatal]
#[macro_export]
macro_rules! log {
    ($t:expr, $($x:expr),*) => {
        $crate::log::log($t, format!($($x,)*));
    };
    ([info] $($x:expr),*) => {
        $crate::log::log($crate::log::LogType::Info, format!($($x,)*));
    };
    ([warn] $($x:expr),*) => {
        $crate::log::log($crate::log::LogType::Warn, format!($($x,)*));
    };
    ([error] $($x:expr),*) => {
        $crate::log::log($crate::log::LogType::Error, format!($($x,)*));
    };
    ([fatal] $($x:expr),*) => {
        $crate::log::log($crate::log::LogType::Fatal, format!($($x,)*));
        ::std::process::exit(1)
    }
}