use colored::*;
pub enum LogLevel {
Info,
Warn,
Error,
}
pub struct Diagnostic {
level: LogLevel,
message: String,
context: Option<String>,
}
impl Diagnostic {
pub fn new(level: LogLevel, message: &str, context: Option<&str>) -> Self {
Self {
level,
message: message.to_string(),
context: context.map(|s| s.to_string()),
}
}
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);
}
}
}
}
}
#[macro_export]
macro_rules! log_info {
($($arg:tt)*) => {
$crate::Diagnostic::new($crate::LogLevel::Info, &format!($($arg)*), None).emit();
};
}
#[macro_export]
macro_rules! log_warn {
($($arg:tt)*) => {
$crate::Diagnostic::new($crate::LogLevel::Warning, &format!($($arg)*), None).emit();
};
}
#[macro_export]
macro_rules! log_error {
($($arg:tt)*) => {
$crate::Diagnostic::new($crate::LogLevel::Error, &format!($($arg)*), None).emit();
};
}