mirror of
https://github.com/chatmail/core.git
synced 2026-04-29 11:26:29 +03:00
ref(logging): Remove message from LogExt::log_err (#4235)
This removes the message that needed to be supplied to LogExt::log_err calls. This was from a time before we adopted anyhow and now we are better off using anyhow::Context::context for the message: it is more consistent, composes better and is less custom. The benefit of the composition can be seen in the FFI calls which need to both log the error as well as return it to the caller via the set_last_error mechanism. It also removes the LogExt::ok_or_log_msg funcion for the same reason, the message is obsoleted by anyhow's context.
This commit is contained in:
committed by
GitHub
parent
585b8ece58
commit
82ace72527
41
src/log.rs
41
src/log.rs
@@ -66,7 +66,7 @@ where
|
||||
Self: std::marker::Sized,
|
||||
{
|
||||
#[track_caller]
|
||||
fn log_err_inner(self, context: &Context, msg: Option<&str>) -> Result<T, E>;
|
||||
fn log_err_inner(self, context: &Context) -> Result<T, E>;
|
||||
|
||||
/// Emits a warning if the receiver contains an Err value.
|
||||
///
|
||||
@@ -79,8 +79,8 @@ where
|
||||
/// like warn!(), since the file!() and line!() macros don't work with track_caller)
|
||||
/// See <https://github.com/rust-lang/rust/issues/78840> for progress on this.
|
||||
#[track_caller]
|
||||
fn log_err(self, context: &Context, msg: &str) -> Result<T, E> {
|
||||
self.log_err_inner(context, Some(msg))
|
||||
fn log_err(self, context: &Context) -> Result<T, E> {
|
||||
self.log_err_inner(context)
|
||||
}
|
||||
|
||||
/// Emits a warning if the receiver contains an Err value and returns an [`Option<T>`].
|
||||
@@ -99,50 +99,21 @@ where
|
||||
/// For a note on the `track_caller` feature, see the doc comment on `log_err()`.
|
||||
#[track_caller]
|
||||
fn ok_or_log(self, context: &Context) -> Option<T> {
|
||||
self.log_err_inner(context, None).ok()
|
||||
}
|
||||
|
||||
/// Like `ok_or_log()`, but you can pass an extra message that is prepended in the log.
|
||||
///
|
||||
/// Example:
|
||||
/// ```text
|
||||
/// if let Err(e) = do_something() {
|
||||
/// warn!(context, "Something went wrong: {:#}", e);
|
||||
/// }
|
||||
/// ```
|
||||
/// is equivalent to:
|
||||
/// ```text
|
||||
/// do_something().ok_or_log_msg(context, "Something went wrong");
|
||||
/// ```
|
||||
/// and is also equivalent to:
|
||||
/// ```text
|
||||
/// use anyhow::Context as _;
|
||||
/// do_something().context("Something went wrong").ok_or_log(context);
|
||||
/// ```
|
||||
///
|
||||
/// For a note on the `track_caller` feature, see the doc comment on `log_err()`.
|
||||
#[track_caller]
|
||||
fn ok_or_log_msg(self, context: &Context, msg: &'static str) -> Option<T> {
|
||||
self.log_err_inner(context, Some(msg)).ok()
|
||||
self.log_err_inner(context).ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: std::fmt::Display> LogExt<T, E> for Result<T, E> {
|
||||
#[track_caller]
|
||||
fn log_err_inner(self, context: &Context, msg: Option<&str>) -> Result<T, E> {
|
||||
fn log_err_inner(self, context: &Context) -> Result<T, E> {
|
||||
if let Err(e) = &self {
|
||||
let location = std::panic::Location::caller();
|
||||
|
||||
let separator = if msg.is_none() { "" } else { ": " };
|
||||
let msg = msg.unwrap_or_default();
|
||||
|
||||
// We are using Anyhow's .context() and to show the inner error, too, we need the {:#}:
|
||||
let full = format!(
|
||||
"{file}:{line}: {msg}{separator}{e:#}",
|
||||
"{file}:{line}: {e:#}",
|
||||
file = location.file(),
|
||||
line = location.line(),
|
||||
msg = msg,
|
||||
separator = separator,
|
||||
e = e
|
||||
);
|
||||
// We can't use the warn!() macro here as the file!() and line!() macros
|
||||
|
||||
Reference in New Issue
Block a user