]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
logging: allow logging to file instead of syslog
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 24 May 2017 13:38:43 +0000 (15:38 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Fri, 26 May 2017 11:33:53 +0000 (13:33 +0200)
logging.c
logging.h

index 7f71719d1c586127ed196c19f5d80853eddf0045..a87aca363d1f084ae558069018035835a06de777 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -40,6 +40,7 @@ int log_debug_enabled = 0;
 /* Flag indicating we have initialised */
 static int initialised = 0;
 
+static FILE *file_log;
 static int system_log = 0;
 
 static int parent_fd = 0;
@@ -69,6 +70,7 @@ void
 LOG_Initialise(void)
 {
   initialised = 1;
+  file_log = stderr;
 }
 
 /* ================================================== */
@@ -79,6 +81,8 @@ LOG_Finalise(void)
 {
   if (system_log) {
     closelog();
+  } else {
+    fclose(file_log);
   }
 
   LOG_CycleLogFiles();
@@ -113,7 +117,7 @@ static void log_message(int fatal, LOG_Severity severity, const char *message)
     }
     syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
   } else {
-    fprintf(stderr, fatal ? "Fatal error : %s\n" : "%s\n", message);
+    fprintf(file_log, fatal ? "Fatal error : %s\n" : "%s\n", message);
   }
 }
 
@@ -135,10 +139,10 @@ void LOG_Message(LOG_Severity severity,
     time(&t);
     stm = *gmtime(&t);
     strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm);
-    fprintf(stderr, "%s ", buf);
+    fprintf(file_log, "%s ", buf);
 #if DEBUG > 0
     if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION)
-      fprintf(stderr, "%s:%d:(%s) ", filename, line_number, function_name);
+      fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);
 #endif
   }
 
@@ -173,6 +177,21 @@ void LOG_Message(LOG_Severity severity,
   }
 }
 
+/* ================================================== */
+
+void
+LOG_OpenFileLog(const char *log_file)
+{
+  FILE *f;
+
+  f = fopen(log_file, "a");
+  if (!f)
+    LOG_FATAL("Could not open log file %s", log_file);
+
+  file_log = f;
+}
+
+
 /* ================================================== */
 
 void
index ed3acca899d88ca6919efcd7ecad3e0804113e28..c50bcf527f93c18d2e4739765fec397d01c939fd 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -99,6 +99,9 @@ extern void LOG_Message(LOG_Severity severity, const char *format, ...);
    */
 extern void LOG_SetDebugLevel(int level);
 
+/* Log messages to a file instead of stderr */
+extern void LOG_OpenFileLog(const char *log_file);
+
 /* Log messages to syslog instead of stderr */
 extern void LOG_OpenSystemLog(void);