diff --git a/daemon/daemon-posix.cc b/daemon/daemon-posix.cc index efef35d28..5d3fa99e3 100644 --- a/daemon/daemon-posix.cc +++ b/daemon/daemon-posix.cc @@ -20,6 +20,8 @@ #include "daemon.h" +using namespace std::literals; + /*** **** ***/ @@ -33,9 +35,9 @@ static int signal_pipe[2]; **** ***/ -static void set_system_error(tr_error** error, int code, char const* message) +static void set_system_error(tr_error** error, int code, std::string_view message) { - tr_error_set(error, code, "%s (%d): %s", message, code, tr_strerror(code)); + tr_error_set(error, code, tr_strvJoin(message, " ("sv, std::to_string(code), "): "sv, tr_strerror(code))); } /*** diff --git a/daemon/daemon-win32.cc b/daemon/daemon-win32.cc index 037f02f06..dd11015e8 100644 --- a/daemon/daemon-win32.cc +++ b/daemon/daemon-win32.cc @@ -45,8 +45,10 @@ static HANDLE service_stop_thread = nullptr; static void set_system_error(tr_error** error, DWORD code, char const* message) { - char* const system_message = tr_win32_format_message(code); - tr_error_set(error, code, "%s (0x%08lx): %s", message, code, system_message); + auto* const system_message = tr_win32_format_message(code); + auto* const buf = tr_strdup_printf("%s (0x%08lx): %s", message, code, system_message); + tr_error_set(error, code, buf); + tr_free(buf); tr_free(system_message); } diff --git a/gtk/Utils.cc b/gtk/Utils.cc index 828436dee..adf9eee91 100644 --- a/gtk/Utils.cc +++ b/gtk/Utils.cc @@ -311,7 +311,7 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error) catch (Glib::Error const& e) { g_message("Unable to trash file \"%s\": %s", filename.c_str(), e.what().c_str()); - tr_error_set_literal(error, e.code(), e.what().c_str()); + tr_error_set(error, e.code(), e.what().raw()); } } @@ -325,7 +325,7 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error) { g_message("Unable to delete file \"%s\": %s", filename.c_str(), e.what().c_str()); tr_error_clear(error); - tr_error_set_literal(error, e.code(), e.what().c_str()); + tr_error_set(error, e.code(), e.what().raw()); result = false; } } diff --git a/libtransmission/crypto-utils.cc b/libtransmission/crypto-utils.cc index 74a9fa0fd..c1fbdf1ca 100644 --- a/libtransmission/crypto-utils.cc +++ b/libtransmission/crypto-utils.cc @@ -8,7 +8,6 @@ #include #include -#include #include /* memmove(), memset(), strlen() */ #include /* random_device, mt19937, uniform_int_distribution*/ #include diff --git a/libtransmission/error.cc b/libtransmission/error.cc index 123e61d35..eec8282d7 100644 --- a/libtransmission/error.cc +++ b/libtransmission/error.cc @@ -6,8 +6,6 @@ * */ -#include - #include "transmission.h" #include "error.h" @@ -15,30 +13,6 @@ #include "tr-macros.h" #include "utils.h" -tr_error* tr_error_new_literal(int code, char const* message) -{ - TR_ASSERT(message != nullptr); - - auto* const error = tr_new(tr_error, 1); - error->code = code; - error->message = tr_strdup(message); - - return error; -} - -static tr_error* tr_error_new_valist(int code, char const* message_format, va_list args) TR_GNUC_PRINTF(2, 0); - -static tr_error* tr_error_new_valist(int code, char const* message_format, va_list args) -{ - TR_ASSERT(message_format != nullptr); - - auto* const error = tr_new(tr_error, 1); - error->code = code; - error->message = tr_strdup_vprintf(message_format, args); - - return error; -} - void tr_error_free(tr_error* error) { if (error == nullptr) @@ -47,39 +21,18 @@ void tr_error_free(tr_error* error) } tr_free(error->message); - tr_free(error); + delete error; } -void tr_error_set(tr_error** error, int code, char const* message_format, ...) +void tr_error_set(tr_error** error, int code, std::string_view message) { - TR_ASSERT(message_format != nullptr); - if (error == nullptr) { return; } TR_ASSERT(*error == nullptr); - - va_list args; - - va_start(args, message_format); - *error = tr_error_new_valist(code, message_format, args); - va_end(args); -} - -void tr_error_set_literal(tr_error** error, int code, char const* message) -{ - TR_ASSERT(message != nullptr); - - if (error == nullptr) - { - return; - } - - TR_ASSERT(*error == nullptr); - - *error = tr_error_new_literal(code, message); + *error = new tr_error{ code, tr_strvDup(message) }; } void tr_error_propagate(tr_error** new_error, tr_error** old_error) diff --git a/libtransmission/error.h b/libtransmission/error.h index 93ecbe2e6..65bfa4542 100644 --- a/libtransmission/error.h +++ b/libtransmission/error.h @@ -8,6 +8,8 @@ #pragma once +#include + #include "tr-macros.h" /** @@ -24,16 +26,6 @@ struct tr_error char* message; }; -/** - * @brief Create new error object using literal error message. - * - * @param[in] code Error code (platform-specific). - * @param[in] message Error message. - * - * @return Newly allocated error object on success, `nullptr` otherwise. - */ -tr_error* tr_error_new_literal(int code, char const* message); - /** * @brief Free memory used by error object. * @@ -41,18 +33,6 @@ tr_error* tr_error_new_literal(int code, char const* message); */ void tr_error_free(tr_error* error); -/** - * @brief Create and set new error object using `printf`-style formatting. - * - * If passed pointer to error object is `nullptr`, do nothing. - * - * @param[in,out] error Pointer to error object to be set. - * @param[in] code Error code (platform-specific). - * @param[in] message_format Error message format string. - * @param[in] ... Format arguments. - */ -void tr_error_set(tr_error** error, int code, char const* message_format, ...) TR_GNUC_PRINTF(3, 4); - /** * @brief Create and set new error object using literal error message. * @@ -62,7 +42,7 @@ void tr_error_set(tr_error** error, int code, char const* message_format, ...) T * @param[in] code Error code (platform-specific). * @param[in] message Error message. */ -void tr_error_set_literal(tr_error** error, int code, char const* message); +void tr_error_set(tr_error** error, int code, std::string_view message); /** * @brief Propagate existing error object upwards. diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 7e58f100f..e5e6c7f4b 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -96,6 +96,8 @@ #endif #endif +using namespace std::literals; + static void set_system_error(tr_error** error, int code) { if (error == nullptr) @@ -103,7 +105,7 @@ static void set_system_error(tr_error** error, int code) return; } - tr_error_set_literal(error, code, tr_strerror(code)); + tr_error_set(error, code, tr_strerror(code)); } static void set_system_error_if_file_found(tr_error** error, int code) @@ -175,7 +177,7 @@ static bool create_path_require_dir(char const* path, tr_error** error) if ((sb.st_mode & S_IFMT) != S_IFDIR) { - tr_error_set(error, ENOTDIR, _("File \"%s\" is in the way"), path); + tr_error_set(error, ENOTDIR, tr_strvJoin("File is in the way: "sv, path)); return false; } diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index 5379822bb..cfab37770 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -7,8 +7,9 @@ */ #include -#include /* isalpha() */ +#include #include +#include /* isalpha() */ #include #include /* SHCreateDirectoryEx() */ @@ -55,12 +56,14 @@ static void set_system_error(tr_error** error, DWORD code) if (message != nullptr) { - tr_error_set_literal(error, code, message); + tr_error_set(error, code, message); tr_free(message); } else { - tr_error_set(error, code, "Unknown error: 0x%08lx", code); + auto buf = std::array{}; + tr_snprintf(std::data(buf), std::size(buf), "Unknown error: 0x%08lx", code); + tr_error_set(error, code, std::data(buf)); } } diff --git a/libtransmission/file.cc b/libtransmission/file.cc index 16406141b..319643378 100644 --- a/libtransmission/file.cc +++ b/libtransmission/file.cc @@ -7,6 +7,7 @@ */ #include +#include #include /* strlen() */ #include "transmission.h" @@ -15,6 +16,8 @@ #include "tr-assert.h" #include "utils.h" +using namespace std::literals; + bool tr_sys_file_read_line(tr_sys_file_t handle, char* buffer, size_t buffer_size, tr_error** error) { TR_ASSERT(handle != TR_BAD_SYS_FILE); @@ -114,7 +117,7 @@ bool tr_sys_file_write_fmt(tr_sys_file_t handle, char const* format, tr_error** } else { - tr_error_set_literal(error, 0, "Unable to format message."); + tr_error_set(error, 0, "Unable to format message."sv); } return ret; diff --git a/libtransmission/log.cc b/libtransmission/log.cc index 9a958e2a8..1aaabd3e0 100644 --- a/libtransmission/log.cc +++ b/libtransmission/log.cc @@ -7,6 +7,7 @@ */ #include +#include #include #include diff --git a/libtransmission/magnet-metainfo.cc b/libtransmission/magnet-metainfo.cc index acf6f6970..b2b9d9a2c 100644 --- a/libtransmission/magnet-metainfo.cc +++ b/libtransmission/magnet-metainfo.cc @@ -152,7 +152,7 @@ bool tr_magnet_metainfo::parseMagnet(std::string_view magnet_link, tr_error** er auto const parsed = tr_urlParse(magnet_link); if (!parsed || parsed->scheme != "magnet"sv) { - tr_error_set_literal(error, TR_ERROR_EINVAL, "Error parsing URL"); + tr_error_set(error, TR_ERROR_EINVAL, "Error parsing URL"sv); return false; } diff --git a/libtransmission/metainfo.cc b/libtransmission/metainfo.cc index a80a43a29..de45a831f 100644 --- a/libtransmission/metainfo.cc +++ b/libtransmission/metainfo.cc @@ -535,7 +535,7 @@ std::optional tr_metainfoParse(tr_session const* session, tr char const* bad_tag = tr_metainfoParseImpl(session, &out.info, &out.pieces, &out.info_dict_length, meta_in); if (bad_tag != nullptr) { - tr_error_set(error, TR_ERROR_EINVAL, _("Error parsing metainfo: %s"), bad_tag); + tr_error_set(error, TR_ERROR_EINVAL, tr_strvJoin("Error parsing metainfo: "sv, bad_tag)); tr_metainfoFree(&out.info); return {}; } diff --git a/libtransmission/subprocess-posix.cc b/libtransmission/subprocess-posix.cc index 141e6051c..05cf112c5 100644 --- a/libtransmission/subprocess-posix.cc +++ b/libtransmission/subprocess-posix.cc @@ -24,6 +24,8 @@ #include "tr-macros.h" #include "utils.h" +using namespace std::literals; + static void handle_sigchld(int /*i*/) { int rc = 0; @@ -37,21 +39,14 @@ static void handle_sigchld(int /*i*/) /* FIXME: Call old handler, if any */ } -static void set_system_error(tr_error** error, int code, char const* what) +static void set_system_error(tr_error** error, int code, std::string_view what) { if (error == nullptr) { return; } - if (what == nullptr) - { - tr_error_set_literal(error, code, tr_strerror(code)); - } - else - { - tr_error_set(error, code, "%s failed: %s", what, tr_strerror(code)); - } + tr_error_set(error, code, tr_strvJoin(what, " failed "sv, tr_strerror(code))); } static bool tr_spawn_async_in_child( diff --git a/libtransmission/subprocess-win32.cc b/libtransmission/subprocess-win32.cc index 9a465ac15..1ed4d127a 100644 --- a/libtransmission/subprocess-win32.cc +++ b/libtransmission/subprocess-win32.cc @@ -30,7 +30,7 @@ enum tr_app_type TR_APP_TYPE_BATCH }; -static void set_system_error(tr_error** error, DWORD code, char const* what) +static void set_system_error(tr_error** error, DWORD code, std::string_view what) { if (error == nullptr) { @@ -44,14 +44,7 @@ static void set_system_error(tr_error** error, DWORD code, char const* what) message = tr_strdup_printf("Unknown error: 0x%08lx", code); } - if (what == nullptr) - { - tr_error_set_literal(error, code, message); - } - else - { - tr_error_set(error, code, "%s failed: %s", what, message); - } + tr_error_set(error, code, tr_strvJoin(what, " failed: "sv, message)); tr_free(message); } diff --git a/libtransmission/torrent-ctor.cc b/libtransmission/torrent-ctor.cc index 66c490453..1c1b2aa53 100644 --- a/libtransmission/torrent-ctor.cc +++ b/libtransmission/torrent-ctor.cc @@ -71,7 +71,7 @@ bool tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename, tr_error** { if (filename == nullptr) { - tr_error_set_literal(error, EINVAL, "no filename specified"); + tr_error_set(error, EINVAL, "no filename specified"sv); return false; } @@ -116,7 +116,7 @@ bool tr_ctorSaveContents(tr_ctor const* ctor, std::string_view filename, tr_erro if (std::empty(ctor->contents)) { - tr_error_set_literal(error, EINVAL, "torrent ctor has no contents to save"); + tr_error_set(error, EINVAL, "torrent ctor has no contents to save"sv); return false; } diff --git a/libtransmission/torrent-metainfo.cc b/libtransmission/torrent-metainfo.cc index aefdd94fa..f260652d4 100644 --- a/libtransmission/torrent-metainfo.cc +++ b/libtransmission/torrent-metainfo.cc @@ -637,7 +637,7 @@ bool tr_torrent_metainfo::parseBenc(std::string_view benc, tr_error** error) tr_variantFree(&top); if (!std::empty(errmsg)) { - tr_error_set(error, TR_ERROR_EINVAL, "Error parsing metainfo: %" TR_PRIsv, TR_PRIsv_ARG(errmsg)); + tr_error_set(error, TR_ERROR_EINVAL, tr_strvJoin("Error parsing metainfo: ", errmsg)); return false; } diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index 9ae738d91..b69f4e19a 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -283,7 +283,7 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error) if (info.type != TR_SYS_PATH_IS_FILE) { tr_logAddError(err_fmt, path, _("Not a regular file")); - tr_error_set_literal(error, TR_ERROR_EISDIR, _("Not a regular file")); + tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv); return nullptr; } @@ -337,7 +337,7 @@ bool tr_loadFile(std::vector& setme, std::string_view path_sv, tr_error** if (info.type != TR_SYS_PATH_IS_FILE) { tr_logAddError(err_fmt, path_sz, _("Not a regular file")); - tr_error_set_literal(error, TR_ERROR_EISDIR, _("Not a regular file")); + tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv); return false; } @@ -1251,7 +1251,7 @@ bool tr_moveFile(char const* oldpath, char const* newpath, tr_error** error) if (info.type != TR_SYS_PATH_IS_FILE) { - tr_error_set_literal(error, TR_ERROR_EINVAL, "Old path does not point to a file."); + tr_error_set(error, TR_ERROR_EINVAL, "Old path does not point to a file."sv); return false; } diff --git a/libtransmission/variant.cc b/libtransmission/variant.cc index c30cd68da..7478fb6f1 100644 --- a/libtransmission/variant.cc +++ b/libtransmission/variant.cc @@ -1243,7 +1243,7 @@ bool tr_variantFromBuf(tr_variant* setme, int opts, std::string_view buf, char c if (err) { - tr_error_set_literal(error, EILSEQ, "error parsing encoded data"); + tr_error_set(error, EILSEQ, "error parsing encoded data"sv); return false; } diff --git a/macosx/Torrent.mm b/macosx/Torrent.mm index 984970c97..44094007f 100644 --- a/macosx/Torrent.mm +++ b/macosx/Torrent.mm @@ -141,7 +141,7 @@ bool trashDataFile(char const* filename, tr_error** error) NSError* localError; if (![Torrent trashFile:@(filename) error:&localError]) { - tr_error_set_literal(error, localError.code, localError.description.UTF8String); + tr_error_set(error, localError.code, localError.description.UTF8String); return false; } } diff --git a/tests/libtransmission/error-test.cc b/tests/libtransmission/error-test.cc index 5066966ca..3933a7e64 100644 --- a/tests/libtransmission/error-test.cc +++ b/tests/libtransmission/error-test.cc @@ -11,6 +11,8 @@ #include "gtest/gtest.h" +using namespace std::literals; + TEST(Error, errorSet) { tr_error* err = nullptr; @@ -18,14 +20,7 @@ TEST(Error, errorSet) tr_error_prefix(&err, "error: "); EXPECT_EQ(nullptr, err); - tr_error_set(&err, 1, "error: %s (%d)", "oops", 2); - EXPECT_NE(nullptr, err); - EXPECT_EQ(1, err->code); - EXPECT_STREQ("error: oops (2)", err->message); - tr_error_clear(&err); - EXPECT_EQ(nullptr, err); - - tr_error_set_literal(&err, 2, "oops"); + tr_error_set(&err, 2, "oops"sv); EXPECT_NE(nullptr, err); EXPECT_EQ(2, err->code); EXPECT_STREQ("oops", err->message); @@ -44,7 +39,7 @@ TEST(Error, propagate) tr_error* err2 = nullptr; auto constexpr Code = int{ 1 }; - tr_error_set_literal(&err, Code, "oops"); + tr_error_set(&err, Code, "oops"sv); EXPECT_NE(nullptr, err); EXPECT_EQ(Code, err->code); EXPECT_STREQ("oops", err->message);