From 21adbec2ce2c0acb3f59cc8fa9b1b3c2804a9bad Mon Sep 17 00:00:00 2001 From: emilis Date: Sun, 3 Mar 2024 14:51:07 +0000 Subject: [PATCH] add error_and_panic helper for logerr --- src/logerr.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/logerr.rs b/src/logerr.rs index 77aa02c..a94b0ee 100644 --- a/src/logerr.rs +++ b/src/logerr.rs @@ -2,6 +2,14 @@ use std::{error::Error, fmt::Debug}; use log::{debug, error}; +// So we still get a useful panic when logs are filtered +macro_rules! error_and_panic { + ($($arg:tt)*) => { + log::error!($($arg)*); + panic!($($arg)*); + }; +} + pub trait UnwrapLog { type Target; @@ -35,8 +43,7 @@ where match self { Ok(target) => target, Err(err) => { - error!("called `Result::unwrap_log()` on an `Err` value: {err}"); - panic!("called `Result::unwrap_log()` on an `Err` value: {err}"); + error_and_panic!("called `Result::unwrap_log()` on an `Err` value: {err}"); } } } @@ -45,8 +52,9 @@ where match self { Ok(target) => target, Err(err) => { - error!("[{expect}] called `Result::expect_log()` on an `Err` value: {err}"); - panic!("[{expect}] called `Result::expect_log()` on an `Err` value: {err}"); + error_and_panic!( + "[{expect}] called `Result::expect_log()` on an `Err` value: {err}" + ); } } } @@ -76,8 +84,7 @@ where match self { Some(target) => target, None => { - error!("called `Option::unwrap_log()` on a `None` value"); - panic!("called `Option::unwrap_log()` on a `None` value"); + error_and_panic!("called `Option::unwrap_log()` on a `None` value"); } } } @@ -86,8 +93,7 @@ where match self { Some(target) => target, None => { - error!("[{expect}] called `Option::expect_log()` on an `None` value"); - panic!("[{expect}] called `Option::expect_log()` on an `None` value"); + error_and_panic!("[{expect}] called `Option::expect_log()` on an `None` value"); } } }