Merge pull request #413 from matthiasbeyer/libimagutil/error-trace-red

Libimagutil/error trace red
This commit is contained in:
Matthias Beyer 2016-05-17 00:23:34 +02:00
commit 4a6ab66c32
3 changed files with 9 additions and 4 deletions

View file

@ -7,4 +7,5 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
lazy_static = "0.1.15"
log = "0.3"
regex = "0.1"
ansi_term = "0.7"

View file

@ -16,6 +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;

View file

@ -2,6 +2,8 @@ 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
@ -29,8 +31,9 @@ pub fn trace_error(e: &Error) {
/// 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);
write!(stderr(),
"{}/{} Levels of errors will be printed\n", (if max > n { n } else { max }), n).ok();
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();
}
@ -55,7 +58,7 @@ fn print_trace_maxdepth(idx: u64, e: &Error, max: u64) -> Option<&Error> {
} else {
write!(stderr(), "\n").ok();
}
write!(stderr(), "ERROR[{:>4}]: {}", idx, e.description()).ok();
write!(stderr(), "{}: {}", Red.paint(format!("ERROR[{:>4}]", idx)), e.description()).ok();
e.cause()
}
@ -65,7 +68,7 @@ fn count_error_causes(e: &Error) -> u64 {
}
fn print_trace_dbg(idx: u64, e: &Error) {
debug!("ERROR[{:>4}]: {}", idx, e.description());
debug!("{}: {}", Red.blink().paint(format!("ERROR[{:>4}]", idx)), e.description());
if e.cause().is_some() {
print_trace_dbg(idx + 1, e.cause().unwrap());
}