-
Notifications
You must be signed in to change notification settings - Fork 255
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
Use std::panic::Location
for file & line information
#410
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 |
src/macros.rs
Outdated
if cfg!(feature = "track_caller") { | ||
::std::panic::Location::caller().file() | ||
} else { | ||
file!() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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!()
}
src/macros.rs
Outdated
file!() | ||
#[cfg(feature = "track_caller")] | ||
{ | ||
::std::panic::Location::caller().file() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -48,6 +48,9 @@ std = [] | |||
kv_unstable = [] | |||
kv_unstable_sval = ["kv_unstable", "sval/fmt"] | |||
|
|||
# requires 1.46.0 | |||
track_caller = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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::Location
instead 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