Skip to content

Commit

Permalink
fix: wrap runtime format strings with fmt::runtime in library, daemon…
Browse files Browse the repository at this point in the history
… and cli

fmt::format_string ctor is consteval with C++20
See fmtlib/fmt#2438

Signed-off-by: Dzmitry Neviadomski <[email protected]>
  • Loading branch information
nevack committed Jun 29, 2024
1 parent 2cc639c commit 55da2fd
Show file tree
Hide file tree
Showing 36 changed files with 152 additions and 129 deletions.
29 changes: 15 additions & 14 deletions daemon/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ auto onFileAdded(tr_session* session, std::string_view dirname, std::string_view
if (!tr_file_read(filename, content, &error))
{
tr_logAddWarn(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't read '{path}': {error} ({error_code})")),
fmt::arg("path", basename),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())));
Expand All @@ -218,7 +218,7 @@ auto onFileAdded(tr_session* session, std::string_view dirname, std::string_view

if (tr_torrentNew(ctor, nullptr) == nullptr)
{
tr_logAddError(fmt::format(_("Couldn't add torrent file '{path}'"), fmt::arg("path", basename)));
tr_logAddError(fmt::format(fmt::runtime(_("Couldn't add torrent file '{path}'")), fmt::arg("path", basename)));
}
else
{
Expand All @@ -227,12 +227,12 @@ auto onFileAdded(tr_session* session, std::string_view dirname, std::string_view

if (test && trash)
{
tr_logAddInfo(fmt::format(_("Removing torrent file '{path}'"), fmt::arg("path", basename)));
tr_logAddInfo(fmt::format(fmt::runtime(_("Removing torrent file '{path}'")), fmt::arg("path", basename)));

if (auto error = tr_error{}; !tr_sys_path_remove(filename, &error))
{
tr_logAddError(fmt::format(
_("Couldn't remove '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't remove '{path}': {error} ({error_code})")),
fmt::arg("path", basename),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())));
Expand Down Expand Up @@ -620,7 +620,8 @@ bool tr_daemon::parse_args(int argc, char const* const* argv, bool* dump_setting
}
else
{
std::cerr << fmt::format(_("Couldn't parse log level '{level}'"), fmt::arg("level", optstr)) << std::endl;
std::cerr << fmt::format(fmt::runtime(_("Couldn't parse log level '{level}'")), fmt::arg("level", optstr))
<< std::endl;
}
break;

Expand Down Expand Up @@ -681,7 +682,7 @@ void tr_daemon::reconfigure()
}

configDir = tr_sessionGetConfigDir(my_session_);
tr_logAddInfo(fmt::format(_("Reloading settings from '{path}'"), fmt::arg("path", configDir)));
tr_logAddInfo(fmt::format(fmt::runtime(_("Reloading settings from '{path}'")), fmt::arg("path", configDir)));

tr_sessionSet(my_session_, load_settings(configDir));
tr_sessionReloadBlocklists(my_session_);
Expand All @@ -705,7 +706,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
{
auto const error_code = errno;
auto const errmsg = fmt::format(
_("Couldn't initialize daemon: {error} ({error_code})"),
fmt::runtime(_("Couldn't initialize daemon: {error} ({error_code})")),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code));
printMessage(log_stream_, TR_LOG_ERROR, MyName, errmsg, __FILE__, __LINE__);
Expand All @@ -717,7 +718,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
auto const* const cdir = this->config_dir_.c_str();
auto* session = tr_sessionInit(cdir, true, settings_);
tr_sessionSetRPCCallback(session, on_rpc_callback, this);
tr_logAddInfo(fmt::format(_("Loading settings from '{path}'"), fmt::arg("path", cdir)));
tr_logAddInfo(fmt::format(fmt::runtime(_("Loading settings from '{path}'")), fmt::arg("path", cdir)));
tr_sessionSaveSettings(session, cdir, settings_);

auto sv = std::string_view{};
Expand All @@ -738,13 +739,13 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
auto const out = std::to_string(getpid());
tr_sys_file_write(fp, std::data(out), std::size(out), nullptr);
tr_sys_file_close(fp);
tr_logAddInfo(fmt::format(_("Saved pidfile '{path}'"), fmt::arg("path", sz_pid_filename)));
tr_logAddInfo(fmt::format(fmt::runtime(_("Saved pidfile '{path}'")), fmt::arg("path", sz_pid_filename)));
pidfile_created = true;
}
else
{
tr_logAddError(fmt::format(
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't save '{path}': {error} ({error_code})")),
fmt::arg("path", sz_pid_filename),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())));
Expand Down Expand Up @@ -775,7 +776,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
(void)tr_variantDictFindStrView(&settings_, TR_KEY_watch_dir, &dir);
if (!std::empty(dir))
{
tr_logAddInfo(fmt::format(_("Watching '{path}' for new torrent files"), fmt::arg("path", dir)));
tr_logAddInfo(fmt::format(fmt::runtime(_("Watching '{path}' for new torrent files")), fmt::arg("path", dir)));

auto handler = [session](std::string_view dirname, std::string_view basename)
{
Expand Down Expand Up @@ -820,7 +821,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't create event: {error} ({error_code})"),
fmt::runtime(_("Couldn't create event: {error} ({error_code})")),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
goto CLEANUP;
Expand All @@ -830,7 +831,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't add event: {error} ({error_code})"),
fmt::runtime(_("Couldn't add event: {error} ({error_code})")),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
goto CLEANUP;
Expand All @@ -844,7 +845,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground)
{
auto const error_code = errno;
tr_logAddError(fmt::format(
_("Couldn't launch daemon event loop: {error} ({error_code})"),
fmt::runtime(_("Couldn't launch daemon event loop: {error} ({error_code})")),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
goto CLEANUP;
Expand Down
4 changes: 2 additions & 2 deletions libtransmission/announcer-http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::
{
tr_logAddWarn(
fmt::format(
_("Couldn't parse announce response: {error} ({error_code})"),
fmt::runtime(_("Couldn't parse announce response: {error} ({error_code})")),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())),
log_name);
Expand Down Expand Up @@ -644,7 +644,7 @@ void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::stri
{
tr_logAddWarn(
fmt::format(
_("Couldn't parse scrape response: {error} ({error_code})"),
fmt::runtime(_("Couldn't parse scrape response: {error} ({error_code})")),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())),
log_name);
Expand Down
9 changes: 5 additions & 4 deletions libtransmission/announcer-udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,10 @@ struct tau_tracker
}
else if (action == TAU_ACTION_ERROR)
{
std::string errmsg = !std::empty(buf) ?
buf.to_string() :
fmt::format(_("{ip_protocol} connection failed"), fmt::arg("ip_protocol", tr_ip_protocol_to_sv(ip_protocol)));
std::string errmsg = !std::empty(buf) ? buf.to_string() :
fmt::format(
fmt::runtime(_("{ip_protocol} connection failed")),
fmt::arg("ip_protocol", tr_ip_protocol_to_sv(ip_protocol)));
fail_all(true, false, errmsg);
logdbg(log_name(), std::move(errmsg));
}
Expand Down Expand Up @@ -507,7 +508,7 @@ struct tau_tracker
logwarn(
log_name(),
fmt::format(
_("Couldn't look up '{address}:{port}' in {ip_protocol}: {error} ({error_code})"),
fmt::runtime(_("Couldn't look up '{address}:{port}' in {ip_protocol}: {error} ({error_code})")),
fmt::arg("address", host),
fmt::arg("port", port.host()),
fmt::arg("ip_protocol", tr_ip_protocol_to_sv(ip_protocol)),
Expand Down
13 changes: 8 additions & 5 deletions libtransmission/announcer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class tr_announcer_impl final : public tr_announcer
}
else
{
tr_logAddError(fmt::format(_("Unsupported URL: '{url}'"), fmt::arg("url", scrape_sv)));
tr_logAddError(fmt::format(fmt::runtime(_("Unsupported URL: '{url}'")), fmt::arg("url", scrape_sv)));
}
}

Expand All @@ -198,7 +198,7 @@ class tr_announcer_impl final : public tr_announcer
}
else
{
tr_logAddWarn(fmt::format(_("Unsupported URL: '{url}'"), fmt::arg("url", announce_sv)));
tr_logAddWarn(fmt::format(fmt::runtime(_("Unsupported URL: '{url}'")), fmt::arg("url", announce_sv)));
}
}

Expand Down Expand Up @@ -881,7 +881,10 @@ void on_announce_error(tr_tier* tier, char const* err, tr_announce_event e)
{
tr_logAddErrorTier(
tier,
fmt::format(_("Announce error: {error} ({url})"), fmt::arg("error", err), fmt::arg("url", announce_url)));
fmt::format(
fmt::runtime(_("Announce error: {error} ({url})")),
fmt::arg("error", err),
fmt::arg("url", announce_url)));
}
else
{
Expand All @@ -890,10 +893,10 @@ void on_announce_error(tr_tier* tier, char const* err, tr_announce_event e)
tr_logAddWarnTier(
tier,
fmt::format(
tr_ngettext(
fmt::runtime(tr_ngettext(
"Announce error: {error} (Retrying in {count} second) ({url})",
"Announce error: {error} (Retrying in {count} seconds) ({url})",
interval),
interval)),
fmt::arg("error", err),
fmt::arg("count", interval),
fmt::arg("url", announce_url)));
Expand Down
20 changes: 11 additions & 9 deletions libtransmission/blocklist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void save(std::string_view filename, address_range_t const* ranges, size_t n_ran
if (!out.is_open())
{
tr_logAddWarn(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't read '{path}': {error} ({error_code})")),
fmt::arg("path", filename),
fmt::arg("error", tr_strerror(errno)),
fmt::arg("error_code", errno)));
Expand All @@ -70,15 +70,16 @@ void save(std::string_view filename, address_range_t const* ranges, size_t n_ran
!out.write(reinterpret_cast<char const*>(ranges), n_ranges * sizeof(*ranges)))
{
tr_logAddWarn(fmt::format(
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't save '{path}': {error} ({error_code})")),
fmt::arg("path", filename),
fmt::arg("error", tr_strerror(errno)),
fmt::arg("error_code", errno)));
}
else
{
tr_logAddInfo(fmt::format(
tr_ngettext("Blocklist '{path}' has {count} entry", "Blocklist '{path}' has {count} entries", n_ranges),
fmt::runtime(
tr_ngettext("Blocklist '{path}' has {count} entry", "Blocklist '{path}' has {count} entries", n_ranges)),
fmt::arg("path", tr_sys_path_basename(filename)),
fmt::arg("count", n_ranges)));
}
Expand Down Expand Up @@ -237,7 +238,7 @@ auto parseFile(std::string_view filename)
if (!in.is_open())
{
tr_logAddWarn(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't read '{path}': {error} ({error_code})")),
fmt::arg("path", filename),
fmt::arg("error", tr_strerror(errno)),
fmt::arg("error_code", errno)));
Expand All @@ -256,7 +257,7 @@ auto parseFile(std::string_view filename)
else
{
// don't try to display the actual lines - it causes issues
tr_logAddWarn(fmt::format(_("Couldn't parse line: '{line}'"), fmt::arg("line", line_number)));
tr_logAddWarn(fmt::format(fmt::runtime(_("Couldn't parse line: '{line}'")), fmt::arg("line", line_number)));
}
}
in.close();
Expand Down Expand Up @@ -335,7 +336,7 @@ void Blocklists::Blocklist::ensureLoaded() const
if (error)
{
tr_logAddWarn(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't read '{path}': {error} ({error_code})")),
fmt::arg("path", bin_file_),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())));
Expand All @@ -350,7 +351,7 @@ void Blocklists::Blocklist::ensureLoaded() const
if (!in)
{
tr_logAddWarn(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't read '{path}': {error} ({error_code})")),
fmt::arg("path", bin_file_),
fmt::arg("error", tr_strerror(errno)),
fmt::arg("error_code", errno)));
Expand Down Expand Up @@ -400,7 +401,8 @@ void Blocklists::Blocklist::ensureLoaded() const
}

tr_logAddInfo(fmt::format(
tr_ngettext("Blocklist '{path}' has {count} entry", "Blocklist '{path}' has {count} entries", std::size(rules_)),
fmt::runtime(
tr_ngettext("Blocklist '{path}' has {count} entry", "Blocklist '{path}' has {count} entries", std::size(rules_))),
fmt::arg("path", tr_sys_path_basename(bin_file_)),
fmt::arg("count", std::size(rules_))));
}
Expand Down Expand Up @@ -470,7 +472,7 @@ std::optional<Blocklists::Blocklist> Blocklists::Blocklist::saveNew(
if (error)
{
tr_logAddWarn(fmt::format(
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't save '{path}': {error} ({error_code})")),
fmt::arg("path", src_file),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())));
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/crypto-utils-ccrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void log_ccrypto_error(CCCryptorStatus error_code, char const* file, long line)
line,
TR_LOG_ERROR,
fmt::format(
_("{crypto_library} error: {error} ({error_code})"),
fmt::runtime(_("{crypto_library} error: {error} ({error_code})")),
fmt::arg("crypto_library", "CCrypto"),
fmt::arg("error", ccrypto_error_to_str(error_code)),
fmt::arg("error_code", error_code)));
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/crypto-utils-mbedtls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void log_mbedtls_error(int error_code, char const* file, int line)
line,
TR_LOG_ERROR,
fmt::format(
_("{crypto_library} error: {error} ({error_code})"),
fmt::runtime(_("{crypto_library} error: {error} ({error_code})")),
fmt::arg("crypto_library", "MbedTLS"),
fmt::arg("error", error_message),
fmt::arg("error_code", error_code)));
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/crypto-utils-openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void log_openssl_error(char const* file, int line)
line,
TR_LOG_ERROR,
fmt::format(
_("{crypto_library} error: {error} ({error_code})"),
fmt::runtime(_("{crypto_library} error: {error} ({error_code})")),
fmt::arg("crypto_library", "OpenSSL"),
fmt::arg("error", std::data(buf)),
fmt::arg("error_code", error_code)));
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/crypto-utils-wolfssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void log_wolfssl_error(int error_code, char const* file, int line)
line,
TR_LOG_ERROR,
fmt::format(
_("{crypto_library} error: {error} ({error_code})"),
fmt::runtime(_("{crypto_library} error: {error} ({error_code})")),
fmt::arg("crypto_library", "WolfSSL"),
fmt::arg("error", wc_GetErrorString(error_code)),
fmt::arg("error_code", error_code)));
Expand Down
7 changes: 4 additions & 3 deletions libtransmission/global-ip-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void tr_global_ip_cache::update_source_addr(tr_address_type type) noexcept
{
set_source_addr(*source_addr);
tr_logAddDebug(fmt::format(
_("Successfully updated source {protocol} address to {ip}"),
fmt::runtime(_("Successfully updated source {protocol} address to {ip}")),
fmt::arg("protocol", protocol),
fmt::arg("ip", source_addr->display_name())));
}
Expand All @@ -288,7 +288,8 @@ void tr_global_ip_cache::update_source_addr(tr_address_type type) noexcept
{
stop_timer(type); // No point in retrying
has_ip_protocol_[type] = false;
tr_logAddInfo(fmt::format(_("Your machine does not support {protocol}"), fmt::arg("protocol", protocol)));
tr_logAddInfo(
fmt::format(fmt::runtime(_("Your machine does not support {protocol}")), fmt::arg("protocol", protocol)));
}
}

Expand All @@ -312,7 +313,7 @@ void tr_global_ip_cache::on_response_ip_query(tr_address_type type, tr_web::Fetc
upkeep_timers_[type]->set_interval(UpkeepInterval);

tr_logAddDebug(fmt::format(
_("Successfully updated global {type} address to {ip} using {url}"),
fmt::runtime(_("Successfully updated global {type} address to {ip} using {url}")),
fmt::arg("type", protocol),
fmt::arg("ip", addr->display_name()),
fmt::arg("url", IPQueryServices[type][ix_service_[type]])));
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/inout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool write_entire_buf(tr_sys_file_t const fd, uint64_t file_offset, uint8_t cons
error.set(
err,
fmt::format(
_("Couldn't get '{path}': {error} ({error_code})"),
fmt::runtime(_("Couldn't get '{path}': {error} ({error_code})")),
fmt::arg("path", tor.file_subpath(file_index)),
fmt::arg("error", tr_strerror(err)),
fmt::arg("error_code", err)));
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/makemeta.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void walkTree(std::string_view const top, std::string_view const subpath, std::s
if (error)
{
tr_logAddWarn(fmt::format(
_("Skipping '{path}': {error} ({error_code})"),
fmt::runtime(_("Skipping '{path}': {error} ({error_code})")),
fmt::arg("path", path),
fmt::arg("error", error.message()),
fmt::arg("error_code", error.code())));
Expand Down
Loading

0 comments on commit 55da2fd

Please sign in to comment.