]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::format_argv_for_logging to util
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Jul 2023 15:26:23 +0000 (17:26 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:36:15 +0000 (21:36 +0200)
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/execute.cpp
src/hashutil.cpp
src/util/string.cpp
src/util/string.hpp
unittest/test_Util.cpp
unittest/test_util_string.cpp

index fb25c052514343ff441b5aa1efbcf87273738435..28893639ebdb6749840b6c312f5eab036a0ca31f 100644 (file)
@@ -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)
 {
index 81f1c542c0bc738485e63aad8366f318424d407d..d663efc15cff41607a4b83aff73e85fb5cae6996 100644 (file)
@@ -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);
index 0a67083510517c7605a44342a2f1e545089fc0b4..2292d3347935a5c747f24501793e1eb230ada6e7 100644 (file)
@@ -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.
     }
index 10263ba12734d25fb02f316e3449632bb4e4418f..e2af383f4959fc9a5309dccd63500c4910f86c86 100644 (file)
@@ -34,6 +34,7 @@
 #include <fmtmacros.hpp>
 #include <util/file.hpp>
 #include <util/path.hpp>
+#include <util/string.hpp>
 
 #include <vector>
 
@@ -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;
index 9a42db9cc2936d98f7d3cb5f747de7514fb60db2..b42289da0ea07e3a6b6fb553b44d808f8d764dbc 100644 (file)
@@ -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;
index b3a84e38a121f1ab80ebde0f777296e468d2fa55..fb3cdea4aa2138d77b402e22ed7c48fca1ee0c09 100644 (file)
@@ -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<const uint8_t> data)
 {
index d292700c0bd273a76157c65c14639324f3c1d528..f5a9ebc03fd060cd328ed13f79450f63504bef00 100644 (file)
@@ -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<const uint8_t> data);
index f4ee3ecb39fa8c4ea84282a105c2497001ed8fe1..717badad32124b881727d322101074665f91c970 100644 (file)
@@ -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("") == "");
index 4a02bed54ad72b8502421836d3b454be6026937b..13b040dda06262f88ded6cc39ab2e1402529323a 100644 (file)
@@ -39,6 +39,15 @@ operator==(std::pair<std::string_view, std::optional<std::string_view>> 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[] = "";