use std::cmp;
macro_rules! trace {
( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
if $TRACE {
eprintln!($fmt, $($pargs),*);
}
};
( $TRACE:expr, $fmt:expr ) => {
trace!($TRACE, $fmt, );
};
}
pub(crate) fn indent(i: usize) -> &'static str {
let s = " ";
&s[0..cmp::min(i, s.len())]
}
macro_rules! tracer {
( $TRACE:expr, $func:expr, $indent:expr ) => {
macro_rules! t {
( $fmt:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, $fmt) };
( $fmt:expr, $a:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a)) };
( $fmt:expr, $a:expr, $b:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e, $f)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) =>
{ trace!($TRACE, "{}{}: {}", ::macros::indent($indent as usize), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) };
}
}
}
#[allow(unused_macros)]
macro_rules! time_it {
( $label:expr, $ms_threshold:expr, $body:expr ) => {{
use std::time::{SystemTime, Duration};
struct Timer {
start: SystemTime,
};
impl Drop for Timer {
fn drop(&mut self) {
let elapsed = self.start.elapsed();
if elapsed.clone().unwrap_or(Duration::from_millis($ms_threshold))
>= Duration::from_millis($ms_threshold)
{
if $label.len() > 0 {
eprint!("{}:", $label);
}
eprintln!("{}:{}: {:?}", file!(), line!(), elapsed);
}
}
}
let _start = Timer { start: SystemTime::now() };
$body
}};
( $label:expr, $body:expr ) => {
time_it!($label, 0, $body)
};
( $body:expr ) => {
time_it!("", $body)
};
}