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
//! Macros for verbose printing
//!
//! Provides macros for printing when either in debug mode or when the `--verbose` arg is passed to main

#![allow(unused_macros)]
#![allow(unused_imports)]
#![allow(dead_code)]

use once_cell::sync::OnceCell;

pub const VERBOSE_PREFIX: &str = "[verbose]";

/// [`OnceCell`] used for storage of `--verbose` status
static VERBOSE: OnceCell<bool> = OnceCell::new();

/// allows setting the `--verbose` flag's status
///
/// available in `main.rs`only
///
/// throws an error if uninitialized
#[deny(dead_code)]
pub(in crate) fn verbose_set(verbose: bool) {
    VERBOSE.set(verbose).unwrap()
}

/// returns true, if `--verbose` flag set
pub fn verbose_status() -> bool {
    *VERBOSE.get().expect(
        "verbose flag was not initialized: \
                           please set its value using verbose_set( :bool) in main.rs",
    )
}

macro_rules! dprint {
    ($($arg:tt)*) => {
        if (cfg!(debug_assertions) || crate::debug_print::verbose_status()) {
            print!("{} " crate::debug_print::VERBOSE_PREFIX);
            print!($($arg)*);
        }
    };
}

macro_rules! dprintln {
    ($($arg:tt)*) => {
        if (cfg!(debug_assertions) || crate::debug_print::verbose_status()) {
            print!("{} ", crate::debug_print::VERBOSE_PREFIX);
            println!($($arg)*);
        }
    };
}

// make macros visible
pub(crate) use dprint;
pub(crate) use dprintln;