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
pub use color_eyre::eyre;
#[doc(hidden)]
pub use color_eyre::Report;
#[doc(hidden)]
pub use color_eyre::Result;

/// Install the deault panic and error report hooks. Ensure rust backtrace is enabled.
///
/// # Examples
/// ```
/// use color_eyre::eyre::Result;
///
/// fn main() -> Result<()> {
///     krenz::color_eyre_install()?;
///     // ...
///     Ok(())
/// }
/// ```
pub fn color_eyre_install() -> Result<()> {
    // Install color_eyre exactly once
    if std::env::var("RUST_COLOR_EYRE_INSTALL").is_err() {
        std::env::set_var("RUST_COLOR_EYRE_INSTALL", "1");
        color_eyre::install()?;
    }

    // Enable backtraces, if not already enabled by user
    if std::env::var("RUST_BACKTRACE").is_err() {
        std::env::set_var("RUST_BACKTRACE", "1");
    }

    Ok(())
}

/// Debug print variables and line numbers with fully customizeable formatting.
///
/// # Examples
///
/// ```
/// use krenz::dump;
/// let address = 2;
/// let value = 3;
/// dump!("address = {}  value = 0b{:08b}", address, value);
/// ```
#[macro_export]
macro_rules! dump {
    () => {
        eprintln!("[{}:{}]", file!(), line!());
    };
    ($($arg:tt)*) => {
        eprintln!("[{}:{}] {}", file!(), line!(), format!($($arg)*));
    };
    ($($val:expr),+$(,)?) => {
        ($($crate::dump!($val)),+,)
    };
}

/// Debug print variables and line numbers with a simple, default single line format.
///
/// # Examples
///
/// ```
/// use krenz::dmp;
/// let address = 2;
/// let value = 3;
/// dmp!(address, value);
/// ```
#[macro_export]
macro_rules! dmp {
    ($($a:expr),*) => {
        eprintln!(concat!("[", file!(), ":", line!(), "] ", $(stringify!($a), " = {:?}  "),*), $($a),*);
    }
}