imag/libimagerror/src/trace.rs

90 lines
2.9 KiB
Rust
Raw Normal View History

2016-02-06 23:01:36 +00:00
use std::error::Error;
use std::io::Write;
use std::io::stderr;
2016-05-15 15:47:01 +00:00
use ansi_term::Colour::Red;
2016-02-09 19:16:21 +00:00
/// 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>
/// ```
2016-02-06 23:01:36 +00:00
pub fn trace_error(e: &Error) {
print_trace_maxdepth(count_error_causes(e), e, ::std::u64::MAX);
write!(stderr(), "\n").ok();
2016-02-06 23:01:36 +00:00
}
/// Convenience function: calls `trace_error()` with `e` and afterwards `std::process::exit()`
/// with `code`
2016-07-16 09:37:52 +00:00
pub fn trace_error_exit(e: &Error, code: i32) -> ! {
use std::process::exit;
debug!("Tracing error...");
trace_error(e);
debug!("Calling exit()");
exit(code);
}
2016-02-09 19:16:21 +00:00
/// 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.
2016-02-06 23:01:36 +00:00
pub fn trace_error_maxdepth(e: &Error, max: u64) {
let n = count_error_causes(e);
2016-05-15 15:47:01 +00:00
let msg = Red.blink().paint(format!("{}/{} Levels of errors will be printed\n",
(if max > n { n } else { max }), n));
write!(stderr(), "{}", msg).ok();
2016-02-06 23:01:36 +00:00
print_trace_maxdepth(n, e, max);
write!(stderr(), "").ok();
2016-02-06 23:01:36 +00:00
}
2016-02-09 19:16:21 +00:00
/// Print an Error type and its cause recursively with the debug!() macro
///
/// Output is the same as for `trace_error()`.
2016-02-06 23:01:36 +00:00
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 {
2016-08-04 12:09:06 +00:00
e.cause().map(|cause| {
match print_trace_maxdepth(idx - 1, cause, max) {
None => write!(stderr(), "\n").ok(),
Some(_) => write!(stderr(), " -- caused:\n").ok(),
};
});
2016-03-25 12:07:46 +00:00
} else {
write!(stderr(), "\n").ok();
2016-02-06 23:01:36 +00:00
}
2016-05-15 15:47:01 +00:00
write!(stderr(), "{}: {}", Red.paint(format!("ERROR[{:>4}]", idx)), e.description()).ok();
2016-02-06 23:01:36 +00:00
e.cause()
}
/// Count errors in `Error::cause()` recursively
2016-02-06 23:01:36 +00:00
fn count_error_causes(e: &Error) -> u64 {
2016-08-04 12:09:22 +00:00
1 + e.cause().map(|c| count_error_causes(c)).unwrap_or(0)
2016-02-06 23:01:36 +00:00
}
fn print_trace_dbg(idx: u64, e: &Error) {
2016-05-15 15:47:01 +00:00
debug!("{}: {}", Red.blink().paint(format!("ERROR[{:>4}]", idx)), e.description());
2016-02-06 23:01:36 +00:00
if e.cause().is_some() {
2016-08-04 12:10:03 +00:00
e.cause().map(|c| print_trace_dbg(idx + 1, c));
2016-02-06 23:01:36 +00:00
}
}