summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2021-05-25 16:11:35 +0200
committerMax Kellermann <max@musicpd.org>2021-05-27 15:15:47 +0200
commit0d97eba7a58ef6ff3b26ecd1b184bde100619de3 (patch)
treedd2ab805d8569f7f293ceab4aabff4b66f1958a4 /src/client
parent18efda719e3cb0797a88e58b8236287197dfb61e (diff)
client/Response: refactor FormatError() to use libfmt
Diffstat (limited to 'src/client')
-rw-r--r--src/client/Response.cxx10
-rw-r--r--src/client/Response.hxx18
2 files changed, 21 insertions, 7 deletions
diff --git a/src/client/Response.cxx b/src/client/Response.cxx
index f3f6f500e..20b48f8d7 100644
--- a/src/client/Response.cxx
+++ b/src/client/Response.cxx
@@ -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");
}
diff --git a/src/client/Response.hxx b/src/client/Response.hxx
index 4b6af9417..42d2f5a6e 100644
--- a/src/client/Response.hxx
+++ b/src/client/Response.hxx
@@ -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