-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put message box behind a gui feature
- Loading branch information
Showing
9 changed files
with
62 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,8 @@ | |
//! in a vector or have an assert fail somewhere. | ||
//! | ||
//! When an error eventually occurs, you probably will want to know about it. So | ||
//! instead of just providing an error message on the command line or the GUI | ||
//! suddenly disappearing, we can create a call to action for people to submit a | ||
//! report. | ||
//! instead of crashing without notice or printing a confusing error message we | ||
//! can create a call to action for people to submit a report. | ||
//! | ||
//! This should empower people to engage in communication, lowering the chances | ||
//! people might get frustrated. And making it easier to figure out what might be | ||
|
@@ -44,7 +43,8 @@ | |
//! ## GUI Applications | ||
//! GUI applications don't have a terminal to output errors to. | ||
//! The errors printed to the terminal can optionally be displayed in a message box window. | ||
//! This is enabled by using a `Metadata` struct with `create_window: true`. | ||
//! This is enabled with `human-panic = { version = "1.0", features = ["gui"] }` | ||
//! in your `cargo.toml`. | ||
#![cfg_attr(feature = "nightly", deny(missing_docs))] | ||
#![cfg_attr(feature = "nightly", feature(external_doc))] | ||
|
@@ -56,20 +56,9 @@ extern crate failure; | |
extern crate serde_derive; | ||
extern crate termcolor; | ||
|
||
#[cfg(target_os = "windows")] | ||
#[path = "windows_window.rs"] | ||
pub mod window; | ||
|
||
#[cfg(target_os = "linux")] | ||
#[path = "linux_window.rs"] | ||
pub mod window; | ||
|
||
#[cfg(not(any(target_os = "linux", target_os = "windows")))] | ||
pub mod window { | ||
pub fn create_window(_: String) { } | ||
} | ||
|
||
mod report; | ||
|
||
use report::{Method, Report}; | ||
|
||
use std::borrow::Cow; | ||
|
@@ -88,8 +77,6 @@ pub struct Metadata { | |
pub authors: Cow<'static, str>, | ||
/// The URL of the crate's website | ||
pub homepage: Cow<'static, str>, | ||
/// Should an error message window be created. Only implemented for Windows + X11. noop on other platforms | ||
pub create_window: bool, | ||
} | ||
|
||
/// `human-panic` initialisation macro | ||
|
@@ -110,53 +97,42 @@ pub struct Metadata { | |
/// version: env!("CARGO_PKG_VERSION").into(), | ||
/// authors: "My Company Support <[email protected]".into(), | ||
/// homepage: "support.mycompany.com".into(), | ||
/// create_window: false, | ||
/// }); | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! setup_panic { | ||
($meta:expr) => { | ||
use $crate::{handle_dump, print_msg, write_msg, Metadata}; | ||
use $crate::window; | ||
use $crate::{handle_dump, Metadata}; | ||
use std::panic::{self, PanicInfo}; | ||
use std::io::{Cursor, Read}; | ||
|
||
panic::set_hook(Box::new(move |info: &PanicInfo| { | ||
let file_path = handle_dump(&$meta, info); | ||
|
||
print_msg(file_path.clone(), &$meta) | ||
$crate::print_msg(file_path.clone(), &$meta) | ||
.expect("human-panic: printing error message to console failed"); | ||
|
||
if $meta.create_window { | ||
let mut buffer = Cursor::new(vec!()); | ||
write_msg(file_path, &$meta, &mut buffer).expect("human-panic: generating error message for GUI failed: write_msg"); | ||
buffer.set_position(0); | ||
|
||
let mut message = String::new(); | ||
buffer.read_to_string(&mut message).expect("human-panic: generating error message for GUI failed: read_to_string"); | ||
|
||
window::create_window(message); | ||
} | ||
$crate::window::create_window(file_path, &$meta); | ||
})); | ||
}; | ||
|
||
() => { | ||
use $crate::{handle_dump, print_msg, Metadata}; | ||
use $crate::{handle_dump, Metadata}; | ||
use std::panic::{self, PanicInfo}; | ||
|
||
let meta = Metadata { | ||
version: env!("CARGO_PKG_VERSION").into(), | ||
name: env!("CARGO_PKG_NAME").into(), | ||
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(), | ||
homepage: env!("CARGO_PKG_HOMEPAGE").into(), | ||
create_window: false, | ||
}; | ||
|
||
panic::set_hook(Box::new(move |info: &PanicInfo| { | ||
let file_path = handle_dump(&meta, info); | ||
|
||
print_msg(file_path, &meta) | ||
$crate::print_msg(file_path.clone(), &meta) | ||
.expect("human-panic: printing error message to console failed"); | ||
|
||
$crate::window::create_window(file_path, &meta); | ||
})); | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[cfg(all(target_os = "windows", feature = "gui"))] | ||
#[path = "windows.rs"] | ||
mod window_impl; | ||
|
||
#[cfg(all(target_os = "linux", feature = "gui"))] | ||
#[path = "linux.rs"] | ||
mod window_impl; | ||
|
||
#[cfg(not(any(target_os = "linux", target_os = "windows")))] | ||
mod window_impl { | ||
pub(crate) fn create_window(_: String) { } | ||
} | ||
|
||
use std::path::Path; | ||
use Metadata; | ||
|
||
#[allow(unused)] | ||
pub fn create_window<P: AsRef<Path>>(file_path: Option<P>, meta: &Metadata) { | ||
#[cfg(feature = "gui")] | ||
{ | ||
use std::io::{Cursor, Read}; | ||
use write_msg; | ||
|
||
let mut buffer = Cursor::new(vec!()); | ||
write_msg(file_path, meta, &mut buffer).expect("human-panic: generating error message for GUI failed: write_msg"); | ||
buffer.set_position(0); | ||
|
||
let mut message = String::new(); | ||
buffer.read_to_string(&mut message).expect("human-panic: generating error message for GUI failed: read_to_string"); | ||
|
||
window_impl::create_window(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ fn main() { | |
version: env!("CARGO_PKG_VERSION").into(), | ||
authors: "My Company Support <[email protected]".into(), | ||
homepage: "support.mycompany.com".into(), | ||
create_window: false, | ||
}); | ||
|
||
println!("A normal log message"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "custom-panic-gui-example" | ||
version = "0.1.0" | ||
authors = ["Human Panic Authors <[email protected]>"] | ||
|
||
[dependencies] | ||
human-panic = { path = "../..", features = ["gui"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters