]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Move logging.cpp’s print_command to Util
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 1 Aug 2020 07:33:45 +0000 (09:33 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 1 Aug 2020 19:11:53 +0000 (21:11 +0200)
src/Util.cpp
src/Util.hpp
src/logging.cpp
src/logging.hpp
unittest/CMakeLists.txt
unittest/test_Util.cpp
unittest/test_logging.cpp [deleted file]

index e445cdba90ab890d2bca09ba33f38c2a0058ba24..3692cc0a15b6153912acef254674df13c283bf3b 100644 (file)
@@ -474,6 +474,21 @@ for_each_level_1_subdir(const std::string& cache_dir,
   progress_receiver(1.0);
 }
 
+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 += ' ';
+    }
+    for (const char* arg = argv[i]; *arg; ++arg) {
+      result += *arg;
+    }
+  }
+  return result;
+}
+
 std::string
 format_hex(const uint8_t* data, size_t size)
 {
index b48ca55023195bfb5c0bcda4244d06abc51a7010..0a53a05c5d22e61fdac903616fe2b5eb1080a500 100644 (file)
@@ -151,6 +151,10 @@ void for_each_level_1_subdir(const std::string& cache_dir,
                              const SubdirVisitor& visitor,
                              const ProgressReceiver& progress_receiver);
 
+// 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 `size` bytes of `data`. The returned
 // string will be `2 * size` long.
 std::string format_hex(const uint8_t* data, size_t size);
index ed26788f71d2b9c3f371aba7a39903bced8103e4..5d34d6895261829c4ce62af3020e491c17fa6f3f 100644 (file)
@@ -60,15 +60,6 @@ std::string debug_log_buffer;
 
 bool debug_log_enabled = false;
 
-void
-print_command(FILE* fp, const char* const* argv)
-{
-  for (int i = 0; argv[i]; i++) {
-    fprintf(fp, "%s%s", (i == 0) ? "" : " ", argv[i]);
-  }
-  fprintf(fp, "\n");
-}
-
 } // namespace
 
 // Initialize logging. Call only once.
@@ -212,10 +203,13 @@ cc_log_argv(const char* prefix, const char* const* argv)
     return;
   }
 
+  std::string argv_as_string = Util::format_argv_for_logging(argv);
+
   log_prefix(true);
   if (logfile) {
     fputs(prefix, *logfile);
-    print_command(*logfile, argv);
+    fwrite(argv_as_string.data(), argv_as_string.length(), 1, *logfile);
+    fputc('\n', *logfile);
     int rc = fflush(*logfile);
     if (rc) {
       warn_log_fail();
@@ -223,12 +217,13 @@ cc_log_argv(const char* prefix, const char* const* argv)
   }
 #ifdef HAVE_SYSLOG
   if (use_syslog) {
-    syslog(LOG_DEBUG, "%s", format_command(argv).c_str());
+    syslog(LOG_DEBUG, "%s", Util::format_argv_for_logging(argv).c_str());
   }
 #endif
   if (debug_log_enabled) {
     debug_log_buffer += prefix;
-    debug_log_buffer += format_command(argv);
+    debug_log_buffer += argv_as_string;
+    debug_log_buffer += '\n';
   }
 }
 
@@ -246,19 +241,3 @@ cc_dump_debug_log_buffer(const char* path)
     cc_log("Failed to open %s: %s", path, strerror(errno));
   }
 }
-
-std::string
-format_command(const char* const* argv)
-{
-  std::string result;
-  for (int i = 0; argv[i]; i++) {
-    if (i != 0) {
-      result += ' ';
-    }
-    for (const char* arg = argv[i]; *arg != '\0'; arg++) {
-      result += *arg;
-    }
-  }
-  result += '\n';
-  return result;
-}
index 506edc6979e7f0beacb970f09b88092b93e0e6ef..e65a3535ea669d60236f4385cedc5427fdf45d78 100644 (file)
@@ -29,4 +29,3 @@ void cc_log(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
 void cc_bulklog(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
 void cc_log_argv(const char* prefix, const char* const* argv);
 void cc_dump_debug_log_buffer(const char* path);
-std::string format_command(const char* const* argv);
index cee9924a1b04a4139f11fdf73163f174063886b4..8cc318b8137c4f4310345769ecae2bf4867230a2 100644 (file)
@@ -16,8 +16,7 @@ set(
   test_ZstdCompression.cpp
   test_argprocessing.cpp
   test_compopt.cpp
-  test_hashutil.cpp
-  test_logging.cpp)
+  test_hashutil.cpp)
 
 if(INODE_CACHE_SUPPORTED)
   list(APPEND source_files test_InodeCache.cpp)
index 34f2c5d9d813ef4760bc045cc06988ebc3945bd0..440a6e2f3ebd53aa2eadd6a8bcd5a63ef0c88027 100644 (file)
@@ -232,6 +232,15 @@ TEST_CASE("Util::for_each_level_1_subdir")
   CHECK(actual == expected);
 }
 
+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("format_hex")
 {
   uint8_t none[] = "";
diff --git a/unittest/test_logging.cpp b/unittest/test_logging.cpp
deleted file mode 100644 (file)
index c0b07b9..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright (C) 2010-2020 Joel Rosdahl and other contributors
-//
-// See doc/AUTHORS.adoc for a complete list of contributors.
-//
-// This program is free software; you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3 of the License, or (at your option)
-// any later version.
-//
-// This program is distributed in the hope that it will be useful, but WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-// more details.
-//
-// You should have received a copy of the GNU General Public License along with
-// this program; if not, write to the Free Software Foundation, Inc., 51
-// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-#include "../src/logging.hpp"
-
-#include "third_party/doctest.h"
-
-TEST_SUITE_BEGIN("logging");
-
-TEST_CASE("format_command")
-{
-  const char* argv_0[] = {nullptr};
-  CHECK(format_command(argv_0) == "\n");
-
-  const char* argv_2[] = {"foo", "bar", nullptr};
-  CHECK(format_command(argv_2) == "foo bar\n");
-}
-
-TEST_SUITE_END();