Closed Bug 1334042 Opened 8 years ago Closed 3 years ago

Make MOZ_LOG support printf style and streaming style log modes

Categories

(Core :: XPCOM, defect, P3)

52 Branch
defect

Tracking

()

RESOLVED WONTFIX

People

(Reporter: kanru, Assigned: kats)

References

(Blocks 1 open bug)

Details

Attachments

(2 obsolete files)

See bug 1333228 comment 5: We'd have the current printf style: > MOZ_LOG(logger, level, (message)); and a streaming style: > MOZ_LOG(logger, level) << message;
Priority: -- → P3

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: kanru → kats

Depends on D117107

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.

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?

(In reply to Kartikaya Gupta (email:kats@mozilla.staktrace.com) from comment #5)

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?

Yep, you can specialize std::formatter<T> for your type and give that a format() method that performs your type-specific formatting.

(There isn't an equivalent to #[derive(Debug)]; that will require reflection, hopefully to forthcome in C++23.)

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.

Attachment #9225785 - Attachment is obsolete: true
Attachment #9225786 - Attachment is obsolete: true

Closing this in favour of bug 1717448.

Status: NEW → RESOLVED
Closed: 3 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: