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
use std::time::Duration;
use std::{sync::Mutex, time::Instant};

lazy_static::lazy_static! {
    static ref START: Mutex<Option<Instant>> = Mutex::new(None);
}

/// Print a message to stdout containing the file and line number, plus the number of
/// milliseconds elapsed since the last call to this macro (or 0 if this is the first call).
#[macro_export]
macro_rules! print {
    () => {
        {
            println!("[{: >8.3}ms] {}:{}", $crate::lap().as_nanos() as f64 * 1e-6, file!(), line!());
        }
    };
}

#[doc(hidden)]
pub fn lap() -> std::time::Duration {
    let mut start = START.lock().unwrap();

    let elapsed;
    match *start {
        Some(ref mut instant) => {
            elapsed = instant.elapsed();
            *instant = instant.checked_add(elapsed).unwrap();
        }
        None => {
            *start = Some(Instant::now());
            elapsed = Duration::from_secs(0);
        }
    }
    elapsed
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        crate::print!();
        crate::print!();
    }
}