2022-04-24 15:20:25 +02:00
|
|
|
/// Write something to the logger. This defaults to STDERR unless the user is running Windows and a
|
|
|
|
/// debugger has been attached, in which case `OutputDebugString()` will be used instead.
|
2022-01-26 11:26:30 +01:00
|
|
|
///
|
2022-04-24 15:20:25 +02:00
|
|
|
/// The logger's behavior can be controlled by setting the `NIH_LOG` environment variable to:
|
|
|
|
///
|
|
|
|
/// - `stderr`, in which case the log output always gets written to STDERR.
|
|
|
|
/// - `windbg` (only on Windows), in which case the output always gets logged using
|
|
|
|
/// `OutputDebugString()`.
|
|
|
|
/// - A file path, in which case the output gets appended to the end of that file which will be
|
|
|
|
/// created if necessary.
|
2022-01-26 11:34:47 +01:00
|
|
|
#[macro_export]
|
2022-01-26 11:26:30 +01:00
|
|
|
macro_rules! nih_log {
|
2022-04-24 15:20:25 +02:00
|
|
|
($($args:tt)*) => (
|
|
|
|
$crate::log::info!($($args)*)
|
2022-01-26 11:26:30 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-24 15:29:50 +02:00
|
|
|
/// The same as `nih_log!()`, but with source and thread information. Like the
|
|
|
|
/// `nih_debug_assert*!()` macros, this is only shown when compiling in debug mode.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nih_trace {
|
|
|
|
($($args:tt)*) => (
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::trace!($($args)*))
|
2022-04-24 15:29:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-26 15:43:24 +02:00
|
|
|
/// Analogues to the `dbg!()` macro, but respecting the `NIH_LOG` environment variable and with all
|
|
|
|
/// of the same logging features as the other `nih_*!()` macros. Like the `nih_debug_assert*!()`
|
|
|
|
/// macros, this is only shown when compiling in debug mode, but the macro will still return the
|
|
|
|
/// value in non-debug modes.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nih_dbg {
|
|
|
|
() => {
|
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(""));
|
|
|
|
};
|
|
|
|
($val:expr $(,)?) => {
|
|
|
|
// Match here acts as a let-binding: https://stackoverflow.com/questions/48732263/why-is-rusts-assert-eq-implemented-using-a-match/48732525#48732525
|
|
|
|
match $val {
|
|
|
|
tmp => {
|
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!("{} = {:#?}", stringify!($val), &tmp));
|
|
|
|
tmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($($val:expr),+ $(,)?) => { ($($crate::nih_dbg!($val)),+,) };
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:48 +01:00
|
|
|
/// A `debug_assert!()` analogue that prints the error with line number information instead of
|
|
|
|
/// panicking.
|
2022-01-26 11:26:30 +01:00
|
|
|
///
|
|
|
|
/// TODO: Detect if we're running under a debugger, and trigger a break if we are
|
2022-01-26 11:34:47 +01:00
|
|
|
#[macro_export]
|
2022-01-26 11:26:30 +01:00
|
|
|
macro_rules! nih_debug_assert {
|
2022-01-28 13:32:37 +01:00
|
|
|
($cond:expr $(,)?) => (
|
2022-01-26 11:26:30 +01:00
|
|
|
if cfg!(debug_assertions) && !$cond {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", stringify!($cond))));
|
2022-01-26 11:26:30 +01:00
|
|
|
}
|
|
|
|
);
|
2022-01-28 14:47:26 +01:00
|
|
|
($cond:expr, $format:expr $(, $($args:tt)*)?) => (
|
2022-01-26 11:26:30 +01:00
|
|
|
if cfg!(debug_assertions) && !$cond {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", stringify!($cond), ", ", $format), $($($args)*)?));
|
2022-01-26 11:26:30 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-01-26 11:30:48 +01:00
|
|
|
|
2022-01-26 20:51:34 +01:00
|
|
|
/// An unconditional debug assertion failure, for if the condition has already been checked
|
|
|
|
/// elsewhere.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! nih_debug_assert_failure {
|
|
|
|
() => (
|
|
|
|
if cfg!(debug_assertions) {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!("Debug assertion failed"));
|
2022-01-26 20:51:34 +01:00
|
|
|
}
|
|
|
|
);
|
2022-01-28 14:47:26 +01:00
|
|
|
($format:expr $(, $($args:tt)*)?) => (
|
2022-01-26 20:51:34 +01:00
|
|
|
if cfg!(debug_assertions) {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", $format), $($($args)*)?));
|
2022-01-26 20:51:34 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:48 +01:00
|
|
|
/// A `debug_assert_eq!()` analogue that prints the error with line number information instead of
|
|
|
|
/// panicking.
|
2022-01-26 11:34:47 +01:00
|
|
|
#[macro_export]
|
2022-01-26 11:30:48 +01:00
|
|
|
macro_rules! nih_debug_assert_eq {
|
2022-01-28 13:32:37 +01:00
|
|
|
($left:expr, $right:expr $(,)?) => (
|
2022-01-26 11:30:48 +01:00
|
|
|
if cfg!(debug_assertions) && $left != $right {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", stringify!($left), " != ", stringify!($right))));
|
2022-01-26 11:30:48 +01:00
|
|
|
}
|
|
|
|
);
|
2022-03-20 12:44:29 +01:00
|
|
|
($left:expr, $right:expr, $format:expr $(, $($args:tt)*)?) => (
|
2022-01-26 11:30:48 +01:00
|
|
|
if cfg!(debug_assertions) && $left != $right {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", stringify!($left), " != ", stringify!($right), ", ", $format), $($($args)*)?));
|
2022-01-26 11:30:48 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:29:14 +01:00
|
|
|
/// A `debug_assert_ne!()` analogue that prints the error with line number information instead of
|
2022-01-26 11:30:48 +01:00
|
|
|
/// panicking.
|
2022-01-26 11:34:47 +01:00
|
|
|
#[macro_export]
|
2022-02-02 22:29:14 +01:00
|
|
|
macro_rules! nih_debug_assert_ne {
|
2022-01-28 13:32:37 +01:00
|
|
|
($left:expr, $right:expr $(,)?) => (
|
2022-01-26 11:30:48 +01:00
|
|
|
if cfg!(debug_assertions) && $left == $right {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", stringify!($left), " == ", stringify!($right))));
|
2022-01-26 11:30:48 +01:00
|
|
|
}
|
|
|
|
);
|
2022-03-20 12:44:29 +01:00
|
|
|
($left:expr, $right:expr, $format:expr $(, $($args:tt)*)?) => (
|
2022-01-26 11:30:48 +01:00
|
|
|
if cfg!(debug_assertions) && $left == $right {
|
2022-04-24 15:42:27 +02:00
|
|
|
$crate::util::permit_alloc(|| $crate::log::debug!(concat!("Debug assertion failed: ", stringify!($left), " == ", stringify!($right), ", ", $format), $($($args)*)?));
|
2022-01-26 11:30:48 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|