Skip to content

Commit f7400c9

Browse files
authored
Extend panic hook to also log panic messages.
1 parent 025a1ad commit f7400c9

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

crates/rrg/src/main.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
// Use of this source code is governed by an MIT-style license that can be found
44
// in the LICENSE file or at https://opensource.org/licenses/MIT.
55

6-
use log::info;
6+
use log::{error, info};
77

88
fn main() {
99
let args = rrg::args::from_env_args();
1010
rrg::log::init(&args);
1111

12+
// TODO: https://github.com/rust-lang/rust/issues/92649
13+
//
14+
// Refactor once `panic_update_hook` is stable.
15+
16+
// Because Fleetspeak does not necessarily capture RRG's standard error, it
17+
// might be difficult to find reason behind a crash. Thus, we extend the
18+
// standard panic hook to also log the panic message.
19+
let panic_hook = std::panic::take_hook();
20+
std::panic::set_hook(Box::new(move |info| {
21+
// Note that logging is an I/O operation and it itself might panic. In
22+
// case of the logging failure it does not end in an endless cycle (of
23+
// trying to log, which panics, which tries to log and so on) but it
24+
// triggers an abort which is fine.
25+
error!("thread panicked: {info}");
26+
panic_hook(info)
27+
}));
28+
1229
info!("sending Fleetspeak startup information");
1330
fleetspeak::startup(env!("CARGO_PKG_VERSION"));
1431

0 commit comments

Comments
 (0)