Skip to content

Commit 4176674

Browse files
authored
Added lazy evaluation functionality to error printer. (#510)
Currently, the full message given to a function in error_printer must be always created. This causes extra work when there are no errors if the message should contain additional data. This PR introduces `_fn` versions of the existing functions that call a given function on demand to obtain the message. Thus `.warn_error_fn(|| format!("Error processing {context}"))` would only allocate and create the error string when an error needs to be logged.
1 parent 0958579 commit 4176674

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

error_printer/src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ use tracing::{debug, error, info, warn};
88
/// topmost function without #[track_caller] is deemed the callsite.
99
pub trait ErrorPrinter {
1010
fn log_error<M: Display>(self, message: M) -> Self;
11+
fn log_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self;
1112

1213
fn warn_error<M: Display>(self, message: M) -> Self;
14+
fn warn_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self;
1315

1416
fn debug_error<M: Display>(self, message: M) -> Self;
17+
fn debug_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self;
1518

1619
fn info_error<M: Display>(self, message: M) -> Self;
20+
fn info_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self;
1721
}
1822

1923
impl<T, E: Debug> ErrorPrinter for Result<T, E> {
@@ -31,6 +35,20 @@ impl<T, E: Debug> ErrorPrinter for Result<T, E> {
3135
self
3236
}
3337

38+
/// If self is an Err(e), calls the function to get a string to log to tracing::error,
39+
/// appending "error: {e}" to the end of the message.
40+
#[track_caller]
41+
fn log_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self {
42+
match &self {
43+
Ok(_) => {},
44+
Err(e) => {
45+
let caller = get_caller();
46+
error!(caller, "{}, error: {e:?}", message_fn())
47+
},
48+
}
49+
self
50+
}
51+
3452
/// If self is an Err(e), prints out the given string to tracing::warn,
3553
/// appending "error: {e}" to the end of the message.
3654
#[track_caller]
@@ -45,6 +63,20 @@ impl<T, E: Debug> ErrorPrinter for Result<T, E> {
4563
self
4664
}
4765

66+
/// If self is an Err(e), calls the function to get a string to log to tracing::warn,
67+
/// appending "error: {e}" to the end of the message.
68+
#[track_caller]
69+
fn warn_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self {
70+
match &self {
71+
Ok(_) => {},
72+
Err(e) => {
73+
let caller = get_caller();
74+
warn!(caller, "{}, error: {e:?}", message_fn())
75+
},
76+
}
77+
self
78+
}
79+
4880
/// If self is an Err(e), prints out the given string to tracing::debug,
4981
/// appending "error: {e}" to the end of the message.
5082
#[track_caller]
@@ -59,6 +91,20 @@ impl<T, E: Debug> ErrorPrinter for Result<T, E> {
5991
self
6092
}
6193

94+
/// If self is an Err(e), calls the function to get a string to log to tracing::debug,
95+
/// appending "error: {e}" to the end of the message.
96+
#[track_caller]
97+
fn debug_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self {
98+
match &self {
99+
Ok(_) => {},
100+
Err(e) => {
101+
let caller = get_caller();
102+
debug!(caller, "{}, error: {e:?}", message_fn())
103+
},
104+
}
105+
self
106+
}
107+
62108
/// If self is an Err(e), prints out the given string to tracing::info,
63109
/// appending "error: {e}" to the end of the message.
64110
#[track_caller]
@@ -72,6 +118,20 @@ impl<T, E: Debug> ErrorPrinter for Result<T, E> {
72118
}
73119
self
74120
}
121+
122+
/// If self is an Err(e), calls the function to get a string to log to tracing::info,
123+
/// appending "error: {e}" to the end of the message.
124+
#[track_caller]
125+
fn info_error_fn<M: Display, F: FnOnce() -> M>(self, message_fn: F) -> Self {
126+
match &self {
127+
Ok(_) => {},
128+
Err(e) => {
129+
let caller = get_caller();
130+
info!(caller, "{}, error: {e:?}", message_fn())
131+
},
132+
}
133+
self
134+
}
75135
}
76136

77137
/// A helper trait to log when an option is None.

0 commit comments

Comments
 (0)