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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![cfg_attr(feature = "nightly", deny(missing_docs))]
#![cfg_attr(feature = "nightly", feature(external_doc))]
extern crate backtrace;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate termcolor;
mod report;
use report::{Method, Report};
use std::borrow::Cow;
use std::io::{Result as IoResult, Write};
use std::panic::PanicInfo;
use std::path::{Path, PathBuf};
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
pub struct Metadata {
pub version: Cow<'static, str>,
pub name: Cow<'static, str>,
pub authors: Cow<'static, str>,
pub homepage: Cow<'static, str>,
}
#[macro_export]
macro_rules! setup_panic {
($meta:expr) => {
use $crate::{handle_dump, print_msg, Metadata};
use std::panic::{self, PanicInfo};
panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);
print_msg(file_path, &$meta)
.expect("human-panic: printing error message to console failed");
}));
};
() => {
use $crate::{handle_dump, print_msg, Metadata};
use std::panic::{self, PanicInfo};
let meta = Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};
panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&meta, info);
print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
};
}
pub fn print_msg<P: AsRef<Path>>(
file_path: Option<P>,
meta: &Metadata,
) -> IoResult<()> {
let (_version, name, authors, homepage) =
(&meta.version, &meta.name, &meta.authors, &meta.homepage);
let stderr = BufferWriter::stderr(ColorChoice::Auto);
let mut buffer = stderr.buffer();
buffer.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;
writeln!(&mut buffer, "Well, this is embarrassing.\n")?;
writeln!(
&mut buffer,
"{} had a problem and crashed. To help us diagnose the \
problem you can send us a crash report.\n",
name
)?;
writeln!(
&mut buffer,
"We have generated a report file at \"{}\". Submit an \
issue or email with the subject of \"{} Crash Report\" and include the \
report as an attachment.\n",
match file_path {
Some(fp) => format!("{}", fp.as_ref().display()),
None => "<Failed to store file to disk>".to_string(),
},
name
)?;
if !homepage.is_empty() {
writeln!(&mut buffer, "- Homepage: {}", homepage)?;
}
if !authors.is_empty() {
writeln!(&mut buffer, "- Authors: {}", authors)?;
}
writeln!(
&mut buffer,
"\nWe take privacy seriously, and do not perform any \
automated error collection. In order to improve the software, we rely on \
people to submit reports.\n"
)?;
writeln!(&mut buffer, "Thank you kindly!")?;
buffer.reset()?;
stderr.print(&buffer).unwrap();
Ok(())
}
pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
let mut expl = String::new();
let payload = panic_info.payload().downcast_ref::<&str>();
if let Some(payload) = payload {
expl.push_str(&format!("Cause: {}. ", &payload));
}
match panic_info.location() {
Some(location) => expl.push_str(&format!(
"Panic occurred in file '{}' at line {}\n",
location.file(),
location.line()
)),
None => expl.push_str("Panic location unknown.\n"),
}
let report = Report::new(&meta.name, &meta.version, Method::Panic, expl);
match report.persist() {
Ok(f) => Some(f),
Err(_) => {
eprintln!("{}", report.serialize().unwrap());
None
}
}
}