summaryrefslogtreecommitdiffstats
path: root/openpgp/src/macros.rs
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2018-10-09 14:00:06 +0200
committerNeal H. Walfield <neal@pep.foundation>2018-10-09 14:00:06 +0200
commit5db37bd753f49ed5c7156d7aa875afcaa6cda63a (patch)
tree6e0b16c901b0eb5ab271f1a58adbfd5cee48f1bb /openpgp/src/macros.rs
parenta662edea2c7568c992a8fdd9a7f1ff652dc2a15f (diff)
openpgp: Make tracing less invasive.
- Add a few macros to simplify tracing. - Use them.
Diffstat (limited to 'openpgp/src/macros.rs')
-rw-r--r--openpgp/src/macros.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/openpgp/src/macros.rs b/openpgp/src/macros.rs
new file mode 100644
index 00000000..6317a0fe
--- /dev/null
+++ b/openpgp/src/macros.rs
@@ -0,0 +1,52 @@
+use std::cmp;
+
+macro_rules! trace {
+ ( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
+ if $TRACE {
+ eprintln!($fmt, $($pargs),*);
+ }
+ };
+ ( $TRACE:expr, $fmt:expr ) => {
+ trace!($TRACE, $fmt, );
+ };
+}
+
+// Converts an indentation level to whitespace.
+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 ) => {
+ // Currently, Rust doesn't support $( ... ) in a nested
+ // macro's definition. See:
+ // https://users.rust-lang.org/t/nested-macros-issue/8348/2
+ 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)) };
+ }
+ }
+}