Skip to content

Commit

Permalink
Add a logging panic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Dec 27, 2015
1 parent d482c52 commit 30d76c5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<meta http-equiv=refresh content=0;url=log/index.html>' > env/target/doc/index.html &&
echo '<meta http-equiv=refresh content=0;url=log/index.html>' > 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:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ release_max_level_warn = []
release_max_level_info = []
release_max_level_debug = []
release_max_level_trace = []

nightly = []
45 changes: 45 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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("<unnamed>");

let msg = match info.payload().downcast_ref::<&'static str>() {
Some(s) => *s,
None => match info.payload().downcast_ref::<String>() {
Some(s) => &s[..],
None => "Box<Any>",
}
};

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 {
Expand Down

0 comments on commit 30d76c5

Please sign in to comment.