Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print a backtrace/spantrace to debug logs but not to stderr #201

Open
gibfahn opened this issue Sep 16, 2024 · 1 comment
Open

Print a backtrace/spantrace to debug logs but not to stderr #201

gibfahn opened this issue Sep 16, 2024 · 1 comment

Comments

@gibfahn
Copy link

gibfahn commented Sep 16, 2024

(migrated from eyre-rs/color-eyre#130)

Sorry if this is in the docs/examples and I missed it, but I'm struggling to work out how I would manually generate a backtrace or a spantrace from a color_eyre::Result.

The use-case is a user-facing CLI, where I write trace logs to a file. When something goes wrong printing a backtrace tends to confuse users, and makes them even more likely to miss the Suggestion: I added that tells them exactly what to do 😁

However the backtrace is useful for me, when I didn't add enough context to each layer of the error stack to see where the error propagated.

The CLI tells people to ask for support and attach the log file in the issue, and so I'd like to tracing::debug!() log the spantrace so it persists in the log file.

@gibfahn
Copy link
Author

gibfahn commented Sep 16, 2024

I tried a few more things, and as far as I can tell:

A spantrace is mostly useful if you have #[instrument] calls in all your functions. Setting this up means the compact tracing logs start including the function name in them (with as far as I can tell no way to disable it). So avoiding this for now.

A backtrace is more likely to be what I want, but the RUST_BACKTRACE style env vars are only checked at color_eyre setup (config), so changing them later doesn't do anything.

I think what I want is something like this:

    if std::env::var("RUST_BACKTRACE").is_err() {
        std::env::set_var("RUST_BACKTRACE", "1");
    }

    // Rest of program execution goes here.

    // Print the backtrace.
    debug!("Run result: {result}");
    if let Err(report) = result.as_mut() {
        if let Some(handler) = report.handler_mut().downcast_mut::<color_eyre::Handler>() {
            if let Some(backtrace) = handler.backtrace_mut() {
                // Remove the backtrace.
                let _ = backtrace.take();
            }
        }
    }

	// Return the program result, not printing the backtrace.
    return result;

but handler.backtrace_mut() isn't a thing.

Originally posted by @gibfahn in eyre-rs/color-eyre#130 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant