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
#[cfg(test)]
use std::cell::Cell;

#[cfg(test)]
thread_local!(pub static ENABLED: Cell<u32> = Cell::new(0));

#[cfg(test)]
#[macro_export]
macro_rules! debug {
    ($($arg:tt)*) => (
        ::debug::ENABLED.with(|slot| {
            if slot.get() != 0 {
                println!("{}", format_args!($($arg)+));
            }
        })
    )
}

#[cfg(not(test))]
#[macro_export]
macro_rules! debug {
    ($($arg:tt)*) => ( () )
}

#[cfg(test)]
pub struct Logger {
    _x: (),
}

#[cfg(test)]
impl Logger {
    pub fn new() -> Logger {
        ENABLED.with(|slot| slot.set(slot.get() + 1));
        Logger { _x: () }
    }
}

#[cfg(test)]
impl Drop for Logger {
    fn drop(&mut self) {
        ENABLED.with(|slot| slot.set(slot.get() - 1));
    }
}