Skip to content

Commit

Permalink
Port failure support & add backward compat shims
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z committed Apr 30, 2020
1 parent f31fd90 commit f7afbc2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/custom_message.rs
@@ -1,5 +1,5 @@
fn main() {
use color_backtrace::{default_output_stream, install_with_settings, Settings};
install_with_settings(Settings::new().message("Custom message!"));
use color_backtrace::PanicPrinter;
PanicPrinter::new().message("Custom message!").install();
assert_eq!(1, 2);
}
4 changes: 2 additions & 2 deletions examples/failure.rs
@@ -1,4 +1,4 @@
use color_backtrace::{failure::print_backtrace, Settings};
use color_backtrace::{failure::print_backtrace, PanicPrinter};
use failure::{format_err, Fallible};

fn calc_things() -> Fallible<()> {
Expand All @@ -13,7 +13,7 @@ fn main() -> Result<(), std::io::Error> {
if let Err(e) = do_some_stuff() {
// oh noez!
unsafe {
print_backtrace(&e.backtrace(), &mut Settings::new())?;
print_backtrace(&e.backtrace(), &mut PanicPrinter::new())?;
}
}

Expand Down
18 changes: 8 additions & 10 deletions src/failure.rs
@@ -1,5 +1,7 @@
//! Temporary hack to allow printing of `failure::Backtrace` objects.

use crate::default_output_stream;

struct FakeBacktrace {
internal: FakeInternalBacktrace,
}
Expand Down Expand Up @@ -33,19 +35,15 @@ pub unsafe fn backdoortrace(_opaque: &failure::Backtrace) -> Option<&backtrace::
None
}

/// Extracts the internal `backtrace::Backtrace` from a `failure::Backtrace` and prints it, if one
/// exists. Prints that a backtrace was not capture if one is not found.
#[deprecated(
since = "0.4",
note = "Use `PanicPrinter::print_failure_trace` instead."
)]
pub unsafe fn print_backtrace(
trace: &failure::Backtrace,
settings: &mut crate::Settings,
printer: &crate::PanicPrinter,
) -> crate::IOResult {
let internal = backdoortrace(trace);

if let Some(internal) = internal {
super::print_backtrace(internal, settings)
} else {
writeln!(settings.out, "<failure backtrace not captured>")
}
printer.print_failure_trace(trace, &mut default_output_stream())
}

#[cfg(test)]
Expand Down
45 changes: 35 additions & 10 deletions src/lib.rs
Expand Up @@ -87,12 +87,13 @@ impl Verbosity {
// [Panic handler and install logic] //
// ============================================================================================== //

/// Install the `color_backtrace` handler with default settings.
/// Install a `PanicPrinter` handler with `::default()` settings.
///
/// This currently is a convenience shortcut for writing
///
/// ```rust
/// color_backtrace::PanicPrinter::default().install()
/// use color_backtrace::PanicPrinter;
/// PanicPrinter::default().install()
/// ```
pub fn install() {
PanicPrinter::default().install();
Expand All @@ -110,9 +111,6 @@ pub fn default_output_stream() -> Box<StandardStream> {
}))
}

/// Create a `color_backtrace` panic handler.
///
/// This can be used if you want to combine the handler with other handlers.
#[deprecated(
since = "0.4",
note = "Use `PanicPrinter::into_panic_handler()` instead."
Expand All @@ -131,7 +129,6 @@ pub fn create_panic_handler(
})
}

/// Install the `color_backtrace` handler with custom settings.
#[deprecated(since = "0.4", note = "Use `PanicPrinter::install()` instead.")]
pub fn install_with_settings(printer: PanicPrinter) {
std::panic::set_hook(printer.into_panic_handler(default_output_stream()))
Expand Down Expand Up @@ -453,10 +450,7 @@ impl PanicPrinter {
}
}

// ============================================================================================== //
// [Panic printing] //
// ============================================================================================== //

/// Routines for putting the panic printer to use.
impl PanicPrinter {
/// Install the `color_backtrace` handler with default settings.
pub fn install(self) {
Expand Down Expand Up @@ -543,6 +537,23 @@ impl PanicPrinter {
Ok(())
}

/// Extracts the internal `backtrace::Backtrace` from a `failure::Backtrace` and prints it, if
/// one exists. Prints that a backtrace was not capture if one is not found.
#[cfg(feature = "failure-bt")]
pub unsafe fn print_failure_trace(
&self,
trace: &::failure::Backtrace,
out: &mut impl WriteColor,
) -> IOResult {
let internal = failure::backdoortrace(trace);

if let Some(internal) = internal {
self.print_trace(internal, out)
} else {
writeln!(out, "<failure backtrace not captured>")
}
}

// TODO: documentation
pub fn print_trace_to_string(&self, trace: &backtrace::Backtrace) -> IOResult<String> {
let mut ansi = Ansi::new(vec![]);
Expand Down Expand Up @@ -613,3 +624,17 @@ impl PanicPrinter {
}

// ============================================================================================== //
// [Deprecated routines for backward compat] //
// ============================================================================================== //

#[deprecated(since = "0.4", note = "Use `PanicPrinter::print_trace` instead`")]
pub fn print_backtrace(trace: &backtrace::Backtrace, s: &mut PanicPrinter) -> IOResult {
s.print_trace(trace, &mut default_output_stream())
}

#[deprecated(since = "0.4", note = "Use `PanicPrinter::print_panic_info` instead`")]
pub fn print_panic_info(pi: &PanicInfo, s: &mut PanicPrinter) -> IOResult {
s.print_panic_info(pi, &mut default_output_stream())
}

// ============================================================================================== //

0 comments on commit f7afbc2

Please sign in to comment.