Files
continuwuity/src/core/debug.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
2.6 KiB
Rust
Raw Normal View History

2024-11-14 04:31:29 +00:00
#![allow(clippy::disallowed_macros)]
2024-07-10 06:35:11 +00:00
use std::{any::Any, panic};
2024-05-09 15:59:08 -07:00
2024-09-01 01:53:22 +00:00
// Export debug proc_macros
2024-12-14 21:58:01 -05:00
pub use conduwuit_macros::recursion_depth;
2024-07-25 01:41:31 +00:00
2024-09-01 01:53:22 +00:00
// Export all of the ancillary tools from here as well.
pub use crate::{result::DebugInspect, utils::debug::*};
2024-05-27 20:05:33 +00:00
2024-04-21 22:41:47 -07:00
/// Log event at given level in debug-mode (when debug-assertions are enabled).
/// In release-mode it becomes DEBUG level, and possibly subject to elision.
///
/// Release-mode can be simulated in debug-mode builds by enabling the feature
/// 'dev_release_log_level'.
2024-04-21 22:41:47 -07:00
#[macro_export]
macro_rules! debug_event {
( $level:expr, $($x:tt)+ ) => {
2024-08-01 08:41:47 +00:00
if $crate::debug::logging() {
2024-12-30 12:28:18 +00:00
::tracing::event!( $level, _debug = true, $($x)+ )
2024-04-21 22:41:47 -07:00
} else {
2024-07-09 03:31:58 +00:00
::tracing::debug!( $($x)+ )
2024-04-21 22:41:47 -07:00
}
}
}
2024-04-19 20:48:47 -07:00
/// Log message at the ERROR level in debug-mode (when debug-assertions are
/// enabled). In release-mode it becomes DEBUG level, and possibly subject to
2024-04-19 20:48:47 -07:00
/// elision.
#[macro_export]
macro_rules! debug_error {
( $($x:tt)+ ) => {
2024-07-09 03:31:58 +00:00
$crate::debug_event!(::tracing::Level::ERROR, $($x)+ )
2024-04-19 20:48:47 -07:00
}
}
/// Log message at the WARN level in debug-mode (when debug-assertions are
/// enabled). In release-mode it becomes DEBUG level, and possibly subject to
2024-04-19 20:48:47 -07:00
/// elision.
#[macro_export]
macro_rules! debug_warn {
( $($x:tt)+ ) => {
2024-07-09 03:31:58 +00:00
$crate::debug_event!(::tracing::Level::WARN, $($x)+ )
2024-04-19 20:48:47 -07:00
}
}
/// Log message at the INFO level in debug-mode (when debug-assertions are
/// enabled). In release-mode it becomes DEBUG level, and possibly subject to
2024-04-19 20:48:47 -07:00
/// elision.
#[macro_export]
macro_rules! debug_info {
( $($x:tt)+ ) => {
2024-07-09 03:31:58 +00:00
$crate::debug_event!(::tracing::Level::INFO, $($x)+ )
2024-04-19 20:48:47 -07:00
}
}
2024-05-09 15:59:08 -07:00
pub fn set_panic_trap() {
let next = panic::take_hook();
panic::set_hook(Box::new(move |info| {
panic_handler(info, &next);
}));
}
#[inline(always)]
2024-06-16 00:12:56 +00:00
#[allow(deprecated_in_future)]
fn panic_handler(info: &panic::PanicHookInfo<'_>, next: &dyn Fn(&panic::PanicHookInfo<'_>)) {
2024-05-09 15:59:08 -07:00
trap();
next(info);
}
#[inline(always)]
pub fn trap() {
#[cfg(core_intrinsics)]
//SAFETY: embeds llvm intrinsic for hardware breakpoint
unsafe {
std::intrinsics::breakpoint();
}
#[cfg(all(not(core_intrinsics), target_arch = "x86_64"))]
//SAFETY: embeds instruction for hardware breakpoint
unsafe {
std::arch::asm!("int3");
}
}
2024-07-10 06:35:11 +00:00
#[must_use]
pub fn panic_str(p: &Box<dyn Any + Send>) -> &'static str {
p.downcast_ref::<&str>().copied().unwrap_or_default()
}
2024-07-05 08:40:02 +00:00
#[inline(always)]
#[must_use]
2024-09-29 09:20:17 +00:00
pub fn rttype_name<T: ?Sized>(_: &T) -> &'static str { type_name::<T>() }
2024-08-04 06:03:58 +00:00
#[inline(always)]
#[must_use]
2024-09-29 09:20:17 +00:00
pub fn type_name<T: ?Sized>() -> &'static str { std::any::type_name::<T>() }
2024-08-01 08:41:47 +00:00
#[must_use]
#[inline]
pub const fn logging() -> bool {
cfg!(debug_assertions) && cfg!(not(feature = "dev_release_log_level"))
}