diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:04:47 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:20:04 +1300 |
commit | d29420c886d77095c99761f6deb71081f7bc8dcb (patch) | |
tree | 331fac599cc723d72e3bfd2c8fd95b23d33896bd | |
download | log-d29420c886d77095c99761f6deb71081f7bc8dcb.zip |
Initial commit
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | src/lib.rs | 76 | ||||
-rw-r--r-- | src/log_level.rs | 37 |
5 files changed, 127 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bbac0be --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "log" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..027da86 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "log" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e0386ab --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,76 @@ +mod log_level; +pub use log_level::*; + +use std::sync::Mutex; + + +pub const NORMAL: &str = "\x1b[0m"; +pub const BOLD: &str = "\x1b[1m"; +pub const WHITE: &str = "\x1b[37m"; +pub const RED: &str = "\x1b[31m"; +pub const YELLOW: &str = "\x1b[33m"; +pub const BLUE: &str = "\x1b[34m"; + + +pub static LOG_LEVEL: Mutex<LogLevel> = Mutex::new(LogLevel::Warn); + +pub fn set_log_level(level: LogLevel) { + *LOG_LEVEL.lock().unwrap() = level; +} + +pub fn get_log_level() -> LogLevel { + *LOG_LEVEL.lock().unwrap() +} + + +#[macro_export] macro_rules! info { + ($($tokens:tt)*) => { + if *$crate::LOG_LEVEL.lock().unwrap() <= { LogLevel::Info } { + eprint!("{}{}[INFO]{}: ", + $crate::BOLD, + $crate::BLUE, + $crate::NORMAL, + ); + eprint!($($tokens)*); + eprintln!("{}", + $crate::NORMAL, + ); + } + }; +} + +#[macro_export] macro_rules! warn { + ($($tokens:tt)*) => {{ + if *$crate::LOG_LEVEL.lock().unwrap() <= { LogLevel::Warn } { + eprint!("{}{}[WARNING]{}{}: ", + $crate::BOLD, + $crate::YELLOW, + $crate::NORMAL, + $crate::WHITE, + ); + eprint!($($tokens)*); + eprintln!("{}", + $crate::NORMAL, + ); + } + }}; +} + +#[macro_export] macro_rules! error { + ($($tokens:tt)*) => {{ + if *$crate::LOG_LEVEL.lock().unwrap() <= { LogLevel::Error } { + eprint!("{}{}[ERROR]{}: ", + $crate::BOLD, + $crate::RED, + $crate::WHITE, + + ); + eprint!($($tokens)*); + eprintln!("{}", + $crate::NORMAL, + ); + } + std::process::exit(1); + }}; +} + diff --git a/src/log_level.rs b/src/log_level.rs new file mode 100644 index 0000000..6c84cb0 --- /dev/null +++ b/src/log_level.rs @@ -0,0 +1,37 @@ + +use std::cmp::Ordering; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum LogLevel { + /// Verbose record of decisions and operations. + Info, + /// Report recoverable issues. + Warn, + /// Report unrecoverable errors and quit. + Error, +} + +impl PartialOrd for LogLevel { + fn partial_cmp(&self, other: &LogLevel) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +impl Ord for LogLevel { + fn cmp(&self, other: &LogLevel) -> Ordering { + use LogLevel::*; + match (self, other) { + (Info , Info ) => Ordering::Equal, + (Info , Warn ) => Ordering::Less, + (Info , Error) => Ordering::Less, + + (Warn , Info ) => Ordering::Greater, + (Warn , Warn ) => Ordering::Equal, + (Warn , Error) => Ordering::Less, + + (Error , Info ) => Ordering::Greater, + (Error , Warn ) => Ordering::Greater, + (Error , Error) => Ordering::Equal, + } + } +} |