client/common/panic_handler.rs
1// Copyright 2026 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5use std::{
6 backtrace::Backtrace,
7 panic::{self, PanicHookInfo},
8};
9
10/// Install a process-wide panic hook that preserves the default stderr
11/// output, mirrors the message to the log4rs-backed application log, and
12/// flushes the logger before returning so the panic message is durable
13/// before the panicking thread unwinds.
14///
15/// Must be called after `log4rs::init_*` so that `log::error!` reaches
16/// the configured appenders.
17pub fn setup() {
18 panic::set_hook(Box::new(|pi: &PanicHookInfo<'_>| {
19 let backtrace = Backtrace::force_capture();
20 eprintln!("panic: {pi}\nbacktrace:\n{backtrace}");
21 log::error!("panic: {pi}\nbacktrace: {backtrace}");
22 log::logger().flush();
23 }));
24}