Skip to main content

tuwunel_core/utils/result/
log_debug_err.rs

1use std::fmt::Debug;
2
3use tracing::Level;
4
5use super::{DebugInspect, Result};
6use crate::error;
7
8/// Logs debug-formatted errors when debug assertions are enabled.
9///
10/// Successful values pass through without producing a record. With debug
11/// assertions disabled, errors also pass through without producing a record.
12pub trait LogDebugErr<T, E: Debug> {
13	/// Logs a debug-formatted error at `level` and returns the original result.
14	///
15	/// The error is formatted with [`Debug`] when debug assertions are enabled.
16	/// Successful values always produce no log record.
17	#[must_use]
18	fn err_debug_log(self, level: Level) -> Self;
19
20	/// Logs a debug-formatted error at the error tracing level.
21	///
22	/// [`LogDebugErr::err_debug_log`] supplies this convenience form with
23	/// [`Level::ERROR`]. Logging occurs only when debug assertions are enabled,
24	/// and the original result is returned after inspection.
25	#[must_use]
26	fn log_debug_err(self) -> Self
27	where
28		Self: Sized,
29	{
30		self.err_debug_log(Level::ERROR)
31	}
32}
33
34impl<T, E: Debug> LogDebugErr<T, E> for Result<T, E> {
35	#[inline]
36	fn err_debug_log(self, level: Level) -> Self {
37		self.debug_inspect_err(|error| error::inspect_debug_log_level(&error, level))
38	}
39}