arcis-diagnostics 0.4.0

Debugging and performance diagnostics toolkit for the Arcis framework.
Documentation
use colored::*;

pub enum LogLevel {
    Info,
    Warn,
    Error,
}

pub struct Diagnostic {
    level: LogLevel,
    message: String,
    context: Option<String>,
}

impl Diagnostic {
    /// Creates a new diagnostic message.
    pub fn new(level: LogLevel, message: &str, context: Option<&str>) -> Self {
        Self {
            level,
            message: message.to_string(),
            context: context.map(|s| s.to_string()),
        }
    }

    /// Emits the diagnostic message with color styling.
    pub fn emit(&self) {
        match self.level {
            LogLevel::Info => println!("[{}]: {}", String::from("INFO").blue(), self.message),
            LogLevel::Warn => println!("[{}]: {}", String::from("WARNING").yellow(), self.message),
            LogLevel::Error => {
                eprintln!("[{}]: {}", String::from("ERROR").red(), self.message);
                if let Some(ref context) = self.context {
                    eprintln!("{}: {}", "Context".bold(), context);
                }
            }
        }
    }
}

/// Emits an info message to stdinfo.
#[macro_export]
macro_rules! log_info {
    ($($arg:tt)*) => {
        $crate::Diagnostic::new($crate::LogLevel::Info, &format!($($arg)*), None).emit();
    };
}

/// Emits a warning message stdinfo.
#[macro_export]
macro_rules! log_warn {
    ($($arg:tt)*) => {
        $crate::Diagnostic::new($crate::LogLevel::Warning, &format!($($arg)*), None).emit();
    };
}

/// Emits an error message stderr.
#[macro_export]
macro_rules! log_error {
    ($($arg:tt)*) => {
        $crate::Diagnostic::new($crate::LogLevel::Error, &format!($($arg)*), None).emit();
    };
}