summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-02-03 09:34:56 +1300
committerBen Bridle <ben@derelict.engineering>2025-02-03 09:34:56 +1300
commit4db8a3c200345d67ef178308a11d253a112d6304 (patch)
tree28ed058ab1efe645a242640dd2620960779dd150 /src
parent9d9fe9f30e46bc3b723d6a35434c3202c7f0f163 (diff)
downloadlog-4db8a3c200345d67ef178308a11d253a112d6304.zip
Implement a fatal log level
The error log level now reports recoverable errors, with the fatal log level being reserved for unrecoverable errors where the program should immediately terminate.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs16
-rw-r--r--src/log_level.rs12
2 files changed, 27 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7af1f20..5777fcd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -70,6 +70,22 @@ pub fn get_log_level() -> LogLevel {
$crate::NORMAL,
);
}
+ }};
+}
+
+#[macro_export] macro_rules! fatal {
+ ($($tokens:tt)*) => {{
+ if *$crate::LOG_LEVEL.lock().unwrap() <= { $crate::LogLevel::Fatal } {
+ eprint!("{}{}[FATAL]{}: ",
+ $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
index 6c84cb0..9293efc 100644
--- a/src/log_level.rs
+++ b/src/log_level.rs
@@ -7,8 +7,10 @@ pub enum LogLevel {
Info,
/// Report recoverable issues.
Warn,
- /// Report unrecoverable errors and quit.
+ /// Report recoverable process errors.
Error,
+ /// Report unrecoverable application errors and quit.
+ Fatal,
}
impl PartialOrd for LogLevel {
@@ -24,14 +26,22 @@ impl Ord for LogLevel {
(Info , Info ) => Ordering::Equal,
(Info , Warn ) => Ordering::Less,
(Info , Error) => Ordering::Less,
+ (Info , Fatal) => Ordering::Less,
(Warn , Info ) => Ordering::Greater,
(Warn , Warn ) => Ordering::Equal,
(Warn , Error) => Ordering::Less,
+ (Warn , Fatal) => Ordering::Less,
(Error , Info ) => Ordering::Greater,
(Error , Warn ) => Ordering::Greater,
(Error , Error) => Ordering::Equal,
+ (Error , Fatal) => Ordering::Less,
+
+ (Fatal , Info ) => Ordering::Greater,
+ (Fatal , Warn ) => Ordering::Greater,
+ (Fatal , Error) => Ordering::Greater,
+ (Fatal , Fatal) => Ordering::Equal,
}
}
}