Use std::panic::Location for file & line information#410
Conversation
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
|
Is there a benefit to doing this? |
|
Ah, sorry didn't see the second part of your comment, ignore my previous comment. |
|
It allows you to manipulate location information for log messages in the same way that you can for |
| if cfg!(feature = "track_caller") { | ||
| ::std::panic::Location::caller().file() | ||
| } else { | ||
| file!() | ||
| } |
There was a problem hiding this comment.
This will break compilation of log with rustc < 1.46, even without the track_caller feature enabled. The following will allow compilation to succeed without that feature enabled:
#[cfg(feature = "track_caller")]
{
::std::panic::Location::caller().file()
}
#[cfg(not(feature = "track_caller"))]
{
file!()
}
| file!() | ||
| #[cfg(feature = "track_caller")] | ||
| { | ||
| ::std::panic::Location::caller().file() |
There was a problem hiding this comment.
This cfg is evaluated in the context of the crate that invokes a log macro, not the context of the log crate so this won't work.
There was a problem hiding this comment.
thanks @sfackler, I pushed an update so they're evaluated in the correct context
|
My earlier concern has been addressed, but otherwise I'm just a bystander :) |
|
no problem, thanks! |
|
bump, can someone take a look at this? |
KodrAus
left a comment
There was a problem hiding this comment.
Thanks @pwoolcoc! If MSRVs weren't a concern this is probably something we'd want unconditionally, wouldn't we? If that's the case I think we could use our build.rs to detect 1.46.0 support and just enable it quietly instead of using an explicit feature.
| kv_unstable_sval = ["kv_unstable", "sval/fmt"] | ||
|
|
||
| # requires 1.46.0 | ||
| track_caller = [] |
There was a problem hiding this comment.
I think we could detect 1.46.0 in our build.rs and quietly enable this.
|
Just coming in through some triage. I think the only outstanding thing here was using our build script to automatically use |
Bumps [thiserror](https://github.com/dtolnay/thiserror) from 1.0.34 to 1.0.35. - [Release notes](https://github.com/dtolnay/thiserror/releases) - [Commits](dtolnay/thiserror@1.0.34...1.0.35) --- updated-dependencies: - dependency-name: thiserror dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
|
Since this PR was left off, the MSRV has been bumped to 1.60. I think that means this can/should be turned on unconditionally! |
|
@pwoolcoc is this something you're still interested in? We've updated our MSRV to 1.60 so this can be done now. |
|
@Thomasdezeeuw yes, but as I deleted my original fork, I can't seem to update this PR, so I'll have to open a new PR. |
|
Closing in favor of #599 |
this commit adds a feature,
track_caller, that allows the user to opt-in to using location information fromstd::panic::Locationinstead of the built-infile!andline!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