diff --git a/src/main.rs b/src/main.rs index 2d8c564..56b30e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,6 @@ extern crate lazy_static; #[macro_use] extern crate serde; -use std::backtrace; -use std::fs::File; -use std::io::Write; use std::path::PathBuf; use application::Application; @@ -22,6 +19,7 @@ mod events; mod ext_traits; mod library; mod model; +mod panic; mod queue; mod serialization; mod sharing; @@ -40,31 +38,10 @@ mod ipc; #[cfg(feature = "mpris")] mod mpris; -/// Register a custom panic handler to write the backtrace to a file since stdout is in use by the -/// Cursive TUI library during most of the application. -fn register_backtrace_panic_handler() { - // During most of the program, Cursive is responsible for drawing to the - // tty. Since stdout probably doesn't work as expected during a panic, the - // backtrace is written to a file at $USER_CACHE_DIR/ncspot/backtrace.log. - std::panic::set_hook(Box::new(|panic_info| { - // A panic hook will prevent the default panic handler from being - // called. An unwrap in this part would cause a hard crash of ncspot. - // Don't unwrap/expect/panic in here! - if let Ok(backtrace_log) = config::try_proj_dirs() { - let mut path = backtrace_log.cache_dir; - path.push("backtrace.log"); - if let Ok(mut file) = File::create(path) { - writeln!(file, "{}", backtrace::Backtrace::force_capture()).unwrap_or_default(); - writeln!(file, "{panic_info}").unwrap_or_default(); - } - } - })); -} - -// Functionality related to the operating system process itself is implemented here. -// Functionality related to ncspot goes into `Application`. fn main() -> Result<(), String> { - register_backtrace_panic_handler(); + // Set a custom backtrace hook that writes the backtrace to a file instead of stdout, since + // stdout is most likely in use by Cursive. + panic::register_backtrace_panic_handler(); let matches = program_arguments().get_matches(); diff --git a/src/panic.rs b/src/panic.rs new file mode 100644 index 0000000..ca2665d --- /dev/null +++ b/src/panic.rs @@ -0,0 +1,25 @@ +use std::io::Write; +use std::{backtrace, fs::File}; + +use crate::config; + +/// Register a custom panic handler to write backtraces to a file called `backtrace.log` inside the +/// user's cache directory. +pub fn register_backtrace_panic_handler() { + // During most of the program, Cursive is responsible for drawing to the + // tty. Since stdout probably doesn't work as expected during a panic, the + // backtrace is written to a file at $USER_CACHE_DIR/ncspot/backtrace.log. + std::panic::set_hook(Box::new(|panic_info| { + // A panic hook will prevent the default panic handler from being + // called. An unwrap in this part would cause a hard crash of ncspot. + // Don't unwrap/expect/panic in here! + if let Ok(backtrace_log) = config::try_proj_dirs() { + let mut path = backtrace_log.cache_dir; + path.push("backtrace.log"); + if let Ok(mut file) = File::create(path) { + writeln!(file, "{}", backtrace::Backtrace::force_capture()).unwrap_or_default(); + writeln!(file, "{panic_info}").unwrap_or_default(); + } + } + })); +}