Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Log Panics
◌ CI pending Fintan Halpenny committed 7 months ago
commit ed8b086045ee5d7bd1327f579de7861a1cf49e3b
parent 0441b048f93c67e4e01d0d5e0292bd04bbad72ed
2 pending (2 total) View logs
3 files changed +37 -8
modified Cargo.lock
@@ -2908,6 +2908,7 @@ name = "radicle-node"
version = "0.15.0"
dependencies = [
 "amplify",
+
 "backtrace",
 "bloomy",
 "bytes",
 "chrono",
modified crates/radicle-node/Cargo.toml
@@ -10,12 +10,13 @@ build = "build.rs"
rust-version.workspace = true

[features]
-
default = ["systemd", "structured-logger"]
+
default = ["backtrace", "systemd", "structured-logger"]
systemd = ["dep:radicle-systemd"]
test = ["radicle/test", "radicle-crypto/test", "radicle-crypto/cyphernet", "radicle-protocol/test", "qcheck", "snapbox"]

[dependencies]
amplify = { workspace = true }
+
backtrace = { version = "0.3.75", optional = true }
bloomy = "1.2"
bytes = { workspace = true }
chrono = { workspace = true, features = ["clock"] }
modified crates/radicle-node/src/main.rs
@@ -300,15 +300,40 @@ fn initialize_logging(options: &LogOptions) -> Result<(), Box<dyn std::error::Er
    Ok(())
}

-
fn main() {
-
    // If `RUST_BACKTRACE` does not have a value, then we set it to capture
-
    // backtraces for better debugging, otherwise we keep the environments
-
    // value.
-
    const RUST_BACKTRACE: &str = "RUST_BACKTRACE";
-
    if std::env::var_os(RUST_BACKTRACE).is_none() {
-
        std::env::set_var(RUST_BACKTRACE, "1");
+
fn panic_hook(info: &std::panic::PanicHookInfo) {
+
    #[cfg(feature = "backtrace")]
+
    let backtrace = format!("{:?}", backtrace::Backtrace::new());
+

+
    #[cfg(not(feature = "backtrace"))]
+
    let backtrace = " (no backtrace available)";
+

+
    let thread = std::thread::current();
+
    let thread = thread.name().unwrap_or("<unnamed>");
+

+
    let msg = info
+
        .payload()
+
        .downcast_ref::<&'static str>()
+
        .copied()
+
        .or(info.payload().downcast_ref::<String>().map(|s| s.as_str()))
+
        .unwrap_or("Box<Any>");
+

+
    match info.location() {
+
        Some(location) => {
+
            log::error!(
+
                target: "panic", "thread '{thread}' panicked at '{msg}': {}:{}{backtrace}",
+
                location.file(),
+
                location.line(),
+
            );
+
        }
+
        None => log::error!(
+
            target: "panic", "thread '{thread}' panicked at '{msg}'{backtrace}",
+
        ),
    }

+
    log::logger().flush();
+
}
+

+
fn main() {
    let options = parse_options().unwrap_or_else(|err| {
        // The lexopt errors read nicely with a comma.
        eprintln!("Failed to parse options, {err:#}");
@@ -320,6 +345,8 @@ fn main() {
        exit(3);
    });

+
    std::panic::set_hook(Box::new(panic_hook));
+

    if let Err(err) = execute(options) {
        log::error!(target: "node", "{err:#}");
        exit(1);