Is there any logging library can log stack traceback when panicked or assertion failed?

Hi all, I have a Rust program that runs in the background.

Now all logs were logged to stdout/stderr, but that program will run as a service, and I cannot capture the stdout/stderr to a file.

Sometimes the Rust program panicked, and I had no way to inspect the log (since everything was logger to stdout/stderr).

I guess the program was panicked, but I don't know how to capture the stack traceback and log them to a file (as normal log), so I can inspect them later.

Is there any logging library can log stack traceback when panicked or assertion failed?

Any logging library could, in theory. The functionality you're looking for is panic::set_hook.

As an off-the-shelf solution, tracing-panic provides a simple hook to emit a tracing error! when a panic occurs. It doesn't currently try to capture a backtrace, though.

4 Likes

this also might be interesting:

3 Likes

Hi, all. I happened to use the GitHub - athre0z/color-backtrace: Colorful panic backtraces for Rust to capture stack backtrace and write them into a file.

struct FileStream {
    file: std::fs::File,
}

impl FileStream {
    fn new<S: AsRef<std::path::Path>>(s: S) -> std::io::Result<Self> {
        let file = std::fs::File::create(s)?;
        Ok(Self {
            file,
        })
    }
}

impl std::io::Write for FileStream {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.file.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.file.flush()
    }
}

impl color_backtrace::termcolor::WriteColor for FileStream {
    fn supports_color(&self) -> bool {
        false
    }

    fn set_color(&mut self, _spec: &color_backtrace::termcolor::ColorSpec) -> std::io::Result<()> {
        Ok(())
    }

    fn reset(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

// Usage:
let file_name = "log.txt";
color_backtrace::BacktracePrinter::new().install(FileStream::new(file_name)?);

// Test
assert(false);
The application panicked (crashed).
Message:  assertion failed: false
Location: src/main.rs:70

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                              ⋮ 8 frames hidden ⋮                               
 9: core::panicking::panic::h9533b2fee90b999e
    at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:114
10: rust_test::main::h1ebdd6f9888ab9d8
    at /home/foo/CLionProjects/rust-test/src/main.rs:70
      68 │ 
      69 │     std::thread::sleep(std::time::Duration::from_secs(1));
      70 >     assert!(false);
      71 │ 
      72 │     Ok(())
11: core::ops::function::FnOnce::call_once::hea2abc8887e91585
    at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/function.rs:250
                              ⋮ 16 frames hidden ⋮                              
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.