Files
continuwuity/src/core/debug.rs
T

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

117 lines
2.9 KiB
Rust
Raw Normal View History

2024-11-14 04:31:29 +00:00
#![allow(clippy::disallowed_macros)]
2025-02-02 22:13:27 +00:00
use std::{any::Any, env, panic, sync::LazyLock};
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;
2025-01-26 04:46:10 +00:00
use tracing::Level;
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.
2024-04-21 22:41:47 -07:00
#[macro_export]
2025-04-04 23:04:13 +00:00
#[collapse_debuginfo(yes)]
2024-04-21 22:41:47 -07:00
macro_rules! debug_event {
( $level:expr_2021, $($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
2025-01-26 04:46:10 +00:00
pub const INFO_SPAN_LEVEL: Level = if cfg!(debug_assertions) {
Level::INFO
} else {
Level::DEBUG
};
2025-02-02 22:13:27 +00:00
pub static DEBUGGER: LazyLock<bool> =
LazyLock::new(|| env::var("_").unwrap_or_default().ends_with("gdb"));
#[cfg_attr(debug_assertions, ctor::ctor)]
2025-02-02 22:13:27 +00:00
#[cfg_attr(not(debug_assertions), allow(dead_code))]
fn set_panic_trap() {
if !*DEBUGGER {
return;
}
2024-05-09 15:59:08 -07:00
let next = panic::take_hook();
panic::set_hook(Box::new(move |info| {
panic_handler(info, &next);
}));
}
2025-02-02 22:13:27 +00:00
#[cold]
#[inline(never)]
2024-06-16 00:12:56 +00:00
#[allow(deprecated_in_future)]
2025-02-02 22:13:27 +00:00
pub 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 {
2025-08-22 00:51:54 +01:00
(**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]
2024-12-30 17:01:27 +00:00
pub const fn logging() -> bool { cfg!(debug_assertions) }