]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify format_command and print_command and move to where used
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 16 Jul 2020 15:03:52 +0000 (17:03 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 17 Jul 2020 11:55:13 +0000 (13:55 +0200)
src/execute.cpp
src/execute.hpp
src/logging.cpp
src/logging.hpp
unittest/CMakeLists.txt
unittest/test_legacy_util.cpp
unittest/test_logging.cpp [new file with mode: 0644]

index 85d97e6f9a3d1728f11cc4a706d99ff6687e4124..16544b08e8441594346a73c3018edad5c7a54254 100644 (file)
@@ -355,36 +355,3 @@ find_executable_in_path(const char* name,
 
   return "";
 }
-
-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");
-}
-
-char*
-format_command(const char* const* argv)
-{
-  size_t len = 0;
-  for (int i = 0; argv[i]; i++) {
-    len += (i == 0) ? 0 : 1;
-    len += strlen(argv[i]);
-  }
-  len += 1;
-  char* buf = static_cast<char*>(x_malloc(len + 1));
-  char* p = buf;
-  for (int i = 0; argv[i]; i++) {
-    if (i != 0) {
-      *p++ = ' ';
-    }
-    for (const char* q = argv[i]; *q != '\0'; q++) {
-      *p++ = *q;
-    }
-  }
-  *p++ = '\n';
-  *p++ = '\0';
-  return buf;
-}
index 00e0a02f134e3f590c8cba48a7f4fc7d487aa230..a8f7fc17d9b67bfc67cc707f7fe35c192e21d20e 100644 (file)
@@ -31,9 +31,6 @@ std::string find_executable_in_path(const char* name,
                                     const char* exclude_name,
                                     const char* path);
 
-void print_command(FILE* fp, const char* const* argv);
-char* format_command(const char* const* argv);
-
 #ifdef _WIN32
 char* win32argvtos(const char* prefix, const char* const* argv, int* length);
 std::string win32getshell(const char* path);
index 9566ee110c2ac75e7cc64a44b2c9146fa1be78f4..472ccd5a6078b4cb25150a9eaec2ab7b48b1557e 100644 (file)
@@ -59,6 +59,15 @@ 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.
@@ -214,16 +223,12 @@ cc_log_argv(const char* prefix, const char* const* argv)
   }
 #ifdef HAVE_SYSLOG
   if (use_syslog) {
-    char* s = format_command(argv);
-    syslog(LOG_DEBUG, "%s", s);
-    free(s);
+    syslog(LOG_DEBUG, "%s", format_command(argv).c_str());
   }
 #endif
   if (debug_log_enabled) {
     debug_log_buffer += prefix;
-    char* s = format_command(argv);
-    debug_log_buffer += s;
-    free(s);
+    debug_log_buffer += format_command(argv);
   }
 }
 
@@ -241,3 +246,19 @@ 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 e65a3535ea669d60236f4385cedc5427fdf45d78..506edc6979e7f0beacb970f09b88092b93e0e6ef 100644 (file)
@@ -29,3 +29,4 @@ 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 c791a6703cef66d987531892daef255c3b810633..5f919d94fbd3962e2c699351744c3fc8ee29b2f6 100644 (file)
@@ -17,7 +17,8 @@ set(
   test_argprocessing.cpp
   test_compopt.cpp
   test_hashutil.cpp
-  test_legacy_util.cpp)
+  test_legacy_util.cpp
+  test_logging.cpp)
 
 if(INODE_CACHE_SUPPORTED)
   list(APPEND source_files test_InodeCache.cpp)
index fb9d9bba3eee111f2422663d9b163bafc255a086..e909e73d8b7ab24a9d1ba732c979f0142fa9f302 100644 (file)
@@ -16,7 +16,6 @@
 // this program; if not, write to the Free Software Foundation, Inc., 51
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
-#include "../src/execute.hpp"
 #include "../src/legacy_util.hpp"
 
 #include "third_party/doctest.h"
@@ -130,9 +129,4 @@ TEST_CASE("parse_size_with_suffix")
   }
 }
 
-TEST_CASE("format_command")
-{
-  const char* argv[] = {"foo", "bar", nullptr};
-
-  CHECK_STR_EQ_FREE2("foo bar\n", format_command(argv));
-}
+TEST_SUITE_END();
diff --git a/unittest/test_logging.cpp b/unittest/test_logging.cpp
new file mode 100644 (file)
index 0000000..c0b07b9
--- /dev/null
@@ -0,0 +1,34 @@
+// 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();