Add pub "-v" macros for inline expression logging #316
No reviewers
Labels
No labels
bug
duplicate
easy
enhancement
help wanted
invalid
question
rfc
structured-logging
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-rust-lang/log!316
Loading…
Reference in a new issue
No description provided.
Delete branch "debugv-macros"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
These new macro's were inspired by
std::dbg!(just released in rust 1.32). Usage is the same, but these send output to the log instead of stderr, at theDebugorTracelevel. Onlystd::stringifyorcore::stringifyis required for the implementation, so this doesn't increase MSRV as these are "since 1.0.0".Includes doctests and an external no-harness test.
Works with or without std, per testing.
Hi @dekellum 👋
This is a neat idea! I'm not sure we need to provide it directly in
logthough, it's something that could be provided by a crate externally.Edit: The below rationale is largely superseded by RFC #317.
Fair point. May I offer a rationale for including these macros here in
log?Background
The
dbg!macro RFC: rust-lang/rfcs#2361.In an executable project where configuring
logand an output logger implementation hasn't (yet) happened, anyone can conveniently usestd::dbg!, with no initial setup cost, forexpression = valueprinting to stderr. From the linked rustdoc:Indeed, a major point of the
logpackage andLevels, is the ability to keepdebug!andtrace!logging in place for possible further use, including by other contributors, without payinga cost for unlogged messages in release builds.
Its seems to follow that for executable projects that do already have
logand an output logger dependency and configuration, with debug/trace logging already in place to help, that the use ofstd::dbg!would certainly be unwelcome in PRs and likely less productive than alog-based equivalent.However,
logdoesn't currently have the same convenience macro(s) for logging expressions and passing through values inline.Why add this to
log?The proposed
debugv!andtracev!macros play a very similar role, for example to the existingdebug!macro inlog, which is just more convenient than usinglog!(Level::Debug, …)with an extra import.Ease of use was an important part of the design and decision to add
dbg!to ruststdand the prelude.Comment by the author of RFC 2361:
While the proposed
logequivalent would still require an import, adding this feature tologavoids:Needing to discover, add, and maintain an additional library dependency. Discovery I think would be the biggest issue, and the inclusion of
dbg!instdand the prelude raises a high standard for parity.The community effort to maintain such a separate library with compatibility to the
logcrate, as it evolves. This may sound trivial, but for example, I would think thatdebugv!andtracev!will need some work for parity with your structured logging work (#296). Its not clear to me, that this kind of external extension to theloginterface has been factored into your design?Alternative: cut-n’-paste macro reuse
As adding this feature to
logmight warrant a MINOR version bump, and/or you may want to take the time to further consider including this after more community feedback, a stop gap solution is available for those that want to try these convenience macros sooner in their own code base:Though I wouldn't recommend this, as it might cause more confusion than it avoids,
debugvcould even be locally nameddbgto shadow the preludestd::dbgversion.I offer these reduced code snippets under the same permisive licenses as this PR's version and
log! My inclination is to leave it at that, allowing you time to consider adding these tologdirectly. Thanks for your consideration and work onlog!Thanks for your thoughts @dekellum!
This is something that should be possible to support using structured logging (that would be preferable over interpolation into the message), however that would probably require some breakage.
I think the issue of discoverability is a little different for
logthan forstd, because users have already committed to pulling in dependencies when they're usinglog. We could also make an effort to call out extensions of this kind in the readme, like we already do for frameworks.Combined with additional freedom to iterate on the API (I can imagine requests for
infov,warnv,errorvetc suggesting this feature should be somehow rolled into the existing log macros) and the fact that this is possible to build on top oflog, I would suggest the external crate approach first.Just a follow-up: I think your rationale is fair and thorough, and that this is something we should support somehow, I just feel like we need to give it time and space to iterate before committing to new API in
log.I think its fair that you would want any public macro additions to be good and as stable as possible before adding to
log. While I wanted to offer this PR sooner, as a proof-of-concept and to get it on the radar, I am reluctant to do the work for a new macro-only mini-crate, when the same or better version of this feature should later land inlog!Adding the remaining levels for the feature is reasonable, though it goes beyond strict parity with
dbg!. That could be structured better with the bulk of the feature in one macro (like howlog!is now).Would you entertain a hopefully mini-RFC just focusing on the potential API additions? That discussion would be neutral to the
logcrate or another crate.That sounds like a good idea!
Would someone in the know, like @dtolnay, be able to review the macro additions specifically?
I feel like I'm still a
macro amateur, err, an amateur at macros—I've learned much in the process, but I'm most certainly unpaid . Thanks in advance!I am an unpaid amateur too but this didn't work for me:
Oh boy, that's embarrassing as it seems like something my tests should have caught, since they do test
logv!. Can you share any more details of your environment, rust version? Might the difference be that my tests (to be compatible with older rust), still use#[macro_use]? I must not understand something about the workound and comments you previously added for duel 2015/2018 edition support.edit: I will write another 2018 specific test, in any case, thanks!
It was simpler then that—I had just missed specifying the
local_inner_macrosonlogv. (1) Sorry to divert your time with this, @dtolnay. Is there anything more generally concerning in how these macros are constructed?One related item I'm not sure on is that, currently, the
-vmacros will refuse a trailing comma. Simple/nice handling of that would presumably be with rust-lang/rust#48075, e.g.$(,)?but that is 2018-edition only, and for rust 1.32+. With the limits of my knowledge, I'm inclined to leave that as is for now, with a comment, and awaiting a future MSRV 1.32 and 2018 version. Thoughts?(1) I hadn't fully internalized that there are were no tests currently in log, that actually test 2018-edition style macro import. That could be fixed, likely best as a follow-on PR so as to include all existing public macros.
The implementation looks correct now. (Note: I only looked at the new macro_rules macros in src/macros.rs, not any of the other changes.)
I am not yet sold on adding all these macros. I left a comment on the RFC PR since that seems the more appropriate place for that discussion.
@ -15,36 +15,152 @@//! by this crate, and the consumer of those libraries can choose the loggingIs this true? The following appears to compile even though String does not implement Copy:
@ -15,36 +15,152 @@//! by this crate, and the consumer of those libraries can choose the loggingGood catch, thanks, I'll change this to mention move semantics.
I'm going to close this for now, as it only implements an earlier version of RFC #317: the single, complete format parameter alternative. When and if RFC #317 is approved, a new PR can be submitted for a complete implementation.
Thanks for review feedback.
Pull request closed