1use std::cmp;
2
3macro_rules! trace {
4 ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
5 if $TRACE {
6 eprintln!($fmt, $($pargs),*);
7 }
8 };
9 ( $TRACE:expr, $fmt:expr ) => {
10 trace!($TRACE, $fmt, );
11 };
12}
13
14pub(crate) fn indent(i: isize) -> &'static str {
16 let s = " ";
17 &s[0..cmp::min(usize::try_from(i).unwrap_or(0), s.len())]
18}
19
20macro_rules! tracer {
21 ( $TRACE:expr, $func:expr ) => {
22 tracer!($TRACE, $func, 0)
23 };
24 ( $TRACE:expr, $func:expr, $indent:expr ) => {
25 macro_rules! t {
29 ( $fmt:expr ) =>
30 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, $fmt) };
31 ( $fmt:expr, $a:expr ) =>
32 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a)) };
33 ( $fmt:expr, $a:expr, $b:expr ) =>
34 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b)) };
35 ( $fmt:expr, $a:expr, $b:expr, $c:expr ) =>
36 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c)) };
37 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) =>
38 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d)) };
39 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) =>
40 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e)) };
41 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) =>
42 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f)) };
43 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) =>
44 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) };
45 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) =>
46 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) };
47 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) =>
48 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) };
49 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) =>
50 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) };
51 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) =>
52 { trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) };
53 }
54 }
55}
56
57
58#[allow(unused_macros)]
77macro_rules! time_it {
78 ( $label:expr, $ms_threshold:expr, $body:expr ) => {{
79 use std::time::{SystemTime, Duration};
80
81 struct Timer {
84 start: SystemTime,
85 };
86 impl Drop for Timer {
87 fn drop(&mut self) {
88 let elapsed = self.start.elapsed();
89 if elapsed.clone().unwrap_or(Duration::from_millis($ms_threshold))
90 >= Duration::from_millis($ms_threshold)
91 {
92 if $label.len() > 0 {
93 eprint!("{}:", $label);
94 }
95 eprintln!("{}:{}: {:?}", file!(), line!(), elapsed);
96 }
97 }
98 }
99
100 let _start = Timer { start: SystemTime::now() };
101 $body
102 }};
103 ( $label:expr, $body:expr ) => {
104 time_it!($label, 0, $body)
105 };
106 ( $body:expr ) => {
107 time_it!("", $body)
108 };
109}