Initial
This commit is contained in:
32
src/logger/colors.rs
Normal file
32
src/logger/colors.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
#![allow(dead_code)]
|
||||
pub const RESET: &str = "\x1b[0m";
|
||||
pub const BOLD: &str = "\x1b[1m";
|
||||
pub const ITALIC: &str = "\x1b[3m";
|
||||
pub const UNDERLINE: &str = "\x1b[4m";
|
||||
pub const BLINK: &str = "\x1b[5m";
|
||||
pub const BLINK2: &str = "\x1b[6m";
|
||||
pub const SELECTED: &str = "\x1b[7m";
|
||||
pub const BLACK: &str = "\x1b[30m";
|
||||
pub const RED: &str = "\x1b[31m";
|
||||
pub const GREEN: &str = "\x1b[32m";
|
||||
pub const YELLOW: &str = "\x1b[33m";
|
||||
pub const BLUE: &str = "\x1b[34m";
|
||||
pub const MAGENTA: &str = "\x1b[35m";
|
||||
pub const BEIGE: &str = "\x1b[36m";
|
||||
pub const WHITE: &str = "\x1b[37m";
|
||||
pub const BLACKBG: &str = "\x1b[40m";
|
||||
pub const REDBG: &str = "\x1b[41m";
|
||||
pub const GREENBG: &str = "\x1b[42m";
|
||||
pub const YELLOWBG: &str = "\x1b[43m";
|
||||
pub const BLUEBG: &str = "\x1b[44m";
|
||||
pub const MAGENTABG: &str = "\x1b[45m";
|
||||
pub const BEIGEBG: &str = "\x1b[46m";
|
||||
pub const WHITEBG: &str = "\x1b[47m";
|
||||
pub const GREY: &str = "\x1b[90m";
|
||||
pub const RED2: &str = "\x1b[91m";
|
||||
pub const GREEN2: &str = "\x1b[92m";
|
||||
pub const YELLOW2: &str = "\x1b[93m";
|
||||
pub const BLUE2: &str = "\x1b[94m";
|
||||
pub const MAGENTA2: &str = "\x1b[95m";
|
||||
pub const BEIGE2: &str = "\x1b[96m";
|
||||
pub const WHITE2: &str = "\x1b[97m";
|
||||
106
src/logger/macros.rs
Normal file
106
src/logger/macros.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log {
|
||||
({$($k: expr => $v: expr),* $(,)? }, $lvl:expr, $($arg:tt),+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, crate::logger::Level::Info, $($arg)+)
|
||||
};
|
||||
(module: $module:expr, $lvl:expr, $($arg:tt)+) => {
|
||||
unsafe {
|
||||
crate::logger::LOGGER.log(
|
||||
crate::logger::LogEvent {
|
||||
level: $lvl,
|
||||
module_path: $module.to_string(),
|
||||
message: format!($($arg)+),
|
||||
tags: std::collections::HashMap::new()
|
||||
}
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
($lvl:expr, $($arg:tt)+) => {
|
||||
crate::log!(module: module_path!(), $lvl, $($arg)+)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_tagged {
|
||||
({$($k: expr => $v: expr),* $(,)? }, $module:expr, $lvl:expr, $($arg:tt)+) => {
|
||||
unsafe {
|
||||
crate::logger::LOGGER.log(
|
||||
crate::logger::LogEvent {
|
||||
level: $lvl,
|
||||
module_path: $module.to_string(),
|
||||
message: format!($($arg)+),
|
||||
tags: map_macro::hash_map!{$(stringify!($k).to_string() => Box::new($v) as Box<dyn core::any::Any>,)*}
|
||||
}
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
(module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log!(module: $module, crate::logger::Level::Debug, $($arg:tt)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, module_path!(), crate::logger::Level::Debug, $($arg)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, $module, crate::logger::Level::Debug, $($arg)+)
|
||||
};
|
||||
($($arg:tt)+) => {
|
||||
crate::log!(crate::logger::Level::Debug, $($arg)+)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
(module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log!(module: $module, crate::logger::Level::Info, $($arg:tt)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, module_path!(), crate::logger::Level::Info, $($arg)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, $module, crate::logger::Level::Info, $($arg)+)
|
||||
};
|
||||
($($arg:tt)+) => {
|
||||
crate::log!(crate::logger::Level::Info, $($arg)+)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! warn {
|
||||
(module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log!(module: $module, crate::logger::Level::Warn, $($arg:tt)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, module_path!(), crate::logger::Level::Warn, $($arg)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, $module, crate::logger::Level::Warn, $($arg)+)
|
||||
};
|
||||
($($arg:tt)+) => {
|
||||
crate::log!(crate::logger::Level::Warn, $($arg)+)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
(module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log!(module: $module, crate::logger::Level::Error, $($arg:tt)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, module_path!(), crate::logger::Level::Error, $($arg)+)
|
||||
};
|
||||
({$($k: expr => $v: expr),* $(,)? }, module: $module:expr, $($arg:tt)+) => {
|
||||
crate::log_tagged!({$($k => $v,)*}, $module, crate::logger::Level::Error, $($arg)+)
|
||||
};
|
||||
($($arg:tt)+) => {
|
||||
crate::log!(crate::logger::Level::Error, $($arg)+)
|
||||
};
|
||||
}
|
||||
83
src/logger/mod.rs
Normal file
83
src/logger/mod.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
// use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{cli::CliArgs, types::common::Loc};
|
||||
|
||||
mod types;
|
||||
mod colors;
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
pub use types::{Level, LogEvent, LOGGER};
|
||||
use types::*;
|
||||
|
||||
|
||||
pub struct Logger{
|
||||
pub level: i8
|
||||
}
|
||||
|
||||
|
||||
impl Logger {
|
||||
pub fn new(args: &CliArgs) -> Box<Self> {
|
||||
Box::new(Self {
|
||||
level: args.verbose
|
||||
})
|
||||
}
|
||||
|
||||
pub fn init(args: &CliArgs) -> anyhow::Result<()>{
|
||||
unsafe {
|
||||
types::LOGGER = Box::leak(
|
||||
Self::new(args)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_prefix(&self, level: Level) -> String {
|
||||
use colors::{BOLD, RESET, RED, YELLOW, BLUE, GREEN, MAGENTA};
|
||||
match level {
|
||||
Level::Error => format!("{BOLD}{RED}error{RESET}", ),
|
||||
Level::Warn => format!("{BOLD}{YELLOW}warn{RESET}", ),
|
||||
Level::Info => format!("{BOLD}{GREEN}info{RESET}", ),
|
||||
Level::Debug => format!("{BOLD}{BLUE}debug{RESET}", ),
|
||||
Level::Trace => format!("{BOLD}{MAGENTA}trace{RESET}", ),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Log for Logger {
|
||||
fn enabled(&self, level: Level) -> bool {
|
||||
match level {
|
||||
Level::Error if self.level >= 0 => true,
|
||||
Level::Warn |
|
||||
Level::Info if self.level >= 1 => true,
|
||||
Level::Debug if self.level >= 2 => true,
|
||||
Level::Trace if self.level >= 3 => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn log(&self, event: LogEvent) {
|
||||
|
||||
if self.enabled(event.level) {
|
||||
let modpath = if event.level > Level::Info {
|
||||
format!(" [{}]", event.module_path)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
if let Some(loc) = event.tags.get("loc") {
|
||||
let loc: String = (*loc.deref()).downcast_ref::<Loc>()
|
||||
.map_or(String::from("INVALID"), |l| l.to_string());
|
||||
println!("{} {}{modpath}: {}", loc, self.get_prefix(event.level), event.message);
|
||||
} else {
|
||||
println!("{}{modpath}: {}", self.get_prefix(event.level), event.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn level(&self) -> i8 {
|
||||
self.level
|
||||
}
|
||||
}
|
||||
40
src/logger/types.rs
Normal file
40
src/logger/types.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::{any::Any, collections::HashMap, fmt::Debug };
|
||||
|
||||
|
||||
pub static mut LOGGER: &dyn Log = &NopLogger;
|
||||
|
||||
struct NopLogger;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub enum Level {
|
||||
Error = 1,
|
||||
Warn,
|
||||
Info,
|
||||
Debug,
|
||||
Trace,
|
||||
}
|
||||
|
||||
// pub trait Tag: Display + Debug + Any {}
|
||||
|
||||
|
||||
pub struct LogEvent {
|
||||
pub level: Level,
|
||||
pub module_path: String,
|
||||
pub message: String,
|
||||
pub tags: HashMap<String, Box<dyn Any>>
|
||||
}
|
||||
|
||||
|
||||
impl Log for NopLogger {
|
||||
fn enabled(&self, _: Level) -> bool {false}
|
||||
fn level(&self) -> i8 {0}
|
||||
fn log(&self, _: LogEvent) {}
|
||||
}
|
||||
|
||||
|
||||
pub trait Log {
|
||||
fn enabled(&self, level: Level) -> bool;
|
||||
fn log(&self, event: LogEvent);
|
||||
fn level(&self) -> i8;
|
||||
}
|
||||
Reference in New Issue
Block a user