]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Use Config for init_log()
authorThomas Otto <thomas.otto@pdv-fs.de>
Wed, 5 Feb 2020 21:03:24 +0000 (22:03 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 10 Feb 2020 20:08:27 +0000 (21:08 +0100)
src/ccache.cpp
src/logging.cpp
src/logging.hpp

index 79e60e117f83761e8bb4c27edfcedaa1f13fc78a..8ba339a7e4ecd7aa0dbe1482ece007be632ba1a4 100644 (file)
@@ -3487,11 +3487,14 @@ initialize()
 #endif
   }
 
+  set_up_config(g_config);
+
+  init_log(g_config);
+
   exitfn_init();
   exitfn_add_nullary(stats_flush);
   exitfn_add_nullary(clean_up_pending_tmp_files);
 
-  set_up_config(g_config);
 
   cc_log("=== CCACHE %s STARTED =========================================",
          CCACHE_VERSION);
index cfcd8198011467ca81b2799ecc8b7922ff3f260a..f59877ad5f2b5e2aeb15e742934a1dcce555fad8 100644 (file)
@@ -45,6 +45,9 @@
 // Destination for g_config.log_file.
 static FILE* logfile;
 
+// Path to the logfile.
+static std::string logfile_path;
+
 // Whether to use syslog() instead.
 static bool use_syslog;
 
@@ -59,28 +62,32 @@ static size_t debug_log_size;
 
 #define DEBUG_LOG_BUFFER_MARGIN 1024
 
-static bool
-init_log(void)
+// Initialize logging. Call only once.
+bool
+init_log(const Config& config)
 {
-  if (debug_log_buffer || logfile || use_syslog) {
-    return true;
+  logfile_path = config.log_file();
+
+  if (logfile_path.empty()) {
+    return false;
   }
-  if (g_config.debug()) {
+
+  if (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();
+  if (config.log_file().empty()) {
+    return config.debug();
   }
 #ifdef HAVE_SYSLOG
-  if (g_config.log_file() == "syslog") {
+  if (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");
+  logfile = fopen(logfile_path.c_str(), "a");
   if (logfile) {
 #ifndef _WIN32
     set_cloexec_flag(fileno(logfile));
@@ -152,7 +159,7 @@ 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(),
+          logfile_path.c_str(),
           strerror(errno));
   x_exit(EXIT_FAILURE);
 }
@@ -160,7 +167,7 @@ warn_log_fail(void)
 static void
 vlog(const char* format, va_list ap, bool log_updated_time)
 {
-  if (!init_log()) {
+  if (!(debug_log_buffer || logfile || use_syslog)) {
     return;
   }
 
@@ -218,7 +225,7 @@ cc_bulklog(const char* format, ...)
 void
 cc_log_argv(const char* prefix, char** argv)
 {
-  if (!init_log()) {
+  if (!(debug_log_buffer || logfile || use_syslog)) {
     return;
   }
 
index e80be653d5ad7e497bc0ff0713a7bad32c3f2238..a07c539200057ff49290124712be7f7268301cef 100644 (file)
@@ -22,6 +22,9 @@
 
 #include <string>
 
+class Config;
+
+bool init_log(const Config& config);
 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);