]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Move logging into separate file (#532)
authorThomas Otto <thomas.otto@pdv-fs.de>
Sat, 8 Feb 2020 16:04:28 +0000 (17:04 +0100)
committerGitHub <noreply@github.com>
Sat, 8 Feb 2020 16:04:28 +0000 (17:04 +0100)
17 files changed:
Makefile.in
src/Stat.cpp
src/ZstdCompressor.cpp
src/ccache.cpp
src/cleanup.cpp
src/execute.cpp
src/exitfn.cpp
src/hash.cpp
src/hashutil.cpp
src/legacy_util.cpp
src/legacy_util.hpp
src/lockfile.cpp
src/logging.cpp [new file with mode: 0644]
src/logging.hpp [new file with mode: 0644]
src/manifest.cpp
src/result.hpp
src/stats.cpp

index 1edafa60797d4f91cd90f5a88f061bbad3299f33..2d0e367eee28e4f3c55ca4273ed1e1d8d51ed10e 100644 (file)
@@ -61,6 +61,7 @@ non_third_party_sources = \
     src/legacy_globals.cpp \
     src/legacy_util.cpp \
     src/lockfile.cpp \
+    src/logging.cpp \
     src/manifest.cpp \
     src/result.cpp \
     src/stats.cpp
index c8b750afee30a9e9e3925fe2adfb32bbda421a77..7b398ba6996c2b884443f642e9f70941cbc0360e 100644 (file)
@@ -18,7 +18,7 @@
 
 #include "Stat.hpp"
 
-#include "legacy_util.hpp"
+#include "logging.hpp"
 
 #include "third_party/fmt/core.h"
 
index 424f9ba7fa07518a8d6e1fcca7339c09b4c59caf..a5d16854b6d2b4ef203242e74fa2d3695d4d70d5 100644 (file)
@@ -19,7 +19,7 @@
 #include "ZstdCompressor.hpp"
 
 #include "Error.hpp"
-#include "legacy_util.hpp"
+#include "logging.hpp"
 
 const uint8_t k_default_zstd_compression_level = -1;
 
index b54f74716496772a273fedfa038f3d29b66ab14d..8d35dcf6acaf9d6b1cdd9c66d0eb3b7518cd094e 100644 (file)
@@ -35,6 +35,7 @@
 #include "hashutil.hpp"
 #include "language.hpp"
 #include "legacy_globals.hpp"
+#include "logging.hpp"
 #include "manifest.hpp"
 #include "result.hpp"
 #include "stats.hpp"
index 318f2ee5ccdaa026b5311e74c92fecbe3a7e3fc6..65589b6defdd30bc8a3387385d48d87bd479bf2c 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "CacheFile.hpp"
 #include "Config.hpp"
+#include "logging.hpp"
 #include "stats.hpp"
 
 #include <algorithm>
index 7ab4c0d47138eb63b79f8d2ed6dbfce79cf567b2..0413efc96f27dca232d269bbc92a8261e18b87ff 100644 (file)
@@ -23,6 +23,7 @@
 #include "Stat.hpp"
 #include "Util.hpp"
 #include "ccache.hpp"
+#include "logging.hpp"
 
 using nonstd::string_view;
 
index ea7d2e68dfb7ccf104583fef7229139caa25053e..5f5d137580cdd46094c02374cb4715f88e1352cf 100644 (file)
@@ -19,6 +19,7 @@
 #include "exitfn.hpp"
 
 #include "legacy_util.hpp"
+#include "logging.hpp"
 
 struct exit_function
 {
index 4e3c76f5179e49bbbee6fe5b8cba7cecf7fbd298..da0a813b5ecf3d249906f1e8baafacb87244eea3 100644 (file)
@@ -20,6 +20,7 @@
 #include "hash.hpp"
 
 #include "legacy_util.hpp"
+#include "logging.hpp"
 
 #include <blake2.h>
 
index 44eacdf155d30c9c6f2b2567c987c232b7c210f9..53e7f1e45d6b00cfb8bbf13928149eed6a3a4fa0 100644 (file)
@@ -22,6 +22,7 @@
 #include "args.hpp"
 #include "ccache.hpp"
 #include "execute.hpp"
+#include "logging.hpp"
 #include "macroskip.hpp"
 #include "stats.hpp"
 
index 613d5bf84519e13c53685df1bbaa4cbad4b6c271..8aee912c3c2cbe5b80a25285a9a64d9b3d20c655 100644 (file)
 // this program; if not, write to the Free Software Foundation, Inc., 51
 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
-#include "Config.hpp"
+#include "legacy_util.hpp"
+
 #include "Error.hpp"
 #include "Util.hpp"
-#include "execute.hpp"
+#include "logging.hpp"
 
 #include "third_party/fmt/core.h"
 
@@ -29,9 +30,6 @@
 #ifdef HAVE_PWD_H
 #  include <pwd.h>
 #endif
-#ifdef HAVE_SYSLOG_H
-#  include <syslog.h>
-#endif
 #ifdef HAVE_SYS_TIME_H
 #  include <sys/time.h>
 #endif
 #  include <tchar.h>
 #endif
 
-// Destination for g_config.log_file.
-static FILE* logfile;
-
-// Whether to use syslog() instead.
-static bool use_syslog;
-
-// Buffer used for logs in debug mode.
-static char* debug_log_buffer;
-
-// Allocated debug_log_buffer size.
-static size_t debug_log_buffer_capacity;
-
-// The amount of log data stored in debug_log_buffer.
-static size_t debug_log_size;
-
-#define DEBUG_LOG_BUFFER_MARGIN 1024
-
-static bool
-init_log(void)
-{
-  if (debug_log_buffer || logfile || use_syslog) {
-    return true;
-  }
-  if (g_config.debug()) {
-    debug_log_buffer_capacity = DEBUG_LOG_BUFFER_MARGIN;
-    debug_log_buffer = static_cast<char*>(x_malloc(debug_log_buffer_capacity));
-    debug_log_size = 0;
-  }
-  if (g_config.log_file().empty()) {
-    return g_config.debug();
-  }
-#ifdef HAVE_SYSLOG
-  if (g_config.log_file() == "syslog") {
-    use_syslog = true;
-    openlog("ccache", LOG_PID, LOG_USER);
-    return true;
-  }
-#endif
-  logfile = fopen(g_config.log_file().c_str(), "a");
-  if (logfile) {
-#ifndef _WIN32
-    set_cloexec_flag(fileno(logfile));
-#endif
-    return true;
-  } else {
-    return false;
-  }
-}
-
-static void
-append_to_debug_log(const char* s, size_t len)
-{
-  assert(debug_log_buffer);
-  if (debug_log_size + len + 1 > debug_log_buffer_capacity) {
-    debug_log_buffer_capacity += len + 1 + DEBUG_LOG_BUFFER_MARGIN;
-    debug_log_buffer = static_cast<char*>(
-      x_realloc(debug_log_buffer, debug_log_buffer_capacity));
-  }
-  memcpy(debug_log_buffer + debug_log_size, s, len);
-  debug_log_size += len;
-}
-
-static void
-log_prefix(bool log_updated_time)
-{
-  static char prefix[200];
-#ifdef HAVE_GETTIMEOFDAY
-  if (log_updated_time) {
-    char timestamp[100];
-    struct tm tm;
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-#  ifdef __MINGW64_VERSION_MAJOR
-    localtime_r((time_t*)&tv.tv_sec, &tm);
-#  else
-    localtime_r(&tv.tv_sec, &tm);
-#  endif
-    strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &tm);
-    snprintf(prefix,
-             sizeof(prefix),
-             "[%s.%06d %-5d] ",
-             timestamp,
-             (int)tv.tv_usec,
-             (int)getpid());
-  }
-#else
-  snprintf(prefix, sizeof(prefix), "[%-5d] ", (int)getpid());
-#endif
-  if (logfile) {
-    fputs(prefix, logfile);
-  }
-#ifdef HAVE_SYSLOG
-  if (use_syslog) {
-    // prefix information will be added by syslog
-  }
-#endif
-  if (debug_log_buffer) {
-    append_to_debug_log(prefix, strlen(prefix));
-  }
-}
-
 static long
 path_max(const char* path)
 {
@@ -178,122 +75,6 @@ path_max(const char* path)
 #endif
 }
 
-static void warn_log_fail(void) ATTR_NORETURN;
-
-// Warn about failure writing to the log file and then exit.
-static void
-warn_log_fail(void)
-{
-  // Note: Can't call fatal() since that would lead to recursion.
-  fprintf(stderr,
-          "ccache: error: Failed to write to %s: %s\n",
-          g_config.log_file().c_str(),
-          strerror(errno));
-  x_exit(EXIT_FAILURE);
-}
-
-static void
-vlog(const char* format, va_list ap, bool log_updated_time)
-{
-  if (!init_log()) {
-    return;
-  }
-
-  va_list aq;
-  va_copy(aq, ap);
-  log_prefix(log_updated_time);
-  if (logfile) {
-    int rc1 = vfprintf(logfile, format, ap);
-    int rc2 = fprintf(logfile, "\n");
-    if (rc1 < 0 || rc2 < 0) {
-      warn_log_fail();
-    }
-  }
-#ifdef HAVE_SYSLOG
-  if (use_syslog) {
-    vsyslog(LOG_DEBUG, format, ap);
-  }
-#endif
-  if (debug_log_buffer) {
-    char buf[8192];
-    int len = vsnprintf(buf, sizeof(buf), format, aq);
-    if (len >= 0) {
-      append_to_debug_log(buf, std::min((size_t)len, sizeof(buf) - 1));
-      append_to_debug_log("\n", 1);
-    }
-  }
-  va_end(aq);
-}
-
-// Write a message to the log file (adding a newline) and flush.
-void
-cc_log(const char* format, ...)
-{
-  va_list ap;
-  va_start(ap, format);
-  vlog(format, ap, true);
-  va_end(ap);
-  if (logfile) {
-    fflush(logfile);
-  }
-}
-
-// Write a message to the log file (adding a newline) without flushing and with
-// a reused timestamp.
-void
-cc_bulklog(const char* format, ...)
-{
-  va_list ap;
-  va_start(ap, format);
-  vlog(format, ap, false);
-  va_end(ap);
-}
-
-// Log an executed command to the CCACHE_LOGFILE location.
-void
-cc_log_argv(const char* prefix, char** argv)
-{
-  if (!init_log()) {
-    return;
-  }
-
-  log_prefix(true);
-  if (logfile) {
-    fputs(prefix, logfile);
-    print_command(logfile, argv);
-    int rc = fflush(logfile);
-    if (rc) {
-      warn_log_fail();
-    }
-  }
-#ifdef HAVE_SYSLOG
-  if (use_syslog) {
-    char* s = format_command(argv);
-    syslog(LOG_DEBUG, "%s", s);
-    free(s);
-  }
-#endif
-  if (debug_log_buffer) {
-    append_to_debug_log(prefix, strlen(prefix));
-    char* s = format_command(argv);
-    append_to_debug_log(s, strlen(s));
-    free(s);
-  }
-}
-
-// Copy the current log memory buffer to an output file.
-void
-cc_dump_debug_log_buffer(const char* path)
-{
-  FILE* file = fopen(path, "w");
-  if (file) {
-    (void)fwrite(debug_log_buffer, 1, debug_log_size, file);
-    fclose(file);
-  } else {
-    cc_log("Failed to open %s: %s", path, strerror(errno));
-  }
-}
-
 // Something went badly wrong!
 void
 fatal(const char* format, ...)
index 9b622ce91eb29d1c72f1313dc160cb301e9859c2..d18266ecb4f28f7c30078e2300d730d454c15a37 100644 (file)
 
 #include <string>
 
-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, char** argv);
-void cc_dump_debug_log_buffer(const char* path);
-void fatal(const char* format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
-void warn(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
-
 bool copy_fd(int fd_in, int fd_out);
 bool clone_file(const char* src, const char* dest, bool via_tmp_file);
 bool copy_file(const char* src, const char* dest, bool via_tmp_file);
index ce3029cf7c82ff0fc72f858f6fb38202abea02eb..390bb587ffacd87f60bb6cebeba37be145619410 100644 (file)
@@ -19,6 +19,7 @@
 #include "lockfile.hpp"
 
 #include "Util.hpp"
+#include "logging.hpp"
 
 // This function acquires a lockfile for the given path. Returns true if the
 // lock was acquired, otherwise false. If the lock has been considered stale
diff --git a/src/logging.cpp b/src/logging.cpp
new file mode 100644 (file)
index 0000000..44f5a6d
--- /dev/null
@@ -0,0 +1,259 @@
+// Copyright (C) 2002 Andrew Tridgell
+// Copyright (C) 2009-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 "logging.hpp"
+
+#include "Config.hpp"
+#include "execute.hpp"
+
+#ifdef HAVE_SYSLOG_H
+#  include <syslog.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
+#  include <sys/time.h>
+#endif
+
+#ifdef __linux__
+#  ifdef HAVE_SYS_IOCTL_H
+#    include <sys/ioctl.h>
+#  endif
+#endif
+
+#ifdef _WIN32
+#  include <psapi.h>
+#  include <sys/locking.h>
+#  include <tchar.h>
+#endif
+
+// Destination for g_config.log_file.
+static FILE* logfile;
+
+// Whether to use syslog() instead.
+static bool use_syslog;
+
+// Buffer used for logs in debug mode.
+static char* debug_log_buffer;
+
+// Allocated debug_log_buffer size.
+static size_t debug_log_buffer_capacity;
+
+// The amount of log data stored in debug_log_buffer.
+static size_t debug_log_size;
+
+#define DEBUG_LOG_BUFFER_MARGIN 1024
+
+static bool
+init_log(void)
+{
+  if (debug_log_buffer || logfile || use_syslog) {
+    return true;
+  }
+  if (g_config.debug()) {
+    debug_log_buffer_capacity = DEBUG_LOG_BUFFER_MARGIN;
+    debug_log_buffer = static_cast<char*>(x_malloc(debug_log_buffer_capacity));
+    debug_log_size = 0;
+  }
+  if (g_config.log_file().empty()) {
+    return g_config.debug();
+  }
+#ifdef HAVE_SYSLOG
+  if (g_config.log_file() == "syslog") {
+    use_syslog = true;
+    openlog("ccache", LOG_PID, LOG_USER);
+    return true;
+  }
+#endif
+  logfile = fopen(g_config.log_file().c_str(), "a");
+  if (logfile) {
+#ifndef _WIN32
+    set_cloexec_flag(fileno(logfile));
+#endif
+    return true;
+  } else {
+    return false;
+  }
+}
+
+static void
+append_to_debug_log(const char* s, size_t len)
+{
+  assert(debug_log_buffer);
+  if (debug_log_size + len + 1 > debug_log_buffer_capacity) {
+    debug_log_buffer_capacity += len + 1 + DEBUG_LOG_BUFFER_MARGIN;
+    debug_log_buffer = static_cast<char*>(
+      x_realloc(debug_log_buffer, debug_log_buffer_capacity));
+  }
+  memcpy(debug_log_buffer + debug_log_size, s, len);
+  debug_log_size += len;
+}
+
+static void
+log_prefix(bool log_updated_time)
+{
+  static char prefix[200];
+#ifdef HAVE_GETTIMEOFDAY
+  if (log_updated_time) {
+    char timestamp[100];
+    struct tm tm;
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+#  ifdef __MINGW64_VERSION_MAJOR
+    localtime_r((time_t*)&tv.tv_sec, &tm);
+#  else
+    localtime_r(&tv.tv_sec, &tm);
+#  endif
+    strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &tm);
+    snprintf(prefix,
+             sizeof(prefix),
+             "[%s.%06d %-5d] ",
+             timestamp,
+             (int)tv.tv_usec,
+             (int)getpid());
+  }
+#else
+  snprintf(prefix, sizeof(prefix), "[%-5d] ", (int)getpid());
+#endif
+  if (logfile) {
+    fputs(prefix, logfile);
+  }
+#ifdef HAVE_SYSLOG
+  if (use_syslog) {
+    // prefix information will be added by syslog
+  }
+#endif
+  if (debug_log_buffer) {
+    append_to_debug_log(prefix, strlen(prefix));
+  }
+}
+
+static void warn_log_fail(void) ATTR_NORETURN;
+
+// Warn about failure writing to the log file and then exit.
+static void
+warn_log_fail(void)
+{
+  // Note: Can't call fatal() since that would lead to recursion.
+  fprintf(stderr,
+          "ccache: error: Failed to write to %s: %s\n",
+          g_config.log_file().c_str(),
+          strerror(errno));
+  x_exit(EXIT_FAILURE);
+}
+
+static void
+vlog(const char* format, va_list ap, bool log_updated_time)
+{
+  if (!init_log()) {
+    return;
+  }
+
+  va_list aq;
+  va_copy(aq, ap);
+  log_prefix(log_updated_time);
+  if (logfile) {
+    int rc1 = vfprintf(logfile, format, ap);
+    int rc2 = fprintf(logfile, "\n");
+    if (rc1 < 0 || rc2 < 0) {
+      warn_log_fail();
+    }
+  }
+#ifdef HAVE_SYSLOG
+  if (use_syslog) {
+    vsyslog(LOG_DEBUG, format, ap);
+  }
+#endif
+  if (debug_log_buffer) {
+    char buf[8192];
+    int len = vsnprintf(buf, sizeof(buf), format, aq);
+    if (len >= 0) {
+      append_to_debug_log(buf, std::min((size_t)len, sizeof(buf) - 1));
+      append_to_debug_log("\n", 1);
+    }
+  }
+  va_end(aq);
+}
+
+// Write a message to the log file (adding a newline) and flush.
+void
+cc_log(const char* format, ...)
+{
+  va_list ap;
+  va_start(ap, format);
+  vlog(format, ap, true);
+  va_end(ap);
+  if (logfile) {
+    fflush(logfile);
+  }
+}
+
+// Write a message to the log file (adding a newline) without flushing and with
+// a reused timestamp.
+void
+cc_bulklog(const char* format, ...)
+{
+  va_list ap;
+  va_start(ap, format);
+  vlog(format, ap, false);
+  va_end(ap);
+}
+
+// Log an executed command to the CCACHE_LOGFILE location.
+void
+cc_log_argv(const char* prefix, char** argv)
+{
+  if (!init_log()) {
+    return;
+  }
+
+  log_prefix(true);
+  if (logfile) {
+    fputs(prefix, logfile);
+    print_command(logfile, argv);
+    int rc = fflush(logfile);
+    if (rc) {
+      warn_log_fail();
+    }
+  }
+#ifdef HAVE_SYSLOG
+  if (use_syslog) {
+    char* s = format_command(argv);
+    syslog(LOG_DEBUG, "%s", s);
+    free(s);
+  }
+#endif
+  if (debug_log_buffer) {
+    append_to_debug_log(prefix, strlen(prefix));
+    char* s = format_command(argv);
+    append_to_debug_log(s, strlen(s));
+    free(s);
+  }
+}
+
+// Copy the current log memory buffer to an output file.
+void
+cc_dump_debug_log_buffer(const char* path)
+{
+  FILE* file = fopen(path, "w");
+  if (file) {
+    (void)fwrite(debug_log_buffer, 1, debug_log_size, file);
+    fclose(file);
+  } else {
+    cc_log("Failed to open %s: %s", path, strerror(errno));
+  }
+}
diff --git a/src/logging.hpp b/src/logging.hpp
new file mode 100644 (file)
index 0000000..52e7cc6
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (C) 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
+
+#pragma once
+
+#include "system.hpp"
+
+#include <string>
+
+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, char** argv);
+void cc_dump_debug_log_buffer(const char* path);
+void fatal(const char* format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
+void warn(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
index 3eafd87f0c2530f2082f4323c661ccd6b14f585b..3a969549031bcb7dc1c7f16b1359d3c93914a2a5 100644 (file)
@@ -29,6 +29,7 @@
 #include "hash.hpp"
 #include "hashutil.hpp"
 #include "legacy_globals.hpp"
+#include "logging.hpp"
 
 // Manifest data format
 // ====================
index d169a056a8dcdc8f21d5358d77fd03f3af502662..535573b9034e995975d7803cecffecb4396c399c 100644 (file)
@@ -20,6 +20,8 @@
 
 #include "system.hpp"
 
+#include "logging.hpp"
+
 #include <map>
 #include <string>
 
index 02ce7aeb31c2d0c2435ffda067f787ed44b7d828..eaa7c3a423694c700563f18fd0acb0d54eb53ecb 100644 (file)
@@ -27,6 +27,7 @@
 #include "counters.hpp"
 #include "hashutil.hpp"
 #include "lockfile.hpp"
+#include "logging.hpp"
 
 #include <cmath>
 #include <fcntl.h>