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

@@ -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