mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +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;
|
context.maybe_network().await;
|
||||||
}
|
}
|
||||||
"housekeeping" => {
|
"housekeeping" => {
|
||||||
sql::housekeeping(&context).await.ok_or_log(&context);
|
sql::housekeeping(&context).await.log_err(&context).ok();
|
||||||
}
|
}
|
||||||
"listchats" | "listarchived" | "chats" => {
|
"listchats" | "listarchived" | "chats" => {
|
||||||
let listflags = if arg0 == "listarchived" {
|
let listflags = if arg0 == "listarchived" {
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ impl<'a> BlobObject<'a> {
|
|||||||
if attempt >= MAX_ATTEMPT {
|
if attempt >= MAX_ATTEMPT {
|
||||||
return Err(err).context("failed to create file");
|
return Err(err).context("failed to create file");
|
||||||
} else if attempt == 1 && !dir.exists() {
|
} 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 {
|
} else {
|
||||||
name = format!("{}-{}{}", stem, rand::random::<u32>(), ext);
|
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())
|
delete_expired_messages(context, time())
|
||||||
.await
|
.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()))?;
|
.with_context(|| format!("path {} is not valid unicode", dest.display()))?;
|
||||||
|
|
||||||
context.sql.set_raw_config_int("backup_time", now).await?;
|
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
|
context
|
||||||
.sql
|
.sql
|
||||||
.call_write(|conn| {
|
.call_write(|conn| {
|
||||||
|
|||||||
28
src/log.rs
28
src/log.rs
@@ -65,9 +65,6 @@ pub trait LogExt<T, E>
|
|||||||
where
|
where
|
||||||
Self: std::marker::Sized,
|
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.
|
/// 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)
|
/// Thanks to the [track_caller](https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html#track_caller)
|
||||||
@@ -79,33 +76,12 @@ where
|
|||||||
/// 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.
|
/// See <https://github.com/rust-lang/rust/issues/78840> for progress on this.
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn log_err(self, context: &Context) -> Result<T, E> {
|
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, E: std::fmt::Display> LogExt<T, E> for Result<T, E> {
|
impl<T, E: std::fmt::Display> LogExt<T, E> for Result<T, E> {
|
||||||
#[track_caller]
|
#[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 {
|
if let Err(e) = &self {
|
||||||
let location = std::panic::Location::caller();
|
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 =
|
let next_housekeeping_time =
|
||||||
last_housekeeping_time.saturating_add(60 * 60 * 24);
|
last_housekeeping_time.saturating_add(60 * 60 * 24);
|
||||||
if next_housekeeping_time <= time() {
|
if next_housekeeping_time <= time() {
|
||||||
sql::housekeeping(&ctx).await.ok_or_log(&ctx);
|
sql::housekeeping(&ctx).await.log_err(&ctx).ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -412,7 +412,8 @@ async fn fetch_idle(
|
|||||||
.store_seen_flags_on_imap(ctx)
|
.store_seen_flags_on_imap(ctx)
|
||||||
.await
|
.await
|
||||||
.context("store_seen_flags_on_imap")
|
.context("store_seen_flags_on_imap")
|
||||||
.ok_or_log(ctx);
|
.log_err(ctx)
|
||||||
|
.ok();
|
||||||
} else {
|
} else {
|
||||||
warn!(ctx, "No session even though we just prepared it");
|
warn!(ctx, "No session even though we just prepared it");
|
||||||
}
|
}
|
||||||
@@ -436,7 +437,8 @@ async fn fetch_idle(
|
|||||||
delete_expired_imap_messages(ctx)
|
delete_expired_imap_messages(ctx)
|
||||||
.await
|
.await
|
||||||
.context("delete_expired_imap_messages")
|
.context("delete_expired_imap_messages")
|
||||||
.ok_or_log(ctx);
|
.log_err(ctx)
|
||||||
|
.ok();
|
||||||
|
|
||||||
// Scan additional folders only after finishing fetching the watched folder.
|
// Scan additional folders only after finishing fetching the watched folder.
|
||||||
//
|
//
|
||||||
@@ -476,7 +478,8 @@ async fn fetch_idle(
|
|||||||
.sync_seen_flags(ctx, &watch_folder)
|
.sync_seen_flags(ctx, &watch_folder)
|
||||||
.await
|
.await
|
||||||
.context("sync_seen_flags")
|
.context("sync_seen_flags")
|
||||||
.ok_or_log(ctx);
|
.log_err(ctx)
|
||||||
|
.ok();
|
||||||
|
|
||||||
connection.connectivity.set_connected(ctx).await;
|
connection.connectivity.set_connected(ctx).await;
|
||||||
|
|
||||||
@@ -772,20 +775,22 @@ impl Scheduler {
|
|||||||
pub(crate) async fn stop(self, context: &Context) {
|
pub(crate) async fn stop(self, context: &Context) {
|
||||||
// Send stop signals to tasks so they can shutdown cleanly.
|
// Send stop signals to tasks so they can shutdown cleanly.
|
||||||
for b in self.boxes() {
|
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.
|
// Actually shutdown tasks.
|
||||||
let timeout_duration = std::time::Duration::from_secs(30);
|
let timeout_duration = std::time::Duration::from_secs(30);
|
||||||
for b in once(self.inbox).chain(self.oboxes.into_iter()) {
|
for b in once(self.inbox).chain(self.oboxes.into_iter()) {
|
||||||
tokio::time::timeout(timeout_duration, b.handle)
|
tokio::time::timeout(timeout_duration, b.handle)
|
||||||
.await
|
.await
|
||||||
.ok_or_log(context);
|
.log_err(context)
|
||||||
|
.ok();
|
||||||
}
|
}
|
||||||
tokio::time::timeout(timeout_duration, self.smtp_handle)
|
tokio::time::timeout(timeout_duration, self.smtp_handle)
|
||||||
.await
|
.await
|
||||||
.ok_or_log(context);
|
.log_err(context)
|
||||||
|
.ok();
|
||||||
self.ephemeral_handle.abort();
|
self.ephemeral_handle.abort();
|
||||||
self.location_handle.abort();
|
self.location_handle.abort();
|
||||||
self.recently_seen_loop.abort();
|
self.recently_seen_loop.abort();
|
||||||
|
|||||||
@@ -337,7 +337,7 @@ impl Context {
|
|||||||
let mut folder_added = false;
|
let mut folder_added = false;
|
||||||
|
|
||||||
if let Some(config) = folder.to_config().filter(|c| watched_folders.contains(c)) {
|
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 {
|
if let Some(foldername) = f {
|
||||||
let detailed = &state.get_detailed().await;
|
let detailed = &state.get_detailed().await;
|
||||||
|
|||||||
Reference in New Issue
Block a user