Make MOZ_LOG support printf style and streaming style log modes
Categories
(Core :: XPCOM, defect, P3)
Tracking
()
People
(Reporter: kanru, Assigned: kats)
References
(Blocks 1 open bug)
Details
Attachments
(2 obsolete files)
Updated•7 years ago
|
Assignee | ||
Updated•4 years ago
|
Assignee | ||
Comment 1•3 years ago
|
||
Stealing, since I have a WIP for this now.
Unfortunately with doing MOZ_LOG(logger, level) << message;
there's no way (at least that I could find) to prevent running the <<
operator implementations in the case where the MOZ_LOG_TEST
fails. So even with logging disabled there would be a nontrivial perf impact, which is undesirable. Instead I wrote an implementation works like MOZ_LOG(logger, level, (message << object << whatever));
and the ergonomics of that seem good enough that I think it's worth landing.
Assignee | ||
Comment 2•3 years ago
|
||
Assignee | ||
Comment 3•3 years ago
|
||
Depends on D117107
Comment 4•3 years ago
|
||
Just as a point of general interest, the new hotness in C++ string formatting is std::format
, which combines the type safety of iostreams with the inline format string syntax of printf (but without the burden of specifying types in the format string, since the types can be inferred from the arguments). Basically like Rust.
It's a C++20 library feature, though, and implementing something like it may also require C++20 language features (though I haven't investigated to be sure), which we can't use just yet.
Assignee | ||
Comment 5•3 years ago
|
||
Ah good to know! FWIW the main goal for me in implementing this was to try and get rid of the ToString(<thing>).c_str()
calls we have in a lot of places for MOZ_LOG
invocations. Stuff like this:
MOZ_LOG(sDisplayportLog, LogLevel::Verbose,
("SetDisplayPortMargins %s on scrollId=%" PRIu64 ", newDp=%s\n",
ToString(aMargins).c_str(), viewID,
ToString(newDisplayPort).c_str()));
can be rewritten to just dump aMargins
and newDisplayPort
directly into the stream with the new streaming MOZ_LOG. With the std::format
hotness, is there any equivalent of the Display
or Debug
trait, that would allow structs to serialize themselves in a way that std::format
can use?
Comment 6•3 years ago
|
||
(In reply to Kartikaya Gupta (email:kats@mozilla.staktrace.com) from comment #5)
With the
std::format
hotness, is there any equivalent of theDisplay
orDebug
trait, that would allow structs to serialize themselves in a way thatstd::format
can use?
Yep, you can specialize std::formatter<T>
for your type and give that a format()
method that performs your type-specific formatting.
Comment 7•3 years ago
|
||
(There isn't an equivalent to #[derive(Debug)]
; that will require reflection, hopefully to forthcome in C++23.)
Comment 8•3 years ago
|
||
I've filed bug 1717448 to track potentially adding the {fmt}
library to allow using the c++20 std::format
syntax early, which I think I would prefer as a solution to improve logging ergonomics over this proposal.
Assignee | ||
Updated•3 years ago
|
Updated•3 years ago
|
Updated•3 years ago
|
Assignee | ||
Comment 9•3 years ago
|
||
Closing this in favour of bug 1717448.
Description
•