summaryrefslogtreecommitdiffstats
path: root/libimagerror
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-05-16 18:52:41 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-05-17 20:28:02 +0200
commit1c723a9a0eaadfd06b72732d4baaa0bc08894587 (patch)
treee051cf8b31a595fd2e7d0e835ac05bdfbe849f97 /libimagerror
parent38a8772d690e1864cb933798d492c294773cb478 (diff)
Move trace_error() functionality from libimagutil to libimagerror
Diffstat (limited to 'libimagerror')
-rw-r--r--libimagerror/Cargo.toml2
-rw-r--r--libimagerror/src/lib.rs4
-rw-r--r--libimagerror/src/trace.rs76
3 files changed, 82 insertions, 0 deletions
diff --git a/libimagerror/Cargo.toml b/libimagerror/Cargo.toml
index bd769698..2227ce9e 100644
--- a/libimagerror/Cargo.toml
+++ b/libimagerror/Cargo.toml
@@ -4,3 +4,5 @@ version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
+log = "0.3"
+ansi_term = "0.7"
diff --git a/libimagerror/src/lib.rs b/libimagerror/src/lib.rs
index 9f79f549..d7f374a7 100644
--- a/libimagerror/src/lib.rs
+++ b/libimagerror/src/lib.rs
@@ -1 +1,5 @@
+#[macro_use] extern crate log;
+extern crate ansi_term;
+
pub mod error_gen;
+pub mod trace;
diff --git a/libimagerror/src/trace.rs b/libimagerror/src/trace.rs
new file mode 100644
index 00000000..e04b292f
--- /dev/null
+++ b/libimagerror/src/trace.rs
@@ -0,0 +1,76 @@
+use std::error::Error;
+use std::io::Write;
+use std::io::stderr;
+
+use ansi_term::Colour::Red;
+
+/// Print an Error type and its cause recursively
+///
+/// The error is printed with "Error NNNN :" as prefix, where "NNNN" is a number which increases
+/// which each recursion into the errors cause. The error description is used to visualize what
+/// failed and if there is a cause "-- caused by:" is appended, and the cause is printed on the next
+/// line.
+///
+/// Example output:
+///
+/// ```ignore
+/// Error 1 : Some error -- caused by:
+/// Error 2 : Some other error -- caused by:
+/// Error 3 : Yet another Error -- caused by:
+/// ...
+///
+/// Error <NNNN> : <Error description>
+/// ```
+pub fn trace_error(e: &Error) {
+ print_trace_maxdepth(count_error_causes(e), e, ::std::u64::MAX);
+ write!(stderr(), "\n").ok();
+}
+
+/// Print an Error type and its cause recursively, but only `max` levels
+///
+/// Output is the same as for `trace_error()`, though there are only `max` levels printed.
+pub fn trace_error_maxdepth(e: &Error, max: u64) {
+ let n = count_error_causes(e);
+ let msg = Red.blink().paint(format!("{}/{} Levels of errors will be printed\n",
+ (if max > n { n } else { max }), n));
+ write!(stderr(), "{}", msg).ok();
+ print_trace_maxdepth(n, e, max);
+ write!(stderr(), "").ok();
+}
+
+/// Print an Error type and its cause recursively with the debug!() macro
+///
+/// Output is the same as for `trace_error()`.
+pub fn trace_error_dbg(e: &Error) {
+ print_trace_dbg(0, e);
+}
+
+/// Helper function for `trace_error()` and `trace_error_maxdepth()`.
+///
+/// Returns the cause of the last processed error in the recursion, so `None` if all errors where
+/// processed.
+fn print_trace_maxdepth(idx: u64, e: &Error, max: u64) -> Option<&Error> {
+ if e.cause().is_some() && idx > 0 {
+ match print_trace_maxdepth(idx - 1, e.cause().unwrap(), max) {
+ None => write!(stderr(), "\n").ok(),
+ Some(_) => write!(stderr(), " -- caused:\n").ok(),
+ };
+ } else {
+ write!(stderr(), "\n").ok();
+ }
+ write!(stderr(), "{}: {}", Red.paint(format!("ERROR[{:>4}]", idx)), e.description()).ok();
+ e.cause()
+}
+
+/// Count errors in `Error::cause()` recursively
+fn count_error_causes(e: &Error) -> u64 {
+ 1 + if e.cause().is_some() { count_error_causes(e.cause().unwrap()) } else { 0 }
+}
+
+fn print_trace_dbg(idx: u64, e: &Error) {
+ debug!("{}: {}", Red.blink().paint(format!("ERROR[{:>4}]", idx)), e.description());
+ if e.cause().is_some() {
+ print_trace_dbg(idx + 1, e.cause().unwrap());
+ }
+}
+