Files
continuwuity/src/core/debug.rs
T

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

91 lines
2.4 KiB
Rust
Raw Normal View History

2024-07-10 06:35:11 +00:00
use std::{any::Any, panic};
2024-05-09 15:59:08 -07:00
2024-07-25 01:41:31 +00:00
/// Export debug proc_macros
pub use conduit_macros::recursion_depth;
2024-05-27 20:05:33 +00:00
/// Export all of the ancillary tools from here as well.
pub use crate::utils::debug::*;
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)+ ) => {
if cfg!(debug_assertions) && cfg!(not(feature = "dev_release_log_level")) {
2024-07-09 03:31:58 +00:00
::tracing::event!( $level, $($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::PanicInfo<'_>, next: &dyn Fn(&panic::PanicInfo<'_>)) {
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
#[cfg(debug_assertions)]
#[inline(always)]
#[must_use]
pub fn type_name<T>(_: &T) -> &'static str { std::any::type_name::<T>() }