From: Joel Rosdahl Date: Sun, 16 Jul 2023 15:26:23 +0000 (+0200) Subject: refactor: Move Util::format_argv_for_logging to util X-Git-Tag: v4.9~116 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7019a3d0c39b42b7c1e0335cca801de0c983cce3;p=thirdparty%2Fccache.git refactor: Move Util::format_argv_for_logging to util --- diff --git a/src/Util.cpp b/src/Util.cpp index fb25c0525..28893639e 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -212,23 +212,6 @@ dir_name(std::string_view path) } } -std::string -format_argv_for_logging(const char* const* argv) -{ - std::string result; - for (size_t i = 0; argv[i]; ++i) { - if (i != 0) { - result += ' '; - } - std::string arg(argv[i]); - if (arg.empty() || arg.find(' ') != std::string::npos) { - arg = FMT("\"{}\"", arg); - } - result += arg; - } - return result; -} - void ensure_dir_exists(std::string_view dir) { diff --git a/src/Util.hpp b/src/Util.hpp index 81f1c542c..d663efc15 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -57,10 +57,6 @@ std::string_view dir_name(std::string_view path); // Like create_dir but throws Fatal on error. void ensure_dir_exists(std::string_view dir); -// Format `argv` as a simple string for logging purposes. That is, the result is -// not intended to be machine parsable. `argv` must be terminated by a nullptr. -std::string format_argv_for_logging(const char* const* argv); - // Return the file extension (including the dot) as a view into `path`. If // `path` has no file extension, an empty string_view is returned. std::string_view get_extension(std::string_view path); diff --git a/src/ccache.cpp b/src/ccache.cpp index 0a6708351..2292d3347 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2201,7 +2201,7 @@ initialize(Context& ctx, const char* const* argv, bool masquerading_as_compiler) }); } - LOG("Command line: {}", Util::format_argv_for_logging(argv)); + LOG("Command line: {}", util::format_argv_for_logging(argv)); LOG("Hostname: {}", Util::get_hostname()); LOG("Working directory: {}", ctx.actual_cwd); if (ctx.apparent_cwd != ctx.actual_cwd) { @@ -2385,7 +2385,7 @@ cache_compilation(int argc, const char* const* argv) saved_temp_dir = ctx.config.temporary_dir(); saved_orig_args = std::move(ctx.orig_args); auto execv_argv = saved_orig_args.to_argv(); - LOG("Executing {}", Util::format_argv_for_logging(execv_argv.data())); + LOG("Executing {}", util::format_argv_for_logging(execv_argv.data())); // Execute the original command below after ctx and finalizer have been // destructed. } diff --git a/src/execute.cpp b/src/execute.cpp index 10263ba12..e2af383f4 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include @@ -60,7 +61,7 @@ static int win32execute(const char* path, int execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err) { - LOG("Executing {}", Util::format_argv_for_logging(argv)); + LOG("Executing {}", util::format_argv_for_logging(argv)); return win32execute(argv[0], argv, @@ -284,7 +285,7 @@ win32execute(const char* path, int execute(Context& ctx, const char* const* argv, Fd&& fd_out, Fd&& fd_err) { - LOG("Executing {}", Util::format_argv_for_logging(argv)); + LOG("Executing {}", util::format_argv_for_logging(argv)); { SignalHandlerBlocker signal_handler_blocker; diff --git a/src/hashutil.cpp b/src/hashutil.cpp index 9a42db9cc..b42289da0 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -374,7 +374,7 @@ hash_command_output(Hash& hash, auto argv = args.to_argv(); LOG("Executing compiler check command {}", - Util::format_argv_for_logging(argv.data())); + util::format_argv_for_logging(argv.data())); #ifdef _WIN32 PROCESS_INFORMATION pi; diff --git a/src/util/string.cpp b/src/util/string.cpp index b3a84e38a..fb3cdea4a 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -47,6 +47,23 @@ split_into(std::string_view string, namespace util { +std::string +format_argv_for_logging(const char* const* argv) +{ + std::string result; + for (size_t i = 0; argv[i]; ++i) { + if (i != 0) { + result += ' '; + } + std::string arg(argv[i]); + if (arg.empty() || arg.find(' ') != std::string::npos) { + arg = FMT("\"{}\"", arg); + } + result += arg; + } + return result; +} + std::string format_base16(nonstd::span data) { diff --git a/src/util/string.hpp b/src/util/string.hpp index d292700c0..f5a9ebc03 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -44,6 +44,10 @@ enum class SizeUnitPrefixType { binary, decimal }; // Return true if `suffix` is a suffix of `string`. bool ends_with(std::string_view string, std::string_view suffix); +// Format `argv` as a simple string for logging purposes. That is, the result is +// not intended to be machine parsable. `argv` must be terminated by a nullptr. +std::string format_argv_for_logging(const char* const* argv); + // Format a hexadecimal string representing `data`. The returned string will be // `2 * data.size()` long. std::string format_base16(nonstd::span data); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index f4ee3ecb3..717badad3 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -139,15 +139,6 @@ TEST_CASE("Util::ensure_dir_exists") doctest::Contains("Failed to create directory create/dir/file:")); } -TEST_CASE("Util::format_argv_for_logging") -{ - const char* argv_0[] = {nullptr}; - CHECK(Util::format_argv_for_logging(argv_0) == ""); - - const char* argv_2[] = {"foo", "bar", nullptr}; - CHECK(Util::format_argv_for_logging(argv_2) == "foo bar"); -} - TEST_CASE("Util::get_extension") { CHECK(Util::get_extension("") == ""); diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 4a02bed54..13b040dda 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -39,6 +39,15 @@ operator==(std::pair> left, TEST_SUITE_BEGIN("util"); +TEST_CASE("util::format_argv_for_logging") +{ + const char* argv_0[] = {nullptr}; + CHECK(util::format_argv_for_logging(argv_0) == ""); + + const char* argv_2[] = {"foo", "bar", nullptr}; + CHECK(util::format_argv_for_logging(argv_2) == "foo bar"); +} + TEST_CASE("util::format_base16") { uint8_t none[] = "";