Skip to content

Commit

Permalink
Improve panic handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Jul 5, 2024
1 parent 9a57cf8 commit 38afda7
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 57 deletions.
118 changes: 67 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ arboard = "3.4.0"
chrono = { version = "0.4.38", default-features = false }
clap = "4.5.7"
clap-verbosity-flag = "2.2.0"
color-eyre = "0.6.3"
color-eyre = { version = "0.6.3", default-features = false }
enumflags2 = "0.7.10"
exec = "0.3.1"
fern = "0.6.2"
Expand All @@ -48,11 +48,13 @@ nameof = "1.2.2"
nix = "0.29.0"
open = "5.1.4"
procfs = "0.16.0"
quote = "1.0.36"
rstest = "0.21.0"
serde = { version = "1.0.203", features = ["derive"] }
shellexpand = "3.1.0"
single-instance = "0.3.3"
stderrlog = { version = "0.6.0", default-features = false }
syn = "2.0.68"
sysinfo = "0.30.12"
system_shutdown = "4.0.1"
thiserror = "1.0.61"
Expand Down Expand Up @@ -100,3 +102,4 @@ unwrap_used = "warn"
use_self = "warn"
used_underscore_binding = "warn"
wildcard_imports = "warn"
missing_panics_doc = "warn"
4 changes: 2 additions & 2 deletions gravel-ffi-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition.workspace = true
proc-macro = true

[dependencies]
quote = "1.0.36"
syn = { version = "2.0.68", features = ["full"] }
quote.workspace = true
syn.workspace = true

[lints]
workspace = true
2 changes: 2 additions & 0 deletions gravel-ffi/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct ForwardLogger {
}

impl ForwardLogger {
/// # Panics
/// When called more than once. See `log::set_logger`.
pub fn register(target: BoxDynLogTarget, crate_name: &'static str) {
log::set_max_level(target.max_level().into());
log::set_logger(Box::leak(Box::new(Self { target }))).expect("logger must only be registered once per plugin");
Expand Down
2 changes: 2 additions & 0 deletions gravel-ffi/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{env, iter::once, path::PathBuf};
const XDG_DATA_HOME: &str = "XDG_DATA_HOME";
const XDG_DATA_DIRS: &str = "XDG_DATA_DIRS";

/// # Panics
/// When $HOME/$USERPROFILE are not set.
pub fn home() -> PathBuf {
#[cfg(unix)]
let home = env::var("HOME").expect("$HOME must always be set");
Expand Down
2 changes: 1 addition & 1 deletion gravel-ffi/tests/plugin_load_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused_crate_dependencies)]
#![allow(unused_crate_dependencies, clippy::missing_panics_doc)]

use abi_stable::std_types::{ROption, RSlice, RString};
use abi_stable::{library::RootModule, sabi_trait, traits::IntoReprC};
Expand Down
2 changes: 1 addition & 1 deletion gravel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ anyhow.workspace = true
chrono = { workspace = true }
clap = { workspace = true, features = ["derive"] }
clap-verbosity-flag.workspace = true
color-eyre.workspace = true
color-eyre = { workspace = true, features = ["issue-url"] }
fern.workspace = true
file-rotate.workspace = true
glob.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions gravel/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod engine;
mod frontend;
mod hotkeys;
mod logging;
mod panic;
mod plugins;
mod single_instance;

Expand All @@ -17,4 +18,5 @@ pub use engine::engine;
pub use frontend::frontend;
pub use hotkeys::hotkeys;
pub use logging::logging;
pub use panic::panic;
pub use plugins::plugins;
44 changes: 44 additions & 0 deletions gravel/src/init/panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use color_eyre::config::{HookBuilder, PanicHook};
use std::panic::{set_hook, PanicInfo};

pub fn panic() {
#[allow(clippy::print_stderr)]
set_hook(Box::new(move |panic_info| {
log_panic(panic_info);
eprintln!("{}", get_eyre().panic_report(panic_info));
}));
}

fn get_eyre() -> PanicHook {
let (eyre_panic, _) = HookBuilder::default()
.issue_url(concat!(env!("CARGO_PKG_REPOSITORY"), "/issues/new"))
.into_hooks();

eyre_panic
}

fn log_panic(panic_info: &PanicInfo<'_>) {
let payload = panic_info
.payload()
.downcast_ref::<String>()
.map(String::as_str)
.or_else(|| panic_info.payload().downcast_ref::<&str>().cloned())
.unwrap_or("<non-string panic payload>");

let location = panic_info
.location()
.map_or_else(|| String::from("awd"), |l| format!("{}:{}", l.file(), l.line()));

log::error!("panicked at {location}: {payload}");
log::logger().flush();
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn eyre_hook_init() {
get_eyre();
}
}
2 changes: 1 addition & 1 deletion gravel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod init;
// unwrap instead of returning a Result so we hit color_eyre's panic handler
#[allow(clippy::unwrap_used)]
fn main() {
color_eyre::install().unwrap();
init::panic();

#[cfg(windows)]
init::windows_console::attach();
Expand Down

0 comments on commit 38afda7

Please sign in to comment.