Skip to content

Commit

Permalink
Don't try to be compatible with std::format (for older spdlog compat)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepbluev7 committed Jan 12, 2023
1 parent b8f0e82 commit 79dcdbb
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions include/mtxclient/http/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ClientError
} // namespace mtx

template<>
struct spdlog::fmt_lib::formatter<mtx::http::ClientError>
struct fmt::formatter<mtx::http::ClientError>
{
// Presentation format: 'f' - fixed, 'e' - exponential.
bool print_network_error = false;
Expand All @@ -39,7 +39,7 @@ struct spdlog::fmt_lib::formatter<mtx::http::ClientError>
bool print_matrix_error = false;

// Parses format specifications of the form ['f' | 'e'].
constexpr auto parse(spdlog::fmt_lib::format_parse_context &ctx) -> decltype(ctx.begin())
constexpr auto parse(fmt::format_parse_context &ctx) -> decltype(ctx.begin())
{
// [ctx.begin(), ctx.end()) is a character range that contains a part of
// the format string starting from the format specifications to be parsed,
Expand Down Expand Up @@ -78,7 +78,7 @@ struct spdlog::fmt_lib::formatter<mtx::http::ClientError>

// Check if reached the end of the range:
if (it != end && *it != '}')
throw spdlog::fmt_lib::format_error("invalid format");
throw fmt::format_error("invalid format");

// Return an iterator past the end of the parsed range:
return it;
Expand All @@ -91,52 +91,51 @@ struct spdlog::fmt_lib::formatter<mtx::http::ClientError>
{
// ctx.out() is an output iterator to write to.
bool prepend_comma = false;
spdlog::fmt_lib::format_to(ctx.out(), "(");
fmt::format_to(ctx.out(), "(");
if (print_network_error || e.error_code) {
spdlog::fmt_lib::format_to(ctx.out(), "connection: {}", e.error_code_string());
fmt::format_to(ctx.out(), "connection: {}", e.error_code_string());
prepend_comma = true;
}

if (print_http_error ||
(e.status_code != 0 && (e.status_code < 200 || e.status_code >= 300))) {
if (prepend_comma)
spdlog::fmt_lib::format_to(ctx.out(), ", ");
spdlog::fmt_lib::format_to(ctx.out(), "http: {}", e.status_code);
fmt::format_to(ctx.out(), ", ");
fmt::format_to(ctx.out(), "http: {}", e.status_code);
prepend_comma = true;
}

if (print_parser_error || !e.parse_error.empty()) {
if (prepend_comma)
spdlog::fmt_lib::format_to(ctx.out(), ", ");
spdlog::fmt_lib::format_to(ctx.out(), "parser: {}", e.parse_error);
fmt::format_to(ctx.out(), ", ");
fmt::format_to(ctx.out(), "parser: {}", e.parse_error);
prepend_comma = true;
}

if (print_parser_error ||
(e.matrix_error.errcode != mtx::errors::ErrorCode::M_UNRECOGNIZED &&
!e.matrix_error.error.empty())) {
if (prepend_comma)
spdlog::fmt_lib::format_to(ctx.out(), ", ");
spdlog::fmt_lib::format_to(ctx.out(),
"matrix: {}:'{}'",
to_string(e.matrix_error.errcode),
e.matrix_error.error);
fmt::format_to(ctx.out(), ", ");
fmt::format_to(ctx.out(),
"matrix: {}:'{}'",
to_string(e.matrix_error.errcode),
e.matrix_error.error);
}

return spdlog::fmt_lib::format_to(ctx.out(), ")");
return fmt::format_to(ctx.out(), ")");
}
};

template<>
struct spdlog::fmt_lib::formatter<std::optional<mtx::http::ClientError>>
: formatter<mtx::http::ClientError>
struct fmt::formatter<std::optional<mtx::http::ClientError>> : formatter<mtx::http::ClientError>
{
// parse is inherited from formatter<string_view>.
template<typename FormatContext>
auto format(std::optional<mtx::http::ClientError> c, FormatContext &ctx)
{
if (!c)
return spdlog::fmt_lib::format_to(ctx.out(), "(no error)");
return fmt::format_to(ctx.out(), "(no error)");
else
return formatter<mtx::http::ClientError>::format(*c, ctx);
}
Expand Down

0 comments on commit 79dcdbb

Please sign in to comment.