Skip to content

First class <format>/fmtlib support #51

@BurningEnlightenment

Description

@BurningEnlightenment

Problem Description

Since <iostream> has been superseeded in C++20/23 as the preferred stringification/formatting API, I would like to use either fmtlib or std::format to format error messages/log output. This is currently possible, but looks like this:

SYSTEM_ERROR2_NAMESPACE::system_code code = /*...*/;
std::print("xx API returned [{}|{}]", code.domain().name().c_str(), code.message().c_str());

And the above might still be problematic as it looses the exact error code value and the domain Id.

Proposed Solution

Add a format_support.hpp header similar to the existing iostream_support.hpp which provides the respective formatter specializations for the following types after detecting the presence of C++20 <format> and fmtlib:

  • status_code_domain::string_ref (can be delegated to (std|fmt)::formatter<string_view>)
  • status_code_domain
  • generic_code and its errored_status_code variant
  • status_code<detail::erased<ErasedType>> and errored_status_code<detail::erased<ErasedType>>
  • the native system codes e.g. POSIX, NT, etc.

The formatter domain/code specializations should support a formatting spec for specifying if textual or raw representations should be used. The status_code formatters should support a "full" output mode i.e. something like domain name + message.

Possible Alternative Approaches

Of course these specializations could be provided by downstream users. However, this doesn't compose well in case of diamond-shaped dependency DAGs.

Additional Context

I have implemented barebones formatter specializations for some of these types but without alternate formatting modes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions