1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::process::exit;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use failure::Error;
use ansi_term::Colour::Red;
struct ImagTrace<'a, T: 'a + ?Sized>(&'a T);
impl<'a, T: 'a + ?Sized> ImagTrace<'a, T> {
fn new(d: &'a T) -> ImagTrace<'a, T> {
ImagTrace(d)
}
}
impl<'a> Display for ImagTrace<'a, Error>
{
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
let _ = writeln!(fmt, "{}: {}", Red.blink().paint("ERROR[ 0]"), self.0)?;
{
for (i, cause) in self.0.iter_causes().enumerate() {
let _ = writeln!(fmt,
"{prefix}: {error}",
prefix = Red.blink().paint(format!("ERROR[{:>4}]", i + 1)),
error = cause)?;
}
}
let _ = writeln!(fmt, "{}", Red.paint("--- BACKTRACE ---"))?;
let _ = writeln!(fmt, "{:?}", self.0.backtrace())?;
Ok(())
}
}
pub fn trace_error(e: &Error) {
eprintln!("{}", ImagTrace::new(e));
}
pub fn trace_error_dbg(e: &Error) {
debug!("{}", ImagTrace::new(e));
}
pub trait MapErrTrace {
type Output;
fn map_err_trace(self) -> Self;
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output;
}
impl<U> MapErrTrace for Result<U, Error> {
type Output = U;
fn map_err_trace(self) -> Self {
self.map_err(|e| { trace_error(&e); e })
}
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output {
self.map_err(|e| { trace_error(&e); exit(code) }).unwrap()
}
}