tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.14k stars 671 forks source link

Attribute for logging every line in the function #2991

Closed amab8901 closed 1 month ago

amab8901 commented 1 month ago

Feature Request

Motivation

I often encounter the situation where there's a bug and I'm not sure where the bug is coming from. This proposed feature would save me lots of time since it would let me apply and cleanup these bughunting logs more quickly by doing it via a function attribute instead of manually having to write it for each line.

Proposal

This attribute:

#[warn]`
fn hello() {
    let cool = something();
    let crab = is(cool);
    let ferris = the(crab);
   return ferris
}

Is shorthand for this:

fn hello() {
    warn!("{}:{}", file!(), line!());
    let cool = something();
    warn!("{}:{}", file!(), line!());
    let crab = is(cool);
    warn!("{}:{}", file!(), line!());
    let ferris = the(crab);
    warn!("{}:{}", file!(), line!());
   return ferris
}

(and similar for the other log levels)

This lets the user add and remove those log lines more quickly to debug their program more quickly. A drawback may be a slightly steeper learning curve for people who haven't used the tracing crate before.

Alternatives

Writing warn!() manually, as well as println!, dbg!. These approaches work, but they are tedious relative to the suggested feature

mladedav commented 1 month ago

Another alternative would be to use a debugger if you need to know whether you're hitting each line.

Anyway, I don't think macros can even be used like this, the line macro would just print the line ofnthe attribute itself as documented.

davidbarsky commented 1 month ago

Yeah, as @mladedav said, you're better off using a debugger here. tracing's is meant to be used as an intentional, human-directed "something happened here"-esque tool.