Some improvements of logging/debug printing with {fmt}

22 views
Skip to first unread message

Masayuki Nakano

unread,
Jun 15, 2026, 6:51:22 AM (2 days ago) Jun 15
to dev-pl...@mozilla.org

Hi,

`RefPtr`, `nsCOMPtr`, `mozilla::OwningNonNull`, `mozilla::StaticAutoPtr` and `mozilla::StaticRefPtr` become formattable with {fmt}, i.e., `fmt::format` and `MOZ_LOG_FMT`. So, you can now write as:

```cpp

RefPtr<Foo> bar = MakeRefPtr<Foo>();
NS_WARNING(fmt::format("{}", bar);

```

So, you don't need to use `mozilla::ToString`.


    

The format is, if it's non-null, redirect to the formatter or `operator<<` of the type and prints an at-mark and the pointer's address, the result is the same as the traditional `RefPtr`, but now prints the instance prettier if the class is formattable but there is no `operator<<` overload for the class.

Additionally, I improved `nsINode`'s printing code.

First, `nsINode` is now formattable, i.e., you don't need to use `mozilla::ToString` for printing its reference.

Next, there are some additional information.

Previously, it prints its path in the subtree if it's not connected to the document. The new code will print all ancestors with crossing the shadow DOM boundaries.

Next, if the node is an unassigned node to any <slot> even though it's an slottable node (i.e., a child of a shadow host element), then, when printing the parent shadow host element, the new code will print the element has a shadow.

And also if the node is a `CharacterData` like a `Text` or `Comment`, it will print the data at most 8 characters. However, for the privacy concern, the data is not printed if the node is in the UA shadow of a text control element like an <input type="text"> or <textarea>.

Finally, I tried to make `mozilla::Maybe` formattable, but oddly, {fmt} does not treat it as formattable even if I define `format_as` explicitly. Therefore, you're welcome to fix this issue in bug 2047496 😉

Thank you!

-- 
Masayuki Nakano <masa...@d-toybox.com>
Working on DOM, Events, editor and IME handling for Gecko
Reply all
Reply to author
Forward
0 new messages