1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/// Prints to the standard output, with a newline, in debug mode.
///
/// Equivalent to the `println!` macro, but does nothing when not in debug mode.
///
/// # Example
///
/// ```
/// use dbgprint::dbgprintln;
/// dbgprintln!("Hello, {}!", "world"); // prints "Hello, world!" in debug mode
/// ```
#[macro_export]
macro_rules! dbgprintln {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
println!($($arg)*);
}
};
}
/// Prints to the standard output in debug mode.
///
/// Equivalent to the `print!` macro, but does nothing when not in debug mode.
///
/// # Example
///
/// ```
/// use dbgprint::dbgprint;
/// dbgprint!("Hello, {}!", "world"); // prints "Hello, world!" in debug mode
/// ```
#[macro_export]
macro_rules! dbgprint {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
print!($($arg)*);
}
};
}
/// Prints to the standard error, with a newline, in debug mode.
///
/// Equivalent to the `eprintln!` macro, but does nothing when not in debug mode.
///
/// # Example
///
/// ```
/// use dbgprint::dbgeprintln;
/// dbgeprintln!("Error: {}", "something went wrong"); // prints "Error: something went wrong" to stderr in debug mode
/// ```
#[macro_export]
macro_rules! dbgeprintln {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
eprintln!($($arg)*);
}
};
}
/// Prints to the standard error in debug mode.
///
/// Equivalent to the `eprint!` macro, but does nothing when not in debug mode.
///
/// # Example
///
/// ```
/// use dbgprint::dbgeprint;
/// dbgeprint!("Error: {}", "something went wrong"); // prints "Error: something went wrong" to stderr in debug mode
/// ```
#[macro_export]
macro_rules! dbgeprint {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
eprint!($($arg)*);
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dbgprint() {
// There is no easy way to test the output of these macros
// without adding additional dependencies, so we just call
// them to make sure they compile correctly.
dbgprint!("Test argument: {}", 1);
dbgprintln!("Test argument: {}", 1);
dbgeprint!("Some other test: {}", "text");
dbgeprintln!("Some other test: {}", "text");
}
}