Skip to content

Commit 58b99f5

Browse files
committed
feat: Log failed debug assertions in all configurations
Add `logged_debug_assert` macro logging a warning if a condition is not satisfied, before invoking `debug_assert!`, and use this macro where `Context` is accessible (i.e. don't change function signatures for now). Follow-up to 0359481.
1 parent 402e42f commit 58b99f5

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

src/chat.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::ephemeral::{Timer as EphemeralTimer, start_chat_ephemeral_timers};
3333
use crate::events::EventType;
3434
use crate::location;
3535
use crate::log::{LogExt, error, info, warn};
36+
use crate::logged_debug_assert;
3637
use crate::message::{self, Message, MessageState, MsgId, Viewtype};
3738
use crate::mimefactory::MimeFactory;
3839
use crate::mimeparser::SystemMessage;
@@ -1339,14 +1340,18 @@ impl ChatId {
13391340

13401341
let mut ret = stock_str::e2e_available(context).await + "\n";
13411342

1342-
for contact_id in get_chat_contacts(context, self)
1343+
for &contact_id in get_chat_contacts(context, self)
13431344
.await?
13441345
.iter()
13451346
.filter(|&contact_id| !contact_id.is_special())
13461347
{
1347-
let contact = Contact::get_by_id(context, *contact_id).await?;
1348+
let contact = Contact::get_by_id(context, contact_id).await?;
13481349
let addr = contact.get_addr();
1349-
debug_assert!(contact.is_key_contact());
1350+
logged_debug_assert!(
1351+
context,
1352+
contact.is_key_contact(),
1353+
"get_encryption_info: contact {contact_id} is not a key-contact."
1354+
);
13501355
let fingerprint = contact
13511356
.fingerprint()
13521357
.context("Contact does not have a fingerprint in encrypted chat")?;

src/context.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::events::{Event, EventEmitter, EventType, Events};
2727
use crate::imap::{FolderMeaning, Imap, ServerMetadata};
2828
use crate::key::{load_self_secret_key, self_fingerprint};
2929
use crate::log::{info, warn};
30+
use crate::logged_debug_assert;
3031
use crate::login_param::{ConfiguredLoginParam, EnteredLoginParam};
3132
use crate::message::{self, Message, MessageState, MsgId};
3233
use crate::param::{Param, Params};
@@ -660,8 +661,16 @@ impl Context {
660661
/// or [`Self::emit_msgs_changed_without_msg_id`] should be used
661662
/// instead of this function.
662663
pub fn emit_msgs_changed(&self, chat_id: ChatId, msg_id: MsgId) {
663-
debug_assert!(!chat_id.is_unset());
664-
debug_assert!(!msg_id.is_unset());
664+
logged_debug_assert!(
665+
self,
666+
!chat_id.is_unset(),
667+
"emit_msgs_changed: chat_id is unset."
668+
);
669+
logged_debug_assert!(
670+
self,
671+
!msg_id.is_unset(),
672+
"emit_msgs_changed: msg_id is unset."
673+
);
665674

666675
self.emit_event(EventType::MsgsChanged { chat_id, msg_id });
667676
chatlist_events::emit_chatlist_changed(self);
@@ -670,7 +679,11 @@ impl Context {
670679

671680
/// Emits a MsgsChanged event with specified chat and without message id.
672681
pub fn emit_msgs_changed_without_msg_id(&self, chat_id: ChatId) {
673-
debug_assert!(!chat_id.is_unset());
682+
logged_debug_assert!(
683+
self,
684+
!chat_id.is_unset(),
685+
"emit_msgs_changed_without_msg_id: chat_id is unset."
686+
);
674687

675688
self.emit_event(EventType::MsgsChanged {
676689
chat_id,

src/receive_imf.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::key::self_fingerprint_opt;
3131
use crate::key::{DcKey, Fingerprint, SignedPublicKey};
3232
use crate::log::LogExt;
3333
use crate::log::{info, warn};
34+
use crate::logged_debug_assert;
3435
use crate::message::{
3536
self, Message, MessageState, MessengerMessage, MsgId, Viewtype, rfc724_mid_exists,
3637
};
@@ -3835,7 +3836,11 @@ async fn lookup_key_contact_by_fingerprint(
38353836
context: &Context,
38363837
fingerprint: &str,
38373838
) -> Result<Option<ContactId>> {
3838-
debug_assert!(!fingerprint.is_empty());
3839+
logged_debug_assert!(
3840+
context,
3841+
!fingerprint.is_empty(),
3842+
"lookup_key_contact_by_fingerprint: fingerprint is empty."
3843+
);
38393844
if fingerprint.is_empty() {
38403845
// Avoid accidentally looking up a non-key-contact.
38413846
return Ok(None);

src/securejoin.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::events::EventType;
1616
use crate::headerdef::HeaderDef;
1717
use crate::key::{DcKey, Fingerprint, load_self_public_key};
1818
use crate::log::{error, info, warn};
19+
use crate::logged_debug_assert;
1920
use crate::message::{Message, Viewtype};
2021
use crate::mimeparser::{MimeMessage, SystemMessage};
2122
use crate::param::Param;
@@ -32,9 +33,10 @@ use qrinvite::QrInvite;
3233
use crate::token::Namespace;
3334

3435
fn inviter_progress(context: &Context, contact_id: ContactId, progress: usize) {
35-
debug_assert!(
36+
logged_debug_assert!(
37+
context,
3638
progress <= 1000,
37-
"value in range 0..1000 expected with: 0=error, 1..999=progress, 1000=success"
39+
"inviter_progress: contact {contact_id}, progress={progress}, but value in range 0..1000 expected with: 0=error, 1..999=progress, 1000=success."
3840
);
3941
context.emit_event(EventType::SecurejoinInviterProgress {
4042
contact_id,

src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl TestContext {
754754
pub async fn add_or_lookup_address_contact(&self, other: &TestContext) -> Contact {
755755
let contact_id = self.add_or_lookup_address_contact_id(other).await;
756756
let contact = Contact::get_by_id(&self.ctx, contact_id).await.unwrap();
757-
debug_assert_eq!(contact.is_key_contact(), false);
757+
assert_eq!(contact.is_key_contact(), false);
758758
contact
759759
}
760760

src/tools.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,5 +801,17 @@ macro_rules! ensure_and_debug_assert_ne {
801801
};
802802
}
803803

804+
/// Logs a warning if a condition is not satisfied.
805+
/// In non-optimized builds, panics also if so.
806+
#[macro_export]
807+
macro_rules! logged_debug_assert {
808+
($ctx:expr, $cond:expr, $($arg:tt)*) => {
809+
if !$cond {
810+
warn!($ctx, $($arg)*);
811+
}
812+
debug_assert!($cond, $($arg)*);
813+
};
814+
}
815+
804816
#[cfg(test)]
805817
mod tools_tests;

0 commit comments

Comments
 (0)