mirror of
https://github.com/chatmail/core.git
synced 2026-04-26 09:56:35 +03:00
ref(logging): remove LogExt::log_or_ok (#4250)
This further reduces the cognitive overload of having many ways to do
something. The same is very easily done using composition. Followup
from 82ace72527.
This commit is contained in:
committed by
GitHub
parent
fd7cc83537
commit
61b8d04418
@@ -563,7 +563,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu
|
||||
context.maybe_network().await;
|
||||
}
|
||||
"housekeeping" => {
|
||||
sql::housekeeping(&context).await.ok_or_log(&context);
|
||||
sql::housekeeping(&context).await.log_err(&context).ok();
|
||||
}
|
||||
"listchats" | "listarchived" | "chats" => {
|
||||
let listflags = if arg0 == "listarchived" {
|
||||
|
||||
@@ -92,7 +92,7 @@ impl<'a> BlobObject<'a> {
|
||||
if attempt >= MAX_ATTEMPT {
|
||||
return Err(err).context("failed to create file");
|
||||
} else if attempt == 1 && !dir.exists() {
|
||||
fs::create_dir_all(dir).await.ok_or_log(context);
|
||||
fs::create_dir_all(dir).await.log_err(context).ok();
|
||||
} else {
|
||||
name = format!("{}-{}{}", stem, rand::random::<u32>(), ext);
|
||||
}
|
||||
|
||||
@@ -575,7 +575,8 @@ pub(crate) async fn ephemeral_loop(context: &Context, interrupt_receiver: Receiv
|
||||
|
||||
delete_expired_messages(context, time())
|
||||
.await
|
||||
.ok_or_log(context);
|
||||
.log_err(context)
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -759,7 +759,7 @@ async fn export_database(context: &Context, dest: &Path, passphrase: String) ->
|
||||
.with_context(|| format!("path {} is not valid unicode", dest.display()))?;
|
||||
|
||||
context.sql.set_raw_config_int("backup_time", now).await?;
|
||||
sql::housekeeping(context).await.ok_or_log(context);
|
||||
sql::housekeeping(context).await.log_err(context).ok();
|
||||
context
|
||||
.sql
|
||||
.call_write(|conn| {
|
||||
|
||||
30
src/log.rs
30
src/log.rs
@@ -65,9 +65,6 @@ pub trait LogExt<T, E>
|
||||
where
|
||||
Self: std::marker::Sized,
|
||||
{
|
||||
#[track_caller]
|
||||
fn log_err_inner(self, context: &Context) -> Result<T, E>;
|
||||
|
||||
/// Emits a warning if the receiver contains an Err value.
|
||||
///
|
||||
/// Thanks to the [track_caller](https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html#track_caller)
|
||||
@@ -76,36 +73,15 @@ where
|
||||
/// Unfortunately, the track_caller feature does not work on async functions (as of Rust 1.50).
|
||||
/// Once it is, you can add `#[track_caller]` to helper functions that use one of the log helpers here
|
||||
/// so that the location of the caller can be seen in the log. (this won't work with the macros,
|
||||
/// like warn!(), since the file!() and line!() macros don't work with track_caller)
|
||||
/// 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) -> Result<T, E> {
|
||||
self.log_err_inner(context)
|
||||
}
|
||||
|
||||
/// Emits a warning if the receiver contains an Err value and returns an [`Option<T>`].
|
||||
///
|
||||
/// Example:
|
||||
/// ```text
|
||||
/// if let Err(e) = do_something() {
|
||||
/// warn!(context, "{:#}", e);
|
||||
/// }
|
||||
/// ```
|
||||
/// is equivalent to:
|
||||
/// ```text
|
||||
/// do_something().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(self, context: &Context) -> Option<T> {
|
||||
self.log_err_inner(context).ok()
|
||||
}
|
||||
fn log_err(self, context: &Context) -> Result<T, E>;
|
||||
}
|
||||
|
||||
impl<T, E: std::fmt::Display> LogExt<T, E> for Result<T, E> {
|
||||
#[track_caller]
|
||||
fn log_err_inner(self, context: &Context) -> Result<T, E> {
|
||||
fn log_err(self, context: &Context) -> Result<T, E> {
|
||||
if let Err(e) = &self {
|
||||
let location = std::panic::Location::caller();
|
||||
|
||||
|
||||
@@ -303,7 +303,7 @@ async fn inbox_loop(ctx: Context, started: Sender<()>, inbox_handlers: ImapConne
|
||||
let next_housekeeping_time =
|
||||
last_housekeeping_time.saturating_add(60 * 60 * 24);
|
||||
if next_housekeeping_time <= time() {
|
||||
sql::housekeeping(&ctx).await.ok_or_log(&ctx);
|
||||
sql::housekeeping(&ctx).await.log_err(&ctx).ok();
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -412,7 +412,8 @@ async fn fetch_idle(
|
||||
.store_seen_flags_on_imap(ctx)
|
||||
.await
|
||||
.context("store_seen_flags_on_imap")
|
||||
.ok_or_log(ctx);
|
||||
.log_err(ctx)
|
||||
.ok();
|
||||
} else {
|
||||
warn!(ctx, "No session even though we just prepared it");
|
||||
}
|
||||
@@ -436,7 +437,8 @@ async fn fetch_idle(
|
||||
delete_expired_imap_messages(ctx)
|
||||
.await
|
||||
.context("delete_expired_imap_messages")
|
||||
.ok_or_log(ctx);
|
||||
.log_err(ctx)
|
||||
.ok();
|
||||
|
||||
// Scan additional folders only after finishing fetching the watched folder.
|
||||
//
|
||||
@@ -476,7 +478,8 @@ async fn fetch_idle(
|
||||
.sync_seen_flags(ctx, &watch_folder)
|
||||
.await
|
||||
.context("sync_seen_flags")
|
||||
.ok_or_log(ctx);
|
||||
.log_err(ctx)
|
||||
.ok();
|
||||
|
||||
connection.connectivity.set_connected(ctx).await;
|
||||
|
||||
@@ -772,20 +775,22 @@ impl Scheduler {
|
||||
pub(crate) async fn stop(self, context: &Context) {
|
||||
// Send stop signals to tasks so they can shutdown cleanly.
|
||||
for b in self.boxes() {
|
||||
b.conn_state.stop().await.ok_or_log(context);
|
||||
b.conn_state.stop().await.log_err(context).ok();
|
||||
}
|
||||
self.smtp.stop().await.ok_or_log(context);
|
||||
self.smtp.stop().await.log_err(context).ok();
|
||||
|
||||
// Actually shutdown tasks.
|
||||
let timeout_duration = std::time::Duration::from_secs(30);
|
||||
for b in once(self.inbox).chain(self.oboxes.into_iter()) {
|
||||
tokio::time::timeout(timeout_duration, b.handle)
|
||||
.await
|
||||
.ok_or_log(context);
|
||||
.log_err(context)
|
||||
.ok();
|
||||
}
|
||||
tokio::time::timeout(timeout_duration, self.smtp_handle)
|
||||
.await
|
||||
.ok_or_log(context);
|
||||
.log_err(context)
|
||||
.ok();
|
||||
self.ephemeral_handle.abort();
|
||||
self.location_handle.abort();
|
||||
self.recently_seen_loop.abort();
|
||||
|
||||
@@ -337,7 +337,7 @@ impl Context {
|
||||
let mut folder_added = false;
|
||||
|
||||
if let Some(config) = folder.to_config().filter(|c| watched_folders.contains(c)) {
|
||||
let f = self.get_config(config).await.ok_or_log(self).flatten();
|
||||
let f = self.get_config(config).await.log_err(self).ok().flatten();
|
||||
|
||||
if let Some(foldername) = f {
|
||||
let detailed = &state.get_detailed().await;
|
||||
|
||||
Reference in New Issue
Block a user