log icon indicating copy to clipboard operation
log copied to clipboard

Use `std::panic::Location` for file & line information

Open pwoolcoc opened this issue 5 years ago • 8 comments

this commit adds a feature, track_caller, that allows the user to opt-in to using location information from std::panic::Location instead of the built-in file! and line! macros. This will allow log record location information to be manipulated in the same way that #[track_caller] allows panic location information to be manipulated

pwoolcoc avatar Aug 27 '20 19:08 pwoolcoc

Is there a benefit to doing this?

Thomasdezeeuw avatar Aug 27 '20 19:08 Thomasdezeeuw

Ah, sorry didn't see the second part of your comment, ignore my previous comment.

Thomasdezeeuw avatar Aug 27 '20 19:08 Thomasdezeeuw

It allows you to manipulate location information for log messages in the same way that you can for panic! messages. For example, one of my applications has a generic "retry-er" that takes a closure and a retry policy, and will run the closure until it gets a successful return value from the closure or until it hits it's max retries. Currently, any logging we do from the retryer (like "got error Foo, retry 1/10") will include location information from inside the retryer, but no identifying information about exactly which closure generated the error. Of course, we could simply pass more information to the retryer to help us identify the piece of code that is erroring out, but I thought it would be possible to use #[track_caller] for this instead, to keep the API cleaner. Turns out I couldn't use log::error!, log::warn!, etc, because those macros use file!() and line!(). I can do it if I write my own log::Record-generating code, but I thought having this functionality built-in to log might be useful for others.

pwoolcoc avatar Aug 27 '20 19:08 pwoolcoc

r? @sfackler @jdm

pwoolcoc avatar Sep 01 '20 14:09 pwoolcoc

My earlier concern has been addressed, but otherwise I'm just a bystander :)

jdm avatar Sep 01 '20 17:09 jdm

no problem, thanks!

pwoolcoc avatar Sep 01 '20 18:09 pwoolcoc

bump, can someone take a look at this?

pwoolcoc avatar Sep 15 '20 13:09 pwoolcoc

Just coming in through some triage. I think the only outstanding thing here was using our build script to automatically use track_caller if we're on at least 1.46.0.

KodrAus avatar Nov 15 '21 23:11 KodrAus