diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f7a543a..ad52328 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,4 +25,5 @@ jobs: run: cargo -Zminimal-versions generate-lockfile - uses: dtolnay/rust-toolchain@1.74.0 - name: Cargo check - run: cargo check --workspace --all-targets + # skip the tests, which require the lazy_cell feature from Rust 1.80 + run: cargo check --workspace diff --git a/Cargo.toml b/Cargo.toml index b35554b..7f7b4c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ description = "Log panics to the `log` macro as error" include = ["src", "LICENSE"] categories = ["development-tools", "development-tools::debugging"] # https://crates.io/category_slugs keywords = ["log", "panic"] +rust-version = "1.74" [dependencies] -log = "0.4" +log = "0.4.4" diff --git a/README.md b/README.md index f6f05c4..32f7e43 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,6 @@ Call this somewhere at the start of your program (after initializing your logger ```rust use panic_log::Configuration; -[...] +// ... panic_log::initialize_hook(Configuration::default()); ``` diff --git a/src/lib.rs b/src/lib.rs index 2b84600..0daa717 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,7 +49,12 @@ pub fn initialize_hook(config: Configuration) { } else { "".to_owned() }; - let message = info.payload().downcast_ref::<&str>().unwrap_or(&""); + let message = info + .payload() + .downcast_ref::<&str>() + .copied() + .or_else(|| info.payload().downcast_ref::().map(|s| s.as_str())) + .unwrap_or(""); let backtrace = if config.force_capture { backtrace::Backtrace::force_capture() diff --git a/tests/test_hook.rs b/tests/test_hook.rs index f8d7355..6a4de0d 100644 --- a/tests/test_hook.rs +++ b/tests/test_hook.rs @@ -6,25 +6,41 @@ use std::{ use panic_log::{initialize_hook, Configuration}; +// The test binary runs all tests in parallel by default; this lets multiple tests overwrite the +// panic hook concurrently and cause spurious failure. +static SERIAL_TEST: Mutex<()> = Mutex::new(()); + #[test] #[should_panic] fn test() { + let _serial = SERIAL_TEST.lock().unwrap(); + initialize_hook(Configuration::default()); + + // Drop the lock to not poison it + drop(_serial); panic!("Test"); } #[test] #[should_panic] fn test_forced_trace() { + let _serial = SERIAL_TEST.lock().unwrap(); + initialize_hook(Configuration { force_capture: true, ..Default::default() }); + + // Drop the lock to not poison it + drop(_serial); panic!("Test"); } #[test] fn test_original_hook() { + let _serial = SERIAL_TEST.lock().unwrap(); + let original_hook = panic::take_hook(); let ran_hook = Arc::new(Mutex::new(false)); let ran_hook_copy = Arc::clone(&ran_hook); @@ -40,11 +56,13 @@ fn test_original_hook() { }); let _ = panic::catch_unwind(|| panic!("Test")); - assert_eq!(*ran_hook.lock().unwrap(), true); + assert!(*ran_hook.lock().unwrap()); } #[test] fn test_no_original_hook() { + let _serial = SERIAL_TEST.lock().unwrap(); + let original_hook = panic::take_hook(); let ran_hook = Arc::new(Mutex::new(false)); let ran_hook_copy = Arc::clone(&ran_hook); @@ -60,11 +78,13 @@ fn test_no_original_hook() { }); let _ = panic::catch_unwind(|| panic!("Test")); - assert_eq!(*ran_hook.lock().unwrap(), false); + assert!(!*ran_hook.lock().unwrap()); } #[test] fn test_flush_logger() { + let _serial = SERIAL_TEST.lock().unwrap(); + struct Logger { pub flushed: Arc>, }