Skip to content

Commit 95ed313

Browse files
authored
fix: use logging macros instead of emitting event directly, so that it is also logged by tracing (#7459)
The events are needed when you are not using chatmail core from rust, if you use chatmail core from your rust bot or from tauri, then you likely already use the rust logging/tracing ecosystem. So it makes sense to use it instead of listening to the events and logging them yourself. This pr fixes a few cases where the event was direclty emitted instead of using the macro and thus was not also automatically logged via tracing.
1 parent 98944ef commit 95ed313

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/accounts.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ impl Accounts {
377377
"Starting background fetch for {n_accounts} accounts."
378378
)),
379379
});
380+
::tracing::event!(
381+
::tracing::Level::INFO,
382+
account_id = 0,
383+
"Starting background fetch for {n_accounts} accounts."
384+
);
380385
let mut set = JoinSet::new();
381386
for account in accounts {
382387
set.spawn(async move {
@@ -392,6 +397,11 @@ impl Accounts {
392397
"Finished background fetch for {n_accounts} accounts."
393398
)),
394399
});
400+
::tracing::event!(
401+
::tracing::Level::INFO,
402+
account_id = 0,
403+
"Finished background fetch for {n_accounts} accounts."
404+
);
395405
}
396406

397407
/// Auxiliary function for [Accounts::background_fetch].
@@ -429,6 +439,11 @@ impl Accounts {
429439
id: 0,
430440
typ: EventType::Warning("Background fetch timed out.".to_string()),
431441
});
442+
::tracing::event!(
443+
::tracing::Level::WARN,
444+
account_id = 0,
445+
"Background fetch timed out."
446+
);
432447
}
433448
events.emit(Event {
434449
id: 0,

src/events/chatlist_events.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ pub(crate) async fn emit_chatlist_item_changed_for_contact_chat(
3030
match ChatId::lookup_by_contact(context, contact_id).await {
3131
Ok(Some(chat_id)) => self::emit_chatlist_item_changed(context, chat_id),
3232
Ok(None) => {}
33-
Err(error) => context.emit_event(EventType::Error(format!(
33+
Err(error) => error!(
34+
context,
3435
"failed to find chat id for contact for chatlist event: {error:?}"
35-
))),
36+
),
3637
}
3738
}
3839

src/log/stream.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ impl<S: SessionStream> AsyncRead for LoggingStream<S> {
9292
"Read error on stream {peer_addr:?} after reading {} and writing {} bytes: {err}.",
9393
this.metrics.total_read, this.metrics.total_written
9494
);
95+
tracing::event!(
96+
::tracing::Level::WARN,
97+
account_id = *this.account_id,
98+
log_message
99+
);
95100
this.events.emit(Event {
96101
id: *this.account_id,
97102
typ: EventType::Warning(log_message),

src/qr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use serde::Deserialize;
1616
use crate::config::Config;
1717
use crate::contact::{Contact, ContactId, Origin};
1818
use crate::context::Context;
19-
use crate::events::EventType;
2019
use crate::key::Fingerprint;
2120
use crate::login_param::{EnteredCertificateChecks, EnteredLoginParam, EnteredServerLoginParam};
2221
use crate::net::http::post_empty;
@@ -824,9 +823,10 @@ pub(crate) async fn login_param_from_account_qr(
824823
match serde_json::from_str::<CreateAccountErrorResponse>(&response_text) {
825824
Ok(error) => Err(anyhow!(error.reason)),
826825
Err(parse_error) => {
827-
context.emit_event(EventType::Error(format!(
826+
error!(
827+
context,
828828
"Cannot create account, server response could not be parsed:\n{parse_error:#}\nraw response:\n{response_text}"
829-
)));
829+
);
830830
bail!("Cannot create account, unexpected server response:\n{response_text:?}")
831831
}
832832
}

0 commit comments

Comments
 (0)