Skip to content

Commit

Permalink
lib/fmt/RuntimeError: new library
Browse files Browse the repository at this point in the history
Replacing FormatRuntimeError().
  • Loading branch information
MaxKellermann committed Nov 29, 2022
1 parent 45b13fc commit fa58db7
Show file tree
Hide file tree
Showing 105 changed files with 551 additions and 502 deletions.
11 changes: 6 additions & 5 deletions src/Listen.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "config/Net.hxx"
#include "lib/fmt/ExceptionFormatter.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "net/AllocatedSocketAddress.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "net/SocketUtil.hxx"
Expand All @@ -35,7 +36,6 @@
#include "fs/StandardDirectory.hxx"
#include "fs/XDG.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"

#include <sys/stat.h>

Expand Down Expand Up @@ -129,9 +129,9 @@ listen_global_init(const ConfigData &config, ClientListener &listener)
ServerSocketAddGeneric(listener, param.value.c_str(),
port);
} catch (...) {
std::throw_with_nested(FormatRuntimeError("Failed to listen on %s (line %i)",
param.value.c_str(),
param.line));
std::throw_with_nested(FmtRuntimeError("Failed to listen on {} (line {})",
param.value,
param.line));
}
}

Expand All @@ -146,7 +146,8 @@ listen_global_init(const ConfigData &config, ClientListener &listener)
try {
listener.AddPort(port);
} catch (...) {
std::throw_with_nested(FormatRuntimeError("Failed to listen on *:%d: ", port));
std::throw_with_nested(FmtRuntimeError("Failed to listen on *:{}",
port));
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/LogInit.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
#include "LogBackend.hxx"
#include "Log.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "config/Param.hxx"
#include "config/Data.hxx"
#include "config/Option.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringAPI.hxx"
#include "lib/fmt/SystemError.hxx"

Expand Down Expand Up @@ -79,9 +79,8 @@ log_init_file(int line)
out_fd = open_log_file();
if (out_fd < 0) {
#ifdef _WIN32
const std::string out_path_utf8 = out_path.ToUTF8();
throw FormatRuntimeError("failed to open log file \"%s\" (config line %d)",
out_path_utf8.c_str(), line);
throw FmtRuntimeError("failed to open log file \"{}\" (config line {})",
out_path, line);
#else
throw FmtErrno("failed to open log file \"{}\" (config line {})",
out_path, line);
Expand Down Expand Up @@ -109,7 +108,7 @@ parse_log_level(const char *value)
else if (StringIsEqual(value, "error"))
return LogLevel::ERROR;
else
throw FormatRuntimeError("unknown log level \"%s\"", value);
throw FmtRuntimeError("unknown log level \"{}\"", value);
}

#endif
Expand Down
1 change: 0 additions & 1 deletion src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#include "config/Domain.hxx"
#include "config/Parser.hxx"
#include "config/PartitionConfig.hxx"
#include "util/RuntimeError.hxx"
#include "util/ScopeExit.hxx"

#ifdef ENABLE_DAEMON
Expand Down
9 changes: 4 additions & 5 deletions src/Permission.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#include "config/Param.hxx"
#include "config/Data.hxx"
#include "config/Option.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "net/AddressInfo.hxx"
#include "net/Resolver.hxx"
#include "net/ToString.hxx"
#include "util/IterableSplitString.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringSplit.hxx"

#include <cassert>
Expand Down Expand Up @@ -68,8 +68,7 @@ ParsePermission(std::string_view s)
if (s == i->name)
return i->value;

throw FormatRuntimeError("unknown permission \"%.*s\"",
int(s.size()), s.data());
throw FmtRuntimeError("unknown permission \"{}\"", s);
}

static unsigned
Expand Down Expand Up @@ -103,8 +102,8 @@ initPermissions(const ConfigData &config)
const auto [password, permissions] =
Split(value, PERMISSION_PASSWORD_CHAR);
if (permissions.data() == nullptr)
throw FormatRuntimeError("\"%c\" not found in password string",
PERMISSION_PASSWORD_CHAR);
throw FmtRuntimeError("\"{}\" not found in password string",
PERMISSION_PASSWORD_CHAR);

permission_passwords.emplace(password,
parsePermissions(permissions));
Expand Down
8 changes: 3 additions & 5 deletions src/PlaylistDatabase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

#include "PlaylistDatabase.hxx"
#include "db/PlaylistVector.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "io/LineReader.hxx"
#include "io/BufferedOutputStream.hxx"
#include "time/ChronoUtil.hxx"
#include "util/StringStrip.hxx"
#include "util/RuntimeError.hxx"

#include <fmt/format.h>

Expand Down Expand Up @@ -55,17 +55,15 @@ playlist_metadata_load(LineReader &file, PlaylistVector &pv, const char *name)
std::strcmp(line, "playlist_end") != 0) {
colon = std::strchr(line, ':');
if (colon == nullptr || colon == line)
throw FormatRuntimeError("unknown line in db: %s",
line);
throw FmtRuntimeError("unknown line in db: {}", line);

*colon++ = 0;
value = StripLeft(colon);

if (std::strcmp(line, "mtime") == 0)
pm.mtime = std::chrono::system_clock::from_time_t(strtol(value, nullptr, 10));
else
throw FormatRuntimeError("unknown line in db: %s",
line);
throw FmtRuntimeError("unknown line in db: {}", line);
}

pv.UpdateOrInsert(std::move(pm));
Expand Down
9 changes: 4 additions & 5 deletions src/SongSave.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "song/DetachedSong.hxx"
#include "TagSave.hxx"
#include "lib/fmt/AudioFormatFormatter.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "io/LineReader.hxx"
#include "io/BufferedOutputStream.hxx"
#include "tag/ParseName.hxx"
Expand All @@ -32,7 +33,6 @@
#include "util/StringAPI.hxx"
#include "util/StringBuffer.hxx"
#include "util/StringStrip.hxx"
#include "util/RuntimeError.hxx"
#include "util/NumberParser.hxx"

#include <stdlib.h>
Expand Down Expand Up @@ -97,9 +97,8 @@ song_load(LineReader &file, const char *uri,
while ((line = file.ReadLine()) != nullptr &&
!StringIsEqual(line, SONG_END)) {
char *colon = std::strchr(line, ':');
if (colon == nullptr || colon == line) {
throw FormatRuntimeError("unknown line in db: %s", line);
}
if (colon == nullptr || colon == line)
throw FmtRuntimeError("unknown line in db: {}", line);

*colon++ = 0;
const char *value = StripLeft(colon);
Expand Down Expand Up @@ -134,7 +133,7 @@ song_load(LineReader &file, const char *uri,
song.SetStartTime(SongTime::FromMS(start_ms));
song.SetEndTime(SongTime::FromMS(end_ms));
} else {
throw FormatRuntimeError("unknown line in db: %s", line);
throw FmtRuntimeError("unknown line in db: {}", line);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/archive/plugins/Iso9660ArchivePlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#include "../ArchiveVisitor.hxx"
#include "input/InputStream.hxx"
#include "fs/Path.hxx"
#include "util/RuntimeError.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "util/StringCompare.hxx"
#include "util/UTF8.hxx"

Expand All @@ -46,8 +47,8 @@ struct Iso9660 {
explicit Iso9660(Path path)
:iso(iso9660_open(path.c_str())) {
if (iso == nullptr)
throw FormatRuntimeError("Failed to open ISO9660 file %s",
path.c_str());
throw FmtRuntimeError("Failed to open ISO9660 file {}",
path);
}

~Iso9660() noexcept {
Expand Down Expand Up @@ -238,8 +239,8 @@ Iso9660ArchiveFile::OpenStream(const char *pathname,
{
auto statbuf = iso9660_ifs_stat_translate(iso->iso, pathname);
if (statbuf == nullptr)
throw FormatRuntimeError("not found in the ISO file: %s",
pathname);
throw FmtRuntimeError("not found in the ISO file: {}",
pathname);

const lsn_t lsn = statbuf->lsn;
const offset_type size = statbuf->size;
Expand Down
20 changes: 9 additions & 11 deletions src/archive/plugins/ZzipArchivePlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,24 @@
#include "../ArchiveFile.hxx"
#include "../ArchiveVisitor.hxx"
#include "input/InputStream.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "fs/Path.hxx"
#include "lib/fmt/SystemError.hxx"
#include "util/RuntimeError.hxx"
#include "util/UTF8.hxx"

#include <zzip/zzip.h>

#include <utility>

#include <cinttypes> /* for PRIoffset (PRIu64) */

struct ZzipDir {
ZZIP_DIR *const dir;

explicit ZzipDir(Path path)
:dir(zzip_dir_open(path.c_str(), nullptr)) {
if (dir == nullptr)
throw FormatRuntimeError("Failed to open ZIP file %s",
path.c_str());
throw FmtRuntimeError("Failed to open ZIP file {}",
path);
}

~ZzipDir() noexcept {
Expand Down Expand Up @@ -140,9 +139,9 @@ ZzipArchiveFile::OpenStream(const char *pathname,
pathname);

default:
throw FormatRuntimeError("Failed to open '%s' in ZIP file: %s",
pathname,
zzip_strerror(error));
throw FmtRuntimeError("Failed to open '{}' in ZIP file: {}",
pathname,
zzip_strerror(error));
}
}

Expand All @@ -161,9 +160,8 @@ ZzipInputStream::Read(std::unique_lock<Mutex> &, void *ptr, size_t read_size)
throw std::runtime_error("zzip_file_read() has failed");

if (nbytes == 0 && !IsEOF())
throw FormatRuntimeError("Unexpected end of file %s"
" at %" PRIoffset " of %" PRIoffset,
GetURI(), GetOffset(), GetSize());
throw FmtRuntimeError("Unexpected end of file {} at {} of {}",
GetURI(), GetOffset(), GetSize());

offset = zzip_tell(file);
return nbytes;
Expand Down
6 changes: 3 additions & 3 deletions src/cmdline/OptionParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "OptionParser.hxx"
#include "OptionDef.hxx"
#include "util/RuntimeError.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "util/StringCompare.hxx"

static const char *
Expand All @@ -37,7 +37,7 @@ OptionParser::CheckShiftValue(const char *s, const OptionDef &option)
return nullptr;

if (args.empty())
throw FormatRuntimeError("Value expected after %s", s);
throw FmtRuntimeError("Value expected after {}", s);

return Shift(args);
}
Expand Down Expand Up @@ -78,7 +78,7 @@ OptionParser::IdentifyOption(const char *s)
}
}

throw FormatRuntimeError("Unknown option: %s", s);
throw FmtRuntimeError("Unknown option: {}", s);
}

OptionParser::Result
Expand Down
3 changes: 3 additions & 0 deletions src/cmdline/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ cmdline = static_library(
'cmdline',
'OptionParser.cxx',
include_directories: inc,
dependencies: [
fmt_dep,
],
)

cmdline_dep = declare_dependency(
Expand Down
12 changes: 6 additions & 6 deletions src/config/Block.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
#include "Parser.hxx"
#include "Path.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "lib/fmt/RuntimeError.hxx"

#include <stdlib.h>

void
BlockParam::ThrowWithNested() const
{
std::throw_with_nested(FormatRuntimeError("Error in setting \"%s\" on line %i",
name.c_str(), line));
std::throw_with_nested(FmtRuntimeError("Error in setting \"{}\" on line {}",
name, line));
}

int
Expand All @@ -39,7 +39,7 @@ BlockParam::GetIntValue() const
char *endptr;
long value2 = strtol(s, &endptr, 0);
if (endptr == s || *endptr != 0)
throw FormatRuntimeError("Not a valid number in line %i", line);
throw FmtRuntimeError("Not a valid number in line {}", line);

return value2;
}
Expand Down Expand Up @@ -147,6 +147,6 @@ ConfigBlock::GetBlockValue(const char *name, bool default_value) const
void
ConfigBlock::ThrowWithNested() const
{
std::throw_with_nested(FormatRuntimeError("Error in block on line %i",
line));
std::throw_with_nested(FmtRuntimeError("Error in block on line {}",
line));
}
6 changes: 3 additions & 3 deletions src/config/Data.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "Data.hxx"
#include "Parser.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "util/StringAPI.hxx"

#include <stdlib.h>
Expand Down Expand Up @@ -157,8 +157,8 @@ ConfigData::FindBlock(ConfigBlockOption option,
for (const auto &block : GetBlockList(option)) {
const char *value2 = block.GetBlockValue(key);
if (value2 == nullptr)
throw FormatRuntimeError("block without '%s' in line %d",
key, block.line);
throw FmtRuntimeError("block without '{}' in line {}",
key, block.line);

if (StringIsEqual(value2, value))
return &block;
Expand Down
Loading

0 comments on commit fa58db7

Please sign in to comment.