Skip to content

Commit

Permalink
Merge pull request #10 from killercup/rollup
Browse files Browse the repository at this point in the history
Rollup!
  • Loading branch information
killercup committed Apr 17, 2018
2 parents 369ca4e + 80046e1 commit d862329
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions examples/err.rs

This file was deleted.

44 changes: 30 additions & 14 deletions src/lib.rs
Expand Up @@ -13,6 +13,7 @@ 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 termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};

Expand All @@ -30,7 +31,6 @@ pub struct Metadata<'a> {
macro_rules! setup_panic {
() => {
use human_panic::*;
use std::env;
use std::panic::{self, PanicInfo};

let meta = Metadata {
Expand All @@ -44,44 +44,60 @@ macro_rules! setup_panic {
let file_path =
handle_dump(info).expect("human-panic: dumping logs to disk failed");

print_msg(file_path, &meta)
print_msg(&file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
};
}

/// Utility function that prints a message to our human users
pub fn print_msg(file_path: String, meta: &Metadata) -> IoResult<()> {
let (_version, name, authors, homepage) = (
meta.version,
meta.name,
meta.authors,
meta.homepage,
);
pub fn print_msg<P: AsRef<Path>>(
file_path: P,
meta: &Metadata,
) -> IoResult<()> {
let (_version, name, authors, homepage) =
(meta.version, meta.name, meta.authors, meta.homepage);

let stderr = BufferWriter::stderr(ColorChoice::Auto);
let mut buffer = stderr.buffer();
buffer.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;

writeln!(&mut buffer, "Well, this is embarrasing.\n")?;
writeln!(&mut buffer, "{} had a problem and crashed. To help us diagnose the problem you can send us a crash report.\n", name)?;
writeln!(&mut buffer, "We have generated a report file at \"{}\". Submit an issue or email with the subject of \"{} Crash Report\" and include the report as an attachment.\n", &file_path, name)?;
writeln!(
&mut buffer,
"{} had a problem and crashed. To help us diagnose the \
problem you can send us a crash report.\n",
name
)?;
writeln!(
&mut buffer,
"We have generated a report file at \"{}\". Submit an \
issue or email with the subject of \"{} Crash Report\" and include the \
report as an attachment.\n",
file_path.as_ref().display(),
name
)?;

if !homepage.is_empty() {
writeln!(&mut buffer, "- Homepage: {}", homepage)?;
}
if !authors.is_empty() {
writeln!(&mut buffer, "- Authors: {}", authors)?;
}
writeln!(&mut buffer, "\nWe take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.\n")?;
writeln!(
&mut buffer,
"\nWe take privacy seriously, and do not perform any \
automated error collection. In order to improve the software, we rely on \
people to submit reports.\n"
)?;
writeln!(&mut buffer, "Thank you kindly!")?;

stderr.print(&buffer).unwrap();
Ok(())
}

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

let payload = panic_info.payload().downcast_ref::<&str>();
Expand All @@ -99,5 +115,5 @@ pub fn handle_dump(panic_info: &PanicInfo) -> Result<String, FailError> {
}

let report = Report::new(Method::Panic, expl);
return report.persist();
report.persist()
}
16 changes: 8 additions & 8 deletions src/report.rs
Expand Up @@ -7,7 +7,7 @@ extern crate uuid;

use self::failure::Error;
use self::uuid::Uuid;
use std::{env, fs::File, io::Write};
use std::{env, fs::File, io::Write, path::Path, path::PathBuf};

/// Method of failure.
#[derive(Debug, Serialize)]
Expand All @@ -27,13 +27,12 @@ pub struct Report {
impl Report {
/// Create a new instance.
pub fn new(method: Method, explanation: String) -> Self {
let operating_system;
if cfg!(windows) {
operating_system = "windows".to_string();
let operating_system = if cfg!(windows) {
"windows".to_string()
} else {
let platform = os_type::current_platform();
operating_system = format!("unix:{:?}", platform.os_type);
}
format!("unix:{:?}", platform.os_type)
};

Self {
crate_version: env!("CARGO_PKG_VERSION").to_string(),
Expand All @@ -45,14 +44,15 @@ impl Report {
}

/// Write a file to disk.
pub fn persist(&self) -> Result<String, Error> {
pub fn persist(&self) -> Result<PathBuf, Error> {
let uuid = Uuid::new_v4().hyphenated().to_string();
let tmp_dir = env::temp_dir();
let tmp_dir = match tmp_dir.to_str() {
Some(dir) => dir,
None => bail!("Could not create a tmp directory for a report."),
};
let file_path = format!("{}/report-{}.toml", tmp_dir, &uuid);
let file_name = format!("report-{}.toml", &uuid);
let file_path = Path::new(tmp_dir).join(file_name);
let mut file = File::create(&file_path)?;
let toml = toml::to_string(&self)?;
file.write_all(toml.as_bytes())?;
Expand Down

0 comments on commit d862329

Please sign in to comment.