src/legacy_globals.cpp \
src/legacy_util.cpp \
src/lockfile.cpp \
+ src/logging.cpp \
src/manifest.cpp \
src/result.cpp \
src/stats.cpp
#include "Stat.hpp"
-#include "legacy_util.hpp"
+#include "logging.hpp"
#include "third_party/fmt/core.h"
#include "ZstdCompressor.hpp"
#include "Error.hpp"
-#include "legacy_util.hpp"
+#include "logging.hpp"
const uint8_t k_default_zstd_compression_level = -1;
#include "hashutil.hpp"
#include "language.hpp"
#include "legacy_globals.hpp"
+#include "logging.hpp"
#include "manifest.hpp"
#include "result.hpp"
#include "stats.hpp"
#include "CacheFile.hpp"
#include "Config.hpp"
+#include "logging.hpp"
#include "stats.hpp"
#include <algorithm>
#include "Stat.hpp"
#include "Util.hpp"
#include "ccache.hpp"
+#include "logging.hpp"
using nonstd::string_view;
#include "exitfn.hpp"
#include "legacy_util.hpp"
+#include "logging.hpp"
struct exit_function
{
#include "hash.hpp"
#include "legacy_util.hpp"
+#include "logging.hpp"
#include <blake2.h>
#include "args.hpp"
#include "ccache.hpp"
#include "execute.hpp"
+#include "logging.hpp"
#include "macroskip.hpp"
#include "stats.hpp"
// 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"
#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)
{
#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, ...)
#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);
#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
--- /dev/null
+// 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));
+ }
+}
--- /dev/null
+// 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);
#include "hash.hpp"
#include "hashutil.hpp"
#include "legacy_globals.hpp"
+#include "logging.hpp"
// Manifest data format
// ====================
#include "system.hpp"
+#include "logging.hpp"
+
#include <map>
#include <string>
#include "counters.hpp"
#include "hashutil.hpp"
#include "lockfile.hpp"
+#include "logging.hpp"
#include <cmath>
#include <fcntl.h>