From a845ff9da536bba8224e022b1d6266ce35d1debf Mon Sep 17 00:00:00 2001 From: RustyNova Date: Thu, 6 Feb 2025 15:34:38 +0000 Subject: [PATCH] feat: add std example --- Cargo.toml | 4 ++++ examples/fmt_to_string_std.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 examples/fmt_to_string_std.rs diff --git a/Cargo.toml b/Cargo.toml index 47891b1..d9f7bee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,7 @@ btparse = { git = "https://github.com/yaahc/btparse.git", rev = "54f9ddb8c7c8f8e [[example]] name = "fmt_to_string" required-features = ["use-backtrace-crate"] + +[[example]] +name = "fmt_to_string_std" +required-features = ["use-btparse-crate"] \ No newline at end of file diff --git a/examples/fmt_to_string_std.rs b/examples/fmt_to_string_std.rs new file mode 100644 index 0000000..b5f0b0e --- /dev/null +++ b/examples/fmt_to_string_std.rs @@ -0,0 +1,25 @@ +use color_backtrace::BacktracePrinter; + +fn main() -> Result<(), std::io::Error> { + // Start your trace + let trace = std::backtrace::Backtrace::force_capture(); + + // Do stuff... + + // And print! + let bt = color_backtrace::btparse::deserialize(&trace).unwrap(); + let printer = BacktracePrinter::default(); + let str = printer.format_trace_to_string(&bt)?; + + if cfg!(windows) { + println!( + "Warning: on Windows, you'll have to enable VT100 \ + printing for your app in order for this to work \ + correctly. This example doesn't do this." + ); + } + + println!("{}", str); + + Ok(()) +}