From 30d76c5732dc445b1d5b294d16ca2457ba59e8c8 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 26 Dec 2015 09:37:43 -0700 Subject: [PATCH] Add a logging panic handler --- .travis.yml | 8 +++++--- Cargo.toml | 2 ++ src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 700e23b74..15b6a4778 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,18 +7,20 @@ rust: - nightly script: - cargo build --verbose + - ([ $TRAVIS_RUST_VERSION != nightly ] || cargo build --verbose --features nightly) - cargo test --verbose - cargo test --verbose --manifest-path env/Cargo.toml - cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml - cargo run --verbose --manifest-path tests/max_level_features/Cargo.toml --release - - cargo doc --manifest-path env/Cargo.toml + - ([ $TRAVIS_RUST_VERSION != nightly ] || cargo doc --no-deps --features nightly) + - CARGO_TARGET_DIR=target cargo doc --no-deps --manifest-path env/Cargo.toml after_success: | [ $TRAVIS_BRANCH = master ] && [ $TRAVIS_PULL_REQUEST = false ] && [ $TRAVIS_RUST_VERSION = nightly ] && - echo '' > env/target/doc/index.html && + echo '' > target/doc/index.html && pip install ghp-import --user $USER && - $HOME/.local/bin/ghp-import -n env/target/doc && + $HOME/.local/bin/ghp-import -n target/doc && git push -qf https://${TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages env: global: diff --git a/Cargo.toml b/Cargo.toml index 583a4bc89..9b6bf7485 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,5 @@ release_max_level_warn = [] release_max_level_info = [] release_max_level_debug = [] release_max_level_trace = [] + +nightly = [] diff --git a/src/lib.rs b/src/lib.rs index 0b1503882..4e4a5a3d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,6 +148,7 @@ html_favicon_url = "https://www.rust-lang.org/favicon.ico", html_root_url = "https://doc.rust-lang.org/log/")] #![warn(missing_docs)] +#![cfg_attr(feature = "nightly", feature(panic_handler))] extern crate libc; @@ -159,6 +160,7 @@ use std::mem; use std::ops::Deref; use std::str::FromStr; use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; +#[macro_use] mod macros; // The setup here is a bit weird to make at_exit work. @@ -620,6 +622,49 @@ impl error::Error for SetLoggerError { fn description(&self) -> &str { "set_logger() called multiple times" } } +/// Registers a panic handler which logs at the error level. +/// +/// The format is the same as the default panic handler. The reporting module is +/// `log::panic`. +/// +/// Requires the `nightly` feature. +#[cfg(feature = "nightly")] +pub fn log_panics() { + std::panic::set_handler(panic::log); +} + +// inner module so that the reporting module is log::panic instead of log +#[cfg(feature = "nightly")] +mod panic { + use std::panic::PanicInfo; + use std::thread; + + pub fn log(info: &PanicInfo) { + let thread = thread::current(); + let thread = thread.name().unwrap_or(""); + + let msg = match info.payload().downcast_ref::<&'static str>() { + Some(s) => *s, + None => match info.payload().downcast_ref::() { + Some(s) => &s[..], + None => "Box", + } + }; + + match info.location() { + Some(location) => { + error!("thread '{}' panicked at '{}': {}:{}", + thread, + msg, + location.file(), + location.line()) + } + None => error!("thread '{}' panicked at '{}'", thread, msg), + } + } +} + + struct LoggerGuard(usize); impl Drop for LoggerGuard {