client/Response: refactor FormatError() to use libfmt
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user