mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 17:06:35 +03:00
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 0359481ba4.
This commit is contained in:
11
src/chat.rs
11
src/chat.rs
@@ -33,6 +33,7 @@ use crate::ephemeral::{Timer as EphemeralTimer, start_chat_ephemeral_timers};
|
|||||||
use crate::events::EventType;
|
use crate::events::EventType;
|
||||||
use crate::location;
|
use crate::location;
|
||||||
use crate::log::{LogExt, error, info, warn};
|
use crate::log::{LogExt, error, info, warn};
|
||||||
|
use crate::logged_debug_assert;
|
||||||
use crate::message::{self, Message, MessageState, MsgId, Viewtype};
|
use crate::message::{self, Message, MessageState, MsgId, Viewtype};
|
||||||
use crate::mimefactory::MimeFactory;
|
use crate::mimefactory::MimeFactory;
|
||||||
use crate::mimeparser::SystemMessage;
|
use crate::mimeparser::SystemMessage;
|
||||||
@@ -1339,14 +1340,18 @@ impl ChatId {
|
|||||||
|
|
||||||
let mut ret = stock_str::e2e_available(context).await + "\n";
|
let mut ret = stock_str::e2e_available(context).await + "\n";
|
||||||
|
|
||||||
for contact_id in get_chat_contacts(context, self)
|
for &contact_id in get_chat_contacts(context, self)
|
||||||
.await?
|
.await?
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&contact_id| !contact_id.is_special())
|
.filter(|&contact_id| !contact_id.is_special())
|
||||||
{
|
{
|
||||||
let contact = Contact::get_by_id(context, *contact_id).await?;
|
let contact = Contact::get_by_id(context, contact_id).await?;
|
||||||
let addr = contact.get_addr();
|
let addr = contact.get_addr();
|
||||||
debug_assert!(contact.is_key_contact());
|
logged_debug_assert!(
|
||||||
|
context,
|
||||||
|
contact.is_key_contact(),
|
||||||
|
"get_encryption_info: contact {contact_id} is not a key-contact."
|
||||||
|
);
|
||||||
let fingerprint = contact
|
let fingerprint = contact
|
||||||
.fingerprint()
|
.fingerprint()
|
||||||
.context("Contact does not have a fingerprint in encrypted chat")?;
|
.context("Contact does not have a fingerprint in encrypted chat")?;
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ use crate::events::{Event, EventEmitter, EventType, Events};
|
|||||||
use crate::imap::{FolderMeaning, Imap, ServerMetadata};
|
use crate::imap::{FolderMeaning, Imap, ServerMetadata};
|
||||||
use crate::key::{load_self_secret_key, self_fingerprint};
|
use crate::key::{load_self_secret_key, self_fingerprint};
|
||||||
use crate::log::{info, warn};
|
use crate::log::{info, warn};
|
||||||
|
use crate::logged_debug_assert;
|
||||||
use crate::login_param::{ConfiguredLoginParam, EnteredLoginParam};
|
use crate::login_param::{ConfiguredLoginParam, EnteredLoginParam};
|
||||||
use crate::message::{self, Message, MessageState, MsgId};
|
use crate::message::{self, Message, MessageState, MsgId};
|
||||||
use crate::param::{Param, Params};
|
use crate::param::{Param, Params};
|
||||||
@@ -660,8 +661,16 @@ impl Context {
|
|||||||
/// or [`Self::emit_msgs_changed_without_msg_id`] should be used
|
/// or [`Self::emit_msgs_changed_without_msg_id`] should be used
|
||||||
/// instead of this function.
|
/// instead of this function.
|
||||||
pub fn emit_msgs_changed(&self, chat_id: ChatId, msg_id: MsgId) {
|
pub fn emit_msgs_changed(&self, chat_id: ChatId, msg_id: MsgId) {
|
||||||
debug_assert!(!chat_id.is_unset());
|
logged_debug_assert!(
|
||||||
debug_assert!(!msg_id.is_unset());
|
self,
|
||||||
|
!chat_id.is_unset(),
|
||||||
|
"emit_msgs_changed: chat_id is unset."
|
||||||
|
);
|
||||||
|
logged_debug_assert!(
|
||||||
|
self,
|
||||||
|
!msg_id.is_unset(),
|
||||||
|
"emit_msgs_changed: msg_id is unset."
|
||||||
|
);
|
||||||
|
|
||||||
self.emit_event(EventType::MsgsChanged { chat_id, msg_id });
|
self.emit_event(EventType::MsgsChanged { chat_id, msg_id });
|
||||||
chatlist_events::emit_chatlist_changed(self);
|
chatlist_events::emit_chatlist_changed(self);
|
||||||
@@ -670,7 +679,11 @@ impl Context {
|
|||||||
|
|
||||||
/// Emits a MsgsChanged event with specified chat and without message id.
|
/// Emits a MsgsChanged event with specified chat and without message id.
|
||||||
pub fn emit_msgs_changed_without_msg_id(&self, chat_id: ChatId) {
|
pub fn emit_msgs_changed_without_msg_id(&self, chat_id: ChatId) {
|
||||||
debug_assert!(!chat_id.is_unset());
|
logged_debug_assert!(
|
||||||
|
self,
|
||||||
|
!chat_id.is_unset(),
|
||||||
|
"emit_msgs_changed_without_msg_id: chat_id is unset."
|
||||||
|
);
|
||||||
|
|
||||||
self.emit_event(EventType::MsgsChanged {
|
self.emit_event(EventType::MsgsChanged {
|
||||||
chat_id,
|
chat_id,
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ use crate::key::self_fingerprint_opt;
|
|||||||
use crate::key::{DcKey, Fingerprint, SignedPublicKey};
|
use crate::key::{DcKey, Fingerprint, SignedPublicKey};
|
||||||
use crate::log::LogExt;
|
use crate::log::LogExt;
|
||||||
use crate::log::{info, warn};
|
use crate::log::{info, warn};
|
||||||
|
use crate::logged_debug_assert;
|
||||||
use crate::message::{
|
use crate::message::{
|
||||||
self, Message, MessageState, MessengerMessage, MsgId, Viewtype, rfc724_mid_exists,
|
self, Message, MessageState, MessengerMessage, MsgId, Viewtype, rfc724_mid_exists,
|
||||||
};
|
};
|
||||||
@@ -3835,7 +3836,11 @@ async fn lookup_key_contact_by_fingerprint(
|
|||||||
context: &Context,
|
context: &Context,
|
||||||
fingerprint: &str,
|
fingerprint: &str,
|
||||||
) -> Result<Option<ContactId>> {
|
) -> Result<Option<ContactId>> {
|
||||||
debug_assert!(!fingerprint.is_empty());
|
logged_debug_assert!(
|
||||||
|
context,
|
||||||
|
!fingerprint.is_empty(),
|
||||||
|
"lookup_key_contact_by_fingerprint: fingerprint is empty."
|
||||||
|
);
|
||||||
if fingerprint.is_empty() {
|
if fingerprint.is_empty() {
|
||||||
// Avoid accidentally looking up a non-key-contact.
|
// Avoid accidentally looking up a non-key-contact.
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ use crate::events::EventType;
|
|||||||
use crate::headerdef::HeaderDef;
|
use crate::headerdef::HeaderDef;
|
||||||
use crate::key::{DcKey, Fingerprint, load_self_public_key};
|
use crate::key::{DcKey, Fingerprint, load_self_public_key};
|
||||||
use crate::log::{error, info, warn};
|
use crate::log::{error, info, warn};
|
||||||
|
use crate::logged_debug_assert;
|
||||||
use crate::message::{Message, Viewtype};
|
use crate::message::{Message, Viewtype};
|
||||||
use crate::mimeparser::{MimeMessage, SystemMessage};
|
use crate::mimeparser::{MimeMessage, SystemMessage};
|
||||||
use crate::param::Param;
|
use crate::param::Param;
|
||||||
@@ -32,9 +33,10 @@ use qrinvite::QrInvite;
|
|||||||
use crate::token::Namespace;
|
use crate::token::Namespace;
|
||||||
|
|
||||||
fn inviter_progress(context: &Context, contact_id: ContactId, progress: usize) {
|
fn inviter_progress(context: &Context, contact_id: ContactId, progress: usize) {
|
||||||
debug_assert!(
|
logged_debug_assert!(
|
||||||
|
context,
|
||||||
progress <= 1000,
|
progress <= 1000,
|
||||||
"value in range 0..1000 expected with: 0=error, 1..999=progress, 1000=success"
|
"inviter_progress: contact {contact_id}, progress={progress}, but value in range 0..1000 expected with: 0=error, 1..999=progress, 1000=success."
|
||||||
);
|
);
|
||||||
context.emit_event(EventType::SecurejoinInviterProgress {
|
context.emit_event(EventType::SecurejoinInviterProgress {
|
||||||
contact_id,
|
contact_id,
|
||||||
|
|||||||
@@ -754,7 +754,7 @@ impl TestContext {
|
|||||||
pub async fn add_or_lookup_address_contact(&self, other: &TestContext) -> Contact {
|
pub async fn add_or_lookup_address_contact(&self, other: &TestContext) -> Contact {
|
||||||
let contact_id = self.add_or_lookup_address_contact_id(other).await;
|
let contact_id = self.add_or_lookup_address_contact_id(other).await;
|
||||||
let contact = Contact::get_by_id(&self.ctx, contact_id).await.unwrap();
|
let contact = Contact::get_by_id(&self.ctx, contact_id).await.unwrap();
|
||||||
debug_assert_eq!(contact.is_key_contact(), false);
|
assert_eq!(contact.is_key_contact(), false);
|
||||||
contact
|
contact
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
src/tools.rs
12
src/tools.rs
@@ -801,5 +801,17 @@ macro_rules! ensure_and_debug_assert_ne {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Logs a warning if a condition is not satisfied.
|
||||||
|
/// In non-optimized builds, panics also if so.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! logged_debug_assert {
|
||||||
|
($ctx:expr, $cond:expr, $($arg:tt)*) => {
|
||||||
|
if !$cond {
|
||||||
|
warn!($ctx, $($arg)*);
|
||||||
|
}
|
||||||
|
debug_assert!($cond, $($arg)*);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tools_tests;
|
mod tools_tests;
|
||||||
|
|||||||
Reference in New Issue
Block a user