diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-05-16 18:52:41 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-05-17 20:28:02 +0200 |
commit | 1c723a9a0eaadfd06b72732d4baaa0bc08894587 (patch) | |
tree | e051cf8b31a595fd2e7d0e835ac05bdfbe849f97 /libimagutil | |
parent | 38a8772d690e1864cb933798d492c294773cb478 (diff) |
Move trace_error() functionality from libimagutil to libimagerror
Diffstat (limited to 'libimagutil')
-rw-r--r-- | libimagutil/Cargo.toml | 1 | ||||
-rw-r--r-- | libimagutil/src/lib.rs | 2 | ||||
-rw-r--r-- | libimagutil/src/trace.rs | 76 |
3 files changed, 0 insertions, 79 deletions
diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index efcbff44..96638623 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -7,5 +7,4 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"] lazy_static = "0.1.15" log = "0.3" regex = "0.1" -ansi_term = "0.7" diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index e0d7a3ce..bde49b00 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -16,9 +16,7 @@ #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; extern crate regex; -extern crate ansi_term; pub mod ismatch; pub mod key_value_split; -pub mod trace; pub mod variants; diff --git a/libimagutil/src/trace.rs b/libimagutil/src/trace.rs deleted file mode 100644 index e04b292f..00000000 --- a/libimagutil/src/trace.rs +++ /dev/null @@ -1,76 +0,0 @@ -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()); - } -} - |