Skip to content

Commit

Permalink
Fixing wrong data in certain crash dump fields (#15)
Browse files Browse the repository at this point in the history
Fixing a bug in the log dumps

* Carrying over metadata from first macro call to properly
include metadata in crash dumps.
* Making Method derive Copy
  • Loading branch information
spacekookie committed Apr 18, 2018
1 parent ed11055 commit a3ec0dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/lib.rs
Expand Up @@ -13,8 +13,8 @@ use report::{Method, Report};

use failure::Error as FailError;
use std::io::{Result as IoResult, Write};
use std::path::{Path, PathBuf};
use std::panic::PanicInfo;
use std::path::{Path, PathBuf};
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};

/// A convenient metadata struct that describes a crate
Expand Down Expand Up @@ -45,8 +45,8 @@ macro_rules! setup_panic {
};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path =
handle_dump(info).expect("human-panic: dumping logs to disk failed");
let file_path = handle_dump(&meta, info)
.expect("human-panic: dumping logs to disk failed");

print_msg(&file_path, &meta)
.expect("human-panic: printing error message to console failed");
Expand Down Expand Up @@ -101,7 +101,10 @@ pub fn print_msg<P: AsRef<Path>>(
}

/// Utility function which will handle dumping information to disk
pub fn handle_dump(panic_info: &PanicInfo) -> Result<PathBuf, FailError> {
pub fn handle_dump(
meta: &Metadata,
panic_info: &PanicInfo,
) -> Result<PathBuf, FailError> {
let mut expl = String::new();

let payload = panic_info.payload().downcast_ref::<&str>();
Expand All @@ -118,6 +121,6 @@ pub fn handle_dump(panic_info: &PanicInfo) -> Result<PathBuf, FailError> {
None => expl.push_str("Panic location unknown.\n"),
}

let report = Report::new(Method::Panic, expl);
let report = Report::new(&meta.name, &meta.version, Method::Panic, expl);
report.persist()
}
13 changes: 9 additions & 4 deletions src/report.rs
Expand Up @@ -10,7 +10,7 @@ use self::uuid::Uuid;
use std::{env, fs::File, io::Write, path::Path, path::PathBuf};

/// Method of failure.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Clone, Copy)]
pub enum Method {
Panic,
}
Expand All @@ -26,7 +26,12 @@ pub struct Report {

impl Report {
/// Create a new instance.
pub fn new(method: Method, explanation: String) -> Self {
pub fn new(
name: &str,
version: &str,
method: Method,
explanation: String,
) -> Self {
let operating_system = if cfg!(windows) {
"windows".to_string()
} else {
Expand All @@ -35,8 +40,8 @@ impl Report {
};

Self {
crate_version: env!("CARGO_PKG_VERSION").to_string(),
name: env!("CARGO_PKG_NAME").to_string(),
crate_version: version.to_string(),
name: name.to_string(),
operating_system,
method,
explanation,
Expand Down

0 comments on commit a3ec0dd

Please sign in to comment.