client/Response: refactor FormatError() to use libfmt

This commit is contained in:
Max Kellermann
2021-05-25 16:11:35 +02:00
parent 18efda719e
commit 0d97eba7a5
6 changed files with 56 additions and 33 deletions

View File

@@ -70,19 +70,17 @@ Response::WriteBinary(ConstBuffer<void> payload) noexcept
void
Response::Error(enum ack code, const char *msg) noexcept
{
FormatError(code, "%s", msg);
FmtError(code, FMT_STRING("{}"), msg);
}
void
Response::FormatError(enum ack code, const char *fmt, ...) noexcept
Response::VFmtError(enum ack code,
fmt::string_view format_str, fmt::format_args args) noexcept
{
Fmt(FMT_STRING("ACK [{}@{}] {{{}}} "),
(int)code, list_index, command);
std::va_list args;
va_start(args, fmt);
FormatV(fmt, args);
va_end(args);
VFmt(format_str, std::move(args));
Write("\n");
}

View File

@@ -105,7 +105,23 @@ public:
bool WriteBinary(ConstBuffer<void> payload) noexcept;
void Error(enum ack code, const char *msg) noexcept;
void FormatError(enum ack code, const char *fmt, ...) noexcept;
void VFmtError(enum ack code,
fmt::string_view format_str, fmt::format_args args) noexcept;
template<typename S, typename... Args>
void FmtError(enum ack code,
const S &format_str, Args&&... args) noexcept {
#if FMT_VERSION >= 70000
return VFmtError(code, fmt::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str,
args...));
#else
/* expensive fallback for older libfmt versions */
const auto result = fmt::format(format_str, args...);
return Error(code, result.c_str());
#endif
}
};
#endif