add custom panic handler

This commit is contained in:
hinto.janai 2023-11-17 12:11:25 -05:00
parent 1eff1b0780
commit 587700ea04
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
2 changed files with 62 additions and 0 deletions

View file

@ -80,6 +80,7 @@ mod regex;
mod xmr;
mod macros;
mod free;
mod panic;
use {macros::*,crate::regex::*,ferris::*,constants::*,node::*,disk::*,update::*,gupax::*,helper::*};
// Sudo (dummy values for Windows)
@ -1116,9 +1117,16 @@ fn cmp_f64(a: f64, b: f64) -> std::cmp::Ordering {
//---------------------------------------------------------------------------------------------------- Main [App] frame
fn main() {
let now = Instant::now();
// Set custom panic hook.
crate::panic::set_panic_hook(now);
// Init logger.
init_logger(now);
let mut app = App::new(now);
init_auto(&mut app);
// Init GUI stuff.
let selected_width = app.state.gupax.selected_width as f32;
let selected_height = app.state.gupax.selected_height as f32;
let initial_window_size = if selected_width > APP_MAX_WIDTH || selected_height > APP_MAX_HEIGHT {
@ -1128,10 +1136,14 @@ fn main() {
Some(Vec2::new(app.state.gupax.selected_width as f32, app.state.gupax.selected_height as f32))
};
let options = init_options(initial_window_size);
// Gupax folder cleanup.
match clean_dir() {
Ok(_) => info!("Temporary folder cleanup ... OK"),
Err(e) => warn!("Could not cleanup [gupax_tmp] folders: {}", e),
}
// Run Gupax.
info!("/*************************************/ Init ... OK /*************************************/");
eframe::run_native(&app.name_version.clone(), options, Box::new(|cc| Box::new(App::cc(cc, app))),);
}

50
src/panic.rs Normal file
View file

@ -0,0 +1,50 @@
//---------------------------------------------------------------------------------------------------- Use
use crate::constants::{
GUPAX_VERSION,
P2POOL_VERSION,
XMRIG_VERSION,
OS_NAME,
COMMIT,
};
//----------------------------------------------------------------------------------------------------
/// Set custom panic hook.
pub(crate) fn set_panic_hook(now: std::time::Instant) {
std::panic::set_hook(Box::new(move |panic_info| {
// Set stack-trace.
let stack_trace = std::backtrace::Backtrace::force_capture();
let args = std::env::args_os();
let uptime = now.elapsed().as_secs_f32();
// Re-format panic info.
let panic_info = format!(
"{panic_info:#?}
info:
OS | {OS_NAME}
args | {args:?}
commit | {COMMIT}
gupax | {GUPAX_VERSION}
p2pool | {P2POOL_VERSION} (bundled)
xmrig | {XMRIG_VERSION} (bundled)
uptime | {uptime} seconds
stack backtrace:\n{stack_trace}",
);
// Attempt to write panic info to disk.
match crate::disk::get_gupax_data_path() {
Ok(mut path) => {
path.push("crash.txt");
match std::fs::write(&path, &panic_info) {
Ok(_) => eprintln!("\nmass_panic!() - Saved panic log to: {}\n", path.display()),
Err(e) => eprintln!("\nmass_panic!() - Could not save panic log: {e}\n"),
}
},
Err(e) => eprintln!("panic_hook PATH error: {e}"),
}
// Exit all threads.
benri::mass_panic!(panic_info);
}));
}