Debian Codegraph @

src/log.rs

raw
1 #![allow(unused_macros)]
2
3 use std::cell::RefCell;
4
5 // Like eprintln!
6 macro_rules! log {
7 ($dst:expr $(,)?) => (
8 eprintln!("{}", $dst)
9 );
10 ($dst:expr, $($arg:tt)*) => (
11 eprintln!("{}", std::format!($dst, $($arg)*))
12 );
13 }
14
15 // The indent level. It is increased with each call to tracer and
16 // decremented when the tracer goes out of scope.
17 thread_local! {
18 pub static INDENT_LEVEL: RefCell<usize> = RefCell::new(0);
19 }
20
21 // Like eprintln!, but the first argument is a boolean, which
22 // indicates if the string should actually be printed.
23 macro_rules! trace {
24 ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
25 if $TRACE {
26 let indent_level = crate::log::INDENT_LEVEL.with(|i| {
27 *i.borrow()
28 });
29
30 let ws = " ";
31
32 log!("{}{}",
33 &ws[0..std::cmp::min(ws.len(), std::cmp::max(1, indent_level) - 1)],
34 format!($fmt, $($pargs),*));
35 }
36 };
37 ( $TRACE:expr, $fmt:expr ) => {
38 trace!($TRACE, $fmt, );
39 };
40 }
41
42 macro_rules! tracer {
43 ( $TRACE:expr, $func:expr ) => {
44 // Currently, Rust doesn't support $( ... ) in a nested
45 // macro's definition. See:
46 // https://users.rust-lang.org/t/nested-macros-issue/8348/2
47 #[allow(unused)]
48 macro_rules! t {
49 ( $fmt:expr ) =>
50 { trace!($TRACE, "{}: {}", $func, $fmt) };
51 ( $fmt:expr, $a:expr ) =>
52 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a)) };
53 ( $fmt:expr, $a:expr, $b:expr ) =>
54 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b)) };
55 ( $fmt:expr, $a:expr, $b:expr, $c:expr ) =>
56 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c)) };
57 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) =>
58 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d)) };
59 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) =>
60 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e)) };
61 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) =>
62 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f)) };
63 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) =>
64 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) };
65 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) =>
66 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) };
67 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) =>
68 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) };
69 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) =>
70 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) };
71 ( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) =>
72 { trace!($TRACE, "{}: {}", $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) };
73 }
74 struct Indent {}
75 impl Indent {
76 fn init() -> Self {
77 crate::log::INDENT_LEVEL.with(|i| {
78 i.replace_with(|i| *i + 1);
79 });
80 Indent {}
81 }
82 }
83 impl Drop for Indent {
84 fn drop(&mut self) {
85 crate::log::INDENT_LEVEL.with(|i| {
86 i.replace_with(|i| *i - 1);
87 });
88 }
89 }
90 let _indent = Indent::init();
91 }
92 }