]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Use File wrapper for static log file object
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 16 Jul 2020 14:36:46 +0000 (16:36 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 17 Jul 2020 11:55:13 +0000 (13:55 +0200)
This flushes and closes the underlying FILE object properly on exit.

src/File.hpp
src/logging.cpp

index bfe229ef9c8ec174821b4be1f69ebef2a86cbd4c..0d51f60244ba1c3c661b8f645b2708eb16ae28d5 100644 (file)
@@ -73,6 +73,14 @@ public:
     return m_file;
   }
 
+  // clang-format off
+  FILE*
+  operator*() const
+  // clang-format on
+  {
+    return m_file;
+  }
+
   FILE*
   get()
   {
index fe5dad858826315f8a4773d0d1b39beecef70b9a..b87a8baa0666f061906d281fafb55141ce174fcf 100644 (file)
@@ -20,6 +20,7 @@
 #include "logging.hpp"
 
 #include "Config.hpp"
+#include "File.hpp"
 #include "exceptions.hpp"
 #include "execute.hpp"
 
@@ -43,7 +44,7 @@
 #endif
 
 // Destination for g_config.log_file.
-static FILE* logfile;
+static File logfile;
 
 // Path to the logfile.
 static std::string logfile_path;
@@ -81,10 +82,10 @@ init_log(const Config& config)
 #endif
 
   logfile_path = config.log_file();
-  logfile = fopen(logfile_path.c_str(), "a");
+  logfile.open(logfile_path, "a");
 #ifndef _WIN32
   if (logfile) {
-    set_cloexec_flag(fileno(logfile));
+    set_cloexec_flag(fileno(*logfile));
   }
 #endif
 }
@@ -129,7 +130,7 @@ log_prefix(bool log_updated_time)
   snprintf(prefix, sizeof(prefix), "[%-5d] ", (int)getpid());
 #endif
   if (logfile) {
-    fputs(prefix, logfile);
+    fputs(prefix, *logfile);
   }
 #ifdef HAVE_SYSLOG
   if (use_syslog) {
@@ -166,8 +167,8 @@ vlog(const char* format, va_list ap, bool log_updated_time)
   va_copy(aq, ap);
   log_prefix(log_updated_time);
   if (logfile) {
-    int rc1 = vfprintf(logfile, format, ap);
-    int rc2 = fprintf(logfile, "\n");
+    int rc1 = vfprintf(*logfile, format, ap);
+    int rc2 = fprintf(*logfile, "\n");
     if (rc1 < 0 || rc2 < 0) {
       warn_log_fail();
     }
@@ -197,7 +198,7 @@ cc_log(const char* format, ...)
   vlog(format, ap, true);
   va_end(ap);
   if (logfile) {
-    fflush(logfile);
+    fflush(*logfile);
   }
 }
 
@@ -222,9 +223,9 @@ cc_log_argv(const char* prefix, const char* const* argv)
 
   log_prefix(true);
   if (logfile) {
-    fputs(prefix, logfile);
-    print_command(logfile, argv);
-    int rc = fflush(logfile);
+    fputs(prefix, *logfile);
+    print_command(*logfile, argv);
+    int rc = fflush(*logfile);
     if (rc) {
       warn_log_fail();
     }