Skip to content

Commit

Permalink
OrcLib: Fmt: error_code: display as unsigned errors from system category
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Nov 12, 2020
1 parent 502cfa3 commit ae99bf1
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/OrcLib/Output/Text/Fmt/Fwd/error_code.h
Expand Up @@ -31,25 +31,59 @@ struct fmt::formatter<std::error_code, wchar_t> : public fmt::formatter<std::wst
template <typename FormatContext>
auto fmt::formatter<std::error_code>::format(const std::error_code& ec, FormatContext& ctx) -> decltype(ctx.out())
{
const auto message = ec.message();
if (message.size())
std::string s;

if (ec.category() == std::system_category())
{
fmt::format_to(std::back_inserter(s), "{:#x}", static_cast<uint32_t>(ec.value()));
}
else
{
return fmt::format_to(ctx.out(), "{:#x}: {}", ec.value(), message);
fmt::format_to(std::back_inserter(s), "{}", ec.value());
}

auto message = ec.message();
if (!message.empty())
{
auto pos = message.rfind("\r\n");
if (pos != std::string::npos && pos == message.size() - 2)
{
message = message.substr(0, pos);
}

fmt::format_to(std::back_inserter(s), ": {}", message);
}

return fmt::format_to(ctx.out(), "{:#x}", ec.value());
return formatter<std::string_view>::format(s, ctx);
}

template <typename FormatContext>
auto fmt::formatter<std::error_code, wchar_t>::format(const std::error_code& ec, FormatContext& ctx)
-> decltype(ctx.out())
{
const auto message = ec.message();
if (message.size())
std::wstring s;

if (ec.category() == std::system_category())
{
fmt::format_to(std::back_inserter(s), L"{:#x}", static_cast<uint32_t>(ec.value()));
}
else
{
fmt::format_to(std::back_inserter(s), L"{}", ec.value());
}

auto message = ec.message();
if (!message.empty())
{
auto pos = message.rfind("\r\n");
if (pos != std::string::npos && pos == message.size() - 2)
{
message = message.substr(0, pos);
}

const auto utf16 = Orc::Utf8ToUtf16(message, Orc::kFailedConversionW);
return fmt::format_to(ctx.out(), L"{:#x}: {}", ec.value(), utf16);
fmt::format_to(std::back_inserter(s), L": {}", utf16);
}

return fmt::format_to(ctx.out(), L"{:#x}", ec.value());
return formatter<std::wstring_view, wchar_t>::format(s, ctx);
}

0 comments on commit ae99bf1

Please sign in to comment.