From 78cda89524743630eb5a218c1a44b272697337e1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 7 Feb 2016 00:05:43 +0100 Subject: [PATCH 1/3] Add dependency: log = 0.3.5 --- libimagutil/Cargo.toml | 1 + libimagutil/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index 341d5aa1..6e442b18 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" authors = ["Matthias Beyer "] [dependencies] +log = "0.3.5" regex = "0.1.47" diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index d26e1d7e..788f28cb 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -1,3 +1,4 @@ +#[macro_use] extern crate log; extern crate regex; pub mod key_value_split; From d8ae741f31af2ac719bd089c249a034486ef242a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 7 Feb 2016 00:01:36 +0100 Subject: [PATCH 2/3] Add error tracing utility --- libimagutil/src/lib.rs | 1 + libimagutil/src/trace.rs | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 libimagutil/src/trace.rs diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 788f28cb..a7586143 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -2,4 +2,5 @@ extern crate regex; pub mod key_value_split; +pub mod trace; pub mod variants; diff --git a/libimagutil/src/trace.rs b/libimagutil/src/trace.rs new file mode 100644 index 00000000..af949e7f --- /dev/null +++ b/libimagutil/src/trace.rs @@ -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()); + } +} + From 0a8eaa14116041dcd00e1474c1baed42e2530c68 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Feb 2016 20:16:21 +0100 Subject: [PATCH 3/3] Add documentation of functions --- libimagutil/src/trace.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libimagutil/src/trace.rs b/libimagutil/src/trace.rs index af949e7f..cdb65aa5 100644 --- a/libimagutil/src/trace.rs +++ b/libimagutil/src/trace.rs @@ -2,11 +2,31 @@ use std::error::Error; use std::io::Write; use std::io::stderr; +/// 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 : +/// ``` pub fn trace_error(e: &Error) { print_trace_maxdepth(count_error_causes(e), e, ::std::u64::MAX); write!(stderr(), ""); } +/// 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); write!(stderr(), "{}/{} Levels of errors will be printed", (if max > n { n } else { max }), n); @@ -14,6 +34,9 @@ pub fn trace_error_maxdepth(e: &Error, max: u64) { write!(stderr(), ""); } +/// 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); }