From 401b0aa59d67a2d065a74e3cd3e355aae8c0c520 Mon Sep 17 00:00:00 2001 From: bjoernQ Date: Fri, 2 Feb 2024 13:38:55 +0100 Subject: [PATCH] Nicer output on nightly --- build.rs | 17 +++++++++++++++++ src/lib.rs | 27 ++++++++++++++++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index c64f44c..74e840c 100644 --- a/build.rs +++ b/build.rs @@ -22,4 +22,21 @@ fn main() { if backend.iter().filter(|&&f| f).count() == 0 { panic!("A backend needs to be selected"); } + + if is_nightly() { + println!("cargo:rustc-cfg=nightly"); + } +} + +fn is_nightly() -> bool { + let version_output = std::process::Command::new( + std::env::var_os("RUSTC").unwrap_or_else(|| std::ffi::OsString::from("rustc")), + ) + .arg("-V") + .output() + .unwrap() + .stdout; + let version_string = String::from_utf8_lossy(&version_output); + + version_string.contains("nightly") } diff --git a/src/lib.rs b/src/lib.rs index 34a25ee..e4ffa0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #![allow(rustdoc::bare_urls)] #![doc = include_str!("../README.md")] #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")] +#![cfg_attr(nightly, feature(panic_info_message))] #[cfg(feature = "defmt")] use defmt as _; @@ -57,20 +58,28 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! { if let Some(location) = info.location() { let (file, line, column) = (location.file(), location.line(), location.column()); println!( - "!! A panic occured in '{}', at line {}, column {}", + "!! A panic occured in '{}', at line {}, column {}:", file, line, column ); } else { - println!("!! A panic occured at an unknown location"); + println!("!! A panic occured at an unknown location:"); } - println!(""); + #[cfg(not(nightly))] + { + #[cfg(not(feature = "defmt"))] + println!("{:#?}", info); - #[cfg(not(feature = "defmt"))] - println!("{:#?}", info); + #[cfg(feature = "defmt")] + println!("{:#?}", defmt::Display2Format(info)); + } - #[cfg(feature = "defmt")] - println!("{:#?}", defmt::Display2Format(info)); + #[cfg(nightly)] + { + if let Some(message) = info.message() { + println!("{}", message); + } + } println!(""); println!("Backtrace:"); @@ -83,6 +92,10 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! { } for e in backtrace { if let Some(addr) = e { + #[cfg(all(feature = "colors", feature = "println"))] + println!("{}0x{:x}", RED, addr - crate::arch::RA_OFFSET); + + #[cfg(not(any(feature = "colors", feature = "println")))] println!("0x{:x}", addr - crate::arch::RA_OFFSET); } }