Add error tracing utility
This commit is contained in:
parent
78cda89524
commit
d8ae741f31
2 changed files with 47 additions and 0 deletions
|
@ -2,4 +2,5 @@
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
|
||||||
pub mod key_value_split;
|
pub mod key_value_split;
|
||||||
|
pub mod trace;
|
||||||
pub mod variants;
|
pub mod variants;
|
||||||
|
|
46
libimagutil/src/trace.rs
Normal file
46
libimagutil/src/trace.rs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
use std::error::Error;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::io::stderr;
|
||||||
|
|
||||||
|
pub fn trace_error(e: &Error) {
|
||||||
|
print_trace_maxdepth(count_error_causes(e), e, ::std::u64::MAX);
|
||||||
|
write!(stderr(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn trace_error_maxdepth(e: &Error, max: u64) {
|
||||||
|
let n = count_error_causes(e);
|
||||||
|
write!(stderr(), "{}/{} Levels of errors will be printed", (if max > n { n } else { max }), n);
|
||||||
|
print_trace_maxdepth(n, e, max);
|
||||||
|
write!(stderr(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
print_trace_maxdepth(idx - 1, e.cause().unwrap(), max);
|
||||||
|
write!(stderr(), " -- caused:");
|
||||||
|
}
|
||||||
|
write!(stderr(), "Error {:>4} : {}", idx, e.description());
|
||||||
|
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!("Error {:>4} : {}", idx, e.description());
|
||||||
|
if e.cause().is_some() {
|
||||||
|
debug!(" -- caused by:");
|
||||||
|
print_trace_dbg(idx + 1, e.cause().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue