client/Response: add method Fmt() based on libfmt

This commit is contained in:
Max Kellermann
2021-05-21 20:35:29 +02:00
parent a9c704b76e
commit 0440c41cba
27 changed files with 241 additions and 195 deletions

View File

@@ -48,6 +48,8 @@
#include "StickerCommands.hxx"
#endif
#include <fmt/format.h>
#include <cassert>
#include <iterator>
@@ -253,7 +255,7 @@ PrintAvailableCommands(Response &r, const Partition &partition,
if (cmd->permission == (permission & cmd->permission) &&
command_available(partition, cmd))
r.Format("command: %s\n", cmd->cmd);
r.Fmt(FMT_STRING("command: {}\n"), cmd->cmd);
}
return CommandResult::OK;
@@ -266,7 +268,7 @@ PrintUnavailableCommands(Response &r, unsigned permission) noexcept
const struct command *cmd = &i;
if (cmd->permission != (permission & cmd->permission))
r.Format("command: %s\n", cmd->cmd);
r.Fmt(FMT_STRING("command: {}\n"), cmd->cmd);
}
return CommandResult::OK;

View File

@@ -17,8 +17,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define __STDC_FORMAT_MACROS /* for PRIu64 */
#include "config.h"
#include "FileCommands.hxx"
#include "Request.hxx"
@@ -43,9 +41,10 @@
#include "thread/Mutex.hxx"
#include "Log.hxx"
#include <fmt/format.h>
#include <algorithm>
#include <cassert>
#include <cinttypes> /* for PRIu64 */
gcc_pure
static bool
@@ -61,13 +60,6 @@ skip_path(Path name_fs) noexcept
return name_fs.HasNewline();
}
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
/* PRIu64 causes bogus compiler warning */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif
CommandResult
handle_listfiles_local(Response &r, Path path_fs)
{
@@ -88,12 +80,12 @@ handle_listfiles_local(Response &r, Path path_fs)
continue;
if (fi.IsRegular())
r.Format("file: %s\n"
"size: %" PRIu64 "\n",
name_utf8.c_str(),
fi.GetSize());
r.Fmt(FMT_STRING("file: {}\n"
"size: {}\n"),
name_utf8,
fi.GetSize());
else if (fi.IsDirectory())
r.Format("directory: %s\n", name_utf8.c_str());
r.Fmt(FMT_STRING("directory: {}\n"), name_utf8);
else
continue;
@@ -135,9 +127,7 @@ public:
void OnPair(StringView key, StringView value) noexcept override {
if (IsValidName(key) && IsValidValue(value))
response.Format("%.*s: %.*s\n",
int(key.size), key.data,
int(value.size), value.data);
response.Fmt(FMT_STRING("{}: {}\n"), key, value);
}
};
@@ -228,11 +218,7 @@ read_stream_art(Response &r, const char *uri, size_t offset)
read_size = is->Read(lock, buffer.get(), buffer_size);
}
#ifdef _WIN32
r.Format("size: %lu\n", (unsigned long)art_file_size);
#else
r.Format("size: %" PRIoffset "\n", art_file_size);
#endif
r.Fmt(FMT_STRING("size: {}\n"), art_file_size);
r.WriteBinary({buffer.get(), read_size});
@@ -315,14 +301,10 @@ public:
return;
}
#ifdef _WIN32
response.Format("size: %lu\n", (unsigned long)buffer.size);
#else
response.Format("size: %zu\n", buffer.size);
#endif
response.Fmt(FMT_STRING("size: {}\n"), buffer.size);
if (mime_type != nullptr)
response.Format("type: %s\n", mime_type);
response.Fmt(FMT_STRING("type: {}\n"), mime_type);
buffer.size -= offset;

View File

@@ -37,6 +37,8 @@
#include "util/MimeType.hxx"
#include "util/UriExtract.hxx"
#include <fmt/format.h>
class GetChromaprintCommand final
: public ThreadBackgroundCommand, ChromaprintDecoderClient, InputStreamHandler
{
@@ -60,8 +62,8 @@ protected:
void Run() override;
void SendResponse(Response &r) noexcept override {
r.Format("chromaprint: %s\n",
GetFingerprint().c_str());
r.Fmt(FMT_STRING("chromaprint: {}\n"),
GetFingerprint());
}
void CancelThread() noexcept override {

View File

@@ -25,6 +25,8 @@
#include "util/ConstBuffer.hxx"
#include "Partition.hxx"
#include <fmt/format.h>
#include <cassert>
#include <set>
#include <string>
@@ -85,7 +87,7 @@ handle_channels(Client &client, [[maybe_unused]] Request args, Response &r)
}
for (const auto &channel : channels)
r.Format("channel: %s\n", channel.c_str());
r.Fmt(FMT_STRING("channel: {}\n"), channel);
return CommandResult::OK;
}
@@ -97,8 +99,8 @@ handle_read_messages(Client &client,
assert(args.empty());
client.ConsumeMessages([&r](const auto &msg){
r.Format("channel: %s\nmessage: %s\n",
msg.GetChannel(), msg.GetMessage());
r.Fmt(FMT_STRING("channel: {}\nmessage: {}\n"),
msg.GetChannel(), msg.GetMessage());
});
return CommandResult::OK;

View File

@@ -25,6 +25,8 @@
#include "neighbor/Glue.hxx"
#include "neighbor/Info.hxx"
#include <fmt/format.h>
#include <string>
bool
@@ -44,9 +46,9 @@ handle_listneighbors(Client &client, [[maybe_unused]] Request args, Response &r)
}
for (const auto &i : neighbors->GetList())
r.Format("neighbor: %s\n"
"name: %s\n",
i.uri.c_str(),
i.display_name.c_str());
r.Fmt(FMT_STRING("neighbor: {}\n"
"name: {}\n"),
i.uri,
i.display_name);
return CommandResult::OK;
}

View File

@@ -55,13 +55,15 @@
#include "db/update/Service.hxx"
#endif
#include <fmt/format.h>
#include <cassert>
static void
print_spl_list(Response &r, const PlaylistVector &list)
{
for (const auto &i : list) {
r.Format("playlist: %s\n", i.name.c_str());
r.Fmt(FMT_STRING("playlist: {}\n"), i.name);
if (!IsNegative(i.mtime))
time_print(r, "Last-Modified", i.mtime);
@@ -72,7 +74,7 @@ CommandResult
handle_urlhandlers(Client &client, [[maybe_unused]] Request args, Response &r)
{
if (client.IsLocal())
r.Format("handler: file://\n");
r.Write("handler: file://\n");
print_supported_uri_schemes(r);
return CommandResult::OK;
}
@@ -248,7 +250,7 @@ handle_update(Response &r, UpdateService &update,
const char *uri_utf8, bool discard)
{
unsigned ret = update.Enqueue(uri_utf8, discard);
r.Format("updating_db: %i\n", ret);
r.Fmt(FMT_STRING("updating_db: {}\n"), ret);
return CommandResult::OK;
}
@@ -258,7 +260,7 @@ handle_update(Response &r, Database &db,
{
unsigned id = db.Update(uri_utf8, discard);
if (id > 0) {
r.Format("updating_db: %i\n", id);
r.Fmt(FMT_STRING("updating_db: {}\n"), id);
return CommandResult::OK;
} else {
/* Database::Update() has returned 0 without setting
@@ -325,7 +327,7 @@ handle_getvol(Client &client, Request, Response &r)
const auto volume = volume_level_get(partition.outputs);
if (volume >= 0)
r.Format("volume: %i\n", volume);
r.Fmt(FMT_STRING("volume: {}\n"), volume);
return CommandResult::OK;
}
@@ -391,7 +393,7 @@ handle_config(Client &client, [[maybe_unused]] Request args, Response &r)
const Storage *storage = client.GetStorage();
if (storage != nullptr) {
const auto path = storage->MapUTF8("");
r.Format("music_directory: %s\n", path.c_str());
r.Fmt(FMT_STRING("music_directory: {}\n"), path);
}
#endif

View File

@@ -27,6 +27,8 @@
#include "client/Response.hxx"
#include "util/CharUtil.hxx"
#include <fmt/format.h>
CommandResult
handle_partition(Client &client, Request request, Response &response)
{
@@ -46,7 +48,7 @@ CommandResult
handle_listpartitions(Client &client, Request, Response &r)
{
for (const auto &partition : client.GetInstance().partitions) {
r.Format("partition: %s\n", partition.name.c_str());
r.Fmt(FMT_STRING("partition: {}\n"), partition.name);
}
return CommandResult::OK;

View File

@@ -38,6 +38,8 @@
#include "db/update/Service.hxx"
#endif
#include <fmt/format.h>
#define COMMAND_STATUS_STATE "state"
#define COMMAND_STATUS_REPEAT "repeat"
#define COMMAND_STATUS_SINGLE "single"
@@ -131,60 +133,60 @@ handle_status(Client &client, [[maybe_unused]] Request args, Response &r)
const auto volume = volume_level_get(partition.outputs);
if (volume >= 0)
r.Format("volume: %i\n", volume);
r.Fmt(FMT_STRING("volume: {}\n"), volume);
r.Format(COMMAND_STATUS_REPEAT ": %i\n"
COMMAND_STATUS_RANDOM ": %i\n"
COMMAND_STATUS_SINGLE ": %s\n"
COMMAND_STATUS_CONSUME ": %i\n"
"partition: %s\n"
COMMAND_STATUS_PLAYLIST ": %li\n"
COMMAND_STATUS_PLAYLIST_LENGTH ": %i\n"
COMMAND_STATUS_MIXRAMPDB ": %f\n"
COMMAND_STATUS_STATE ": %s\n",
playlist.GetRepeat(),
playlist.GetRandom(),
SingleToString(playlist.GetSingle()),
playlist.GetConsume(),
partition.name.c_str(),
(unsigned long)playlist.GetVersion(),
playlist.GetLength(),
(double)pc.GetMixRampDb(),
state);
r.Fmt(FMT_STRING(COMMAND_STATUS_REPEAT ": {}\n"
COMMAND_STATUS_RANDOM ": {}\n"
COMMAND_STATUS_SINGLE ": {}\n"
COMMAND_STATUS_CONSUME ": {}\n"
"partition: {}\n"
COMMAND_STATUS_PLAYLIST ": {}\n"
COMMAND_STATUS_PLAYLIST_LENGTH ": {}\n"
COMMAND_STATUS_MIXRAMPDB ": {}\n"
COMMAND_STATUS_STATE ": {}\n"),
playlist.GetRepeat(),
playlist.GetRandom(),
SingleToString(playlist.GetSingle()),
playlist.GetConsume(),
partition.name.c_str(),
playlist.GetVersion(),
playlist.GetLength(),
pc.GetMixRampDb(),
state);
if (pc.GetCrossFade() > FloatDuration::zero())
r.Format(COMMAND_STATUS_CROSSFADE ": %lu\n",
lround(pc.GetCrossFade().count()));
r.Fmt(FMT_STRING(COMMAND_STATUS_CROSSFADE ": {}\n"),
lround(pc.GetCrossFade().count()));
if (pc.GetMixRampDelay() > FloatDuration::zero())
r.Format(COMMAND_STATUS_MIXRAMPDELAY ": %f\n",
pc.GetMixRampDelay().count());
r.Fmt(FMT_STRING(COMMAND_STATUS_MIXRAMPDELAY ": {}\n"),
pc.GetMixRampDelay().count());
song = playlist.GetCurrentPosition();
if (song >= 0) {
r.Format(COMMAND_STATUS_SONG ": %i\n"
COMMAND_STATUS_SONGID ": %u\n",
song, playlist.PositionToId(song));
r.Fmt(FMT_STRING(COMMAND_STATUS_SONG ": {}\n"
COMMAND_STATUS_SONGID ": {}\n"),
song, playlist.PositionToId(song));
}
if (player_status.state != PlayerState::STOP) {
r.Format(COMMAND_STATUS_TIME ": %i:%i\n"
"elapsed: %1.3f\n"
COMMAND_STATUS_BITRATE ": %u\n",
player_status.elapsed_time.RoundS(),
player_status.total_time.IsNegative()
? 0U
: unsigned(player_status.total_time.RoundS()),
player_status.elapsed_time.ToDoubleS(),
player_status.bit_rate);
r.Fmt(FMT_STRING(COMMAND_STATUS_TIME ": {}:{}\n"
"elapsed: {:1.3}\n"
COMMAND_STATUS_BITRATE ": {}\n"),
player_status.elapsed_time.RoundS(),
player_status.total_time.IsNegative()
? 0U
: unsigned(player_status.total_time.RoundS()),
player_status.elapsed_time.ToDoubleS(),
player_status.bit_rate);
if (!player_status.total_time.IsNegative())
r.Format("duration: %1.3f\n",
r.Fmt(FMT_STRING("duration: {:1.3}\n"),
player_status.total_time.ToDoubleS());
if (player_status.audio_format.IsDefined())
r.Format(COMMAND_STATUS_AUDIO ": %s\n",
ToString(player_status.audio_format).c_str());
r.Fmt(FMT_STRING(COMMAND_STATUS_AUDIO ": {}\n"),
ToString(player_status.audio_format));
}
#ifdef ENABLE_DATABASE
@@ -193,23 +195,23 @@ handle_status(Client &client, [[maybe_unused]] Request args, Response &r)
? update_service->GetId()
: 0;
if (updateJobId != 0) {
r.Format(COMMAND_STATUS_UPDATING_DB ": %i\n",
updateJobId);
r.Fmt(FMT_STRING(COMMAND_STATUS_UPDATING_DB ": {}\n"),
updateJobId);
}
#endif
try {
pc.LockCheckRethrowError();
} catch (...) {
r.Format(COMMAND_STATUS_ERROR ": %s\n",
GetFullMessage(std::current_exception()).c_str());
r.Fmt(FMT_STRING(COMMAND_STATUS_ERROR ": {}\n"),
GetFullMessage(std::current_exception()));
}
song = playlist.GetNextPosition();
if (song >= 0)
r.Format(COMMAND_STATUS_NEXTSONG ": %i\n"
COMMAND_STATUS_NEXTSONGID ": %u\n",
song, playlist.PositionToId(song));
r.Fmt(FMT_STRING(COMMAND_STATUS_NEXTSONG ": {}\n"
COMMAND_STATUS_NEXTSONGID ": {}\n"),
song, playlist.PositionToId(song));
return CommandResult::OK;
}
@@ -351,7 +353,7 @@ CommandResult
handle_replay_gain_status(Client &client, [[maybe_unused]] Request args,
Response &r)
{
r.Format("replay_gain_mode: %s\n",
ToString(client.GetPartition().replay_gain_mode));
r.Fmt(FMT_STRING("replay_gain_mode: {}\n"),
ToString(client.GetPartition().replay_gain_mode));
return CommandResult::OK;
}

View File

@@ -42,6 +42,8 @@
#include "util/UriExtract.hxx"
#include "LocateUri.hxx"
#include <fmt/format.h>
bool
playlist_commands_available() noexcept
{
@@ -52,7 +54,7 @@ static void
print_spl_list(Response &r, const PlaylistVector &list)
{
for (const auto &i : list) {
r.Format("playlist: %s\n", i.name.c_str());
r.Fmt(FMT_STRING("playlist: {}\n"), i.name);
if (!IsNegative(i.mtime))
time_print(r, "Last-Modified", i.mtime);

View File

@@ -39,6 +39,8 @@
#include "util/StringAPI.hxx"
#include "util/NumberParser.hxx"
#include <fmt/format.h>
#include <limits>
static void
@@ -127,7 +129,7 @@ handle_addid(Client &client, Request args, Response &r)
}
}
r.Format("Id: %u\n", added_id);
r.Fmt(FMT_STRING("Id: {}\n"), added_id);
return CommandResult::OK;
}

View File

@@ -17,8 +17,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define __STDC_FORMAT_MACROS /* for PRIu64 */
#include "config.h"
#include "StorageCommands.hxx"
#include "Request.hxx"
@@ -37,7 +35,8 @@
#include "TimePrint.hxx"
#include "IdleFlags.hxx"
#include <cinttypes> /* for PRIu64 */
#include <fmt/format.h>
#include <memory>
gcc_pure
@@ -47,13 +46,6 @@ skip_path(const char *name_utf8) noexcept
return std::strchr(name_utf8, '\n') != nullptr;
}
#if defined(_WIN32) && GCC_CHECK_VERSION(4,6)
/* PRIu64 causes bogus compiler warning */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif
static void
handle_listfiles_storage(Response &r, StorageDirectoryReader &reader)
{
@@ -75,14 +67,14 @@ handle_listfiles_storage(Response &r, StorageDirectoryReader &reader)
continue;
case StorageFileInfo::Type::REGULAR:
r.Format("file: %s\n"
"size: %" PRIu64 "\n",
name_utf8,
info.size);
r.Fmt(FMT_STRING("file: {}\n"
"size: {}\n"),
name_utf8,
info.size);
break;
case StorageFileInfo::Type::DIRECTORY:
r.Format("directory: %s\n", name_utf8);
r.Fmt(FMT_STRING("directory: {}\n"), name_utf8);
break;
}
@@ -139,7 +131,7 @@ print_storage_uri(Client &client, Response &r, const Storage &storage)
uri = std::move(allocated);
}
r.Format("storage: %s\n", uri.c_str());
r.Fmt(FMT_STRING("storage: {}\n"), uri);
}
CommandResult
@@ -155,7 +147,7 @@ handle_listmounts(Client &client, [[maybe_unused]] Request args, Response &r)
const auto visitor = [&client, &r](const char *mount_uri,
const Storage &storage){
r.Format("mount: %s\n", mount_uri);
r.Fmt(FMT_STRING("mount: {}\n"), mount_uri);
print_storage_uri(client, r, storage);
};