diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:45:46 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-03 09:45:46 +1300 |
commit | d5ca49462aa830c2c29b82e03eee667b2dbe084a (patch) | |
tree | cf12871ed4537322278bdfbdc4a5d8a5f3ce9131 | |
parent | 2fe3f919c39ea9df6975920e6e9c6c4fc84cb343 (diff) | |
download | log-d5ca49462aa830c2c29b82e03eee667b2dbe084a.zip |
Move ANSI escape code constants to a separate module
This is to prevent the codes from polluting the main namespace when
glob-importing from the log crate.
-rw-r--r-- | src/lib.rs | 48 |
1 files changed, 25 insertions, 23 deletions
@@ -4,12 +4,14 @@ 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 mod ansi { + 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); @@ -27,13 +29,13 @@ pub fn get_log_level() -> LogLevel { ($($tokens:tt)*) => { if *$crate::LOG_LEVEL.lock().unwrap() <= { $crate::LogLevel::Info } { eprint!("{}{}[INFO]{}: ", - $crate::BOLD, - $crate::BLUE, - $crate::NORMAL, + $crate::ansi::BOLD, + $crate::ansi::BLUE, + $crate::ansi::NORMAL, ); eprint!($($tokens)*); eprintln!("{}", - $crate::NORMAL, + $crate::ansi::NORMAL, ); } }; @@ -43,14 +45,14 @@ pub fn get_log_level() -> LogLevel { ($($tokens:tt)*) => {{ if *$crate::LOG_LEVEL.lock().unwrap() <= { $crate::LogLevel::Warn } { eprint!("{}{}[WARNING]{}{}: ", - $crate::BOLD, - $crate::YELLOW, - $crate::NORMAL, - $crate::WHITE, + $crate::ansi::BOLD, + $crate::ansi::YELLOW, + $crate::ansi::NORMAL, + $crate::ansi::WHITE, ); eprint!($($tokens)*); eprintln!("{}", - $crate::NORMAL, + $crate::ansi::NORMAL, ); } }}; @@ -60,14 +62,14 @@ pub fn get_log_level() -> LogLevel { ($($tokens:tt)*) => {{ if *$crate::LOG_LEVEL.lock().unwrap() <= { $crate::LogLevel::Error } { eprint!("{}{}[ERROR]{}: ", - $crate::BOLD, - $crate::RED, - $crate::WHITE, + $crate::ansi::BOLD, + $crate::ansi::RED, + $crate::ansi::WHITE, ); eprint!($($tokens)*); eprintln!("{}", - $crate::NORMAL, + $crate::ansi::NORMAL, ); } }}; @@ -77,13 +79,13 @@ pub fn get_log_level() -> LogLevel { ($($tokens:tt)*) => {{ if *$crate::LOG_LEVEL.lock().unwrap() <= { $crate::LogLevel::Fatal } { eprint!("{}{}[FATAL]{}: ", - $crate::BOLD, - $crate::RED, - $crate::WHITE, + $crate::ansi::BOLD, + $crate::ansi::RED, + $crate::ansi::WHITE, ); eprint!($($tokens)*); eprintln!("{}", - $crate::NORMAL, + $crate::ansi::NORMAL, ); } std::process::exit(1); |