]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Refactor file logging
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 13 Apr 2010 13:09:28 +0000 (15:09 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 13 Apr 2010 13:16:41 +0000 (15:16 +0200)
15 files changed:
cmdmon.c
logging.c
logging.h
main.c
ntp_core.c
ntp_core.h
refclock.c
refclock.h
reference.c
reference.h
rtc.c
rtc.h
rtc_linux.c
sourcestats.c
sourcestats.h

index 05ebfc565221aa0f70bc815d1e2bc14d430cb9b5..5f0e9eb09ed0a30978c0a094fe38bf8ef819996d 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -1459,11 +1459,7 @@ handle_trimrtc(CMD_Request *rx_message, CMD_Reply *tx_message)
 static void
 handle_cyclelogs(CMD_Request *rx_message, CMD_Reply *tx_message)
 {
-  NCR_CycleLogFile();
-  SST_CycleLogFile();
-  REF_CycleLogFile();
-  RTC_CycleLogFile();
-  RCL_CycleLogFile();
+  LOG_CycleLogFiles();
   
   tx_message->status = htons(STT_SUCCESS);
   return;
index ced17235515b03bc394bfa9e6b78c778737a0d58..8271c001743170c2d41eff347a34b73f5a91d610 100644 (file)
--- a/logging.c
+++ b/logging.c
 #include "sysincl.h"
 
 #include "main.h"
+#include "conf.h"
 #include "logging.h"
 #include "version.h"
+#include "mkdirpp.h"
 
 /* ================================================== */
 /* Flag indicating we have initialised */
@@ -46,6 +48,20 @@ static time_t last_limited = 0;
 static FILE *logfile;
 #endif
 
+struct LogFile {
+  const char *name;
+  const char *banner;
+  FILE *file;
+  unsigned long writes;
+};
+
+static int n_filelogs = 0;
+
+/* Increase this when adding a new logfile */
+#define MAX_FILELOGS 5
+
+static struct LogFile logfiles[MAX_FILELOGS];
+
 /* ================================================== */
 /* Init function */
 
@@ -77,6 +93,8 @@ LOG_Finalise(void)
   }
 #endif
 
+  LOG_CycleLogFiles();
+
   initialised = 0;
   return;
 }
@@ -248,3 +266,96 @@ croak(const char *file, int line, const char *msg)
 }
 
 /* ================================================== */
+
+LOG_FileID
+LOG_FileOpen(const char *name, const char *banner)
+{
+  assert(n_filelogs < MAX_FILELOGS);
+
+  logfiles[n_filelogs].name = name;
+  logfiles[n_filelogs].banner = banner;
+  logfiles[n_filelogs].file = NULL;
+  logfiles[n_filelogs].writes = 0;
+
+  return n_filelogs++;
+}
+
+/* ================================================== */
+
+void
+LOG_FileWrite(LOG_FileID id, const char *format, ...)
+{
+  va_list other_args;
+
+  if (id < 0 || id >= n_filelogs || !logfiles[id].name)
+    return;
+
+  if (!logfiles[id].file) {
+    char filename[512];
+
+    if (snprintf(filename, sizeof(filename), "%s/%s.log",
+          CNF_GetLogDir(), logfiles[id].name) >= sizeof(filename) ||
+        !(logfiles[id].file = fopen(filename, "a"))) {
+      LOG(LOGS_WARN, LOGF_Refclock, "Couldn't open logfile %s for update", filename);
+      logfiles[id].name = NULL;
+      return;
+    }
+  }
+
+  if (logfiles[id].writes++ % 32 == 0) {
+    char bannerline[256];
+    int i, bannerlen;
+
+    bannerlen = strlen(logfiles[id].banner);
+
+    for (i = 0; i < bannerlen; i++)
+      bannerline[i] = '=';
+    bannerline[i] = '\0';
+
+    fprintf(logfiles[id].file, "%s\n", bannerline);
+    fprintf(logfiles[id].file, "%s\n", logfiles[id].banner);
+    fprintf(logfiles[id].file, "%s\n", bannerline);
+  }
+
+  va_start(other_args, format);
+  vfprintf(logfiles[id].file, format, other_args);
+  va_end(other_args);
+  fprintf(logfiles[id].file, "\n");
+
+  fflush(logfiles[id].file);
+}
+
+/* ================================================== */
+
+void
+LOG_CreateLogFileDir(void)
+{
+  const char *logdir;
+
+  if (n_filelogs <= 0)
+    return;
+
+  logdir = CNF_GetLogDir();
+
+  if (!mkdir_and_parents(logdir)) {
+    LOG(LOGS_ERR, LOGF_Logging, "Could not create directory %s", logdir);
+    n_filelogs = 0;
+  }
+}
+
+/* ================================================== */
+
+void
+LOG_CycleLogFiles(void)
+{
+  LOG_FileID i;
+
+  for (i = 0; i < n_filelogs; i++) {
+    if (logfiles[i].file)
+      fclose(logfiles[i].file);
+    logfiles[i].file = NULL;
+    logfiles[i].writes = 0;
+  }
+}
+
+/* ================================================== */
index 250cc2cb3ee2d650b0bf11bbe7d1d791acc019cc..c309c666c4561c323da9b53a555577fe3d134951 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -109,4 +109,14 @@ extern int croak(const char *file, int line, const char *msg);
 #define CROAK(message) croak(__FILE__, __LINE__, message);
 #endif
 
+/* File logging functions */
+
+typedef int LOG_FileID;
+
+extern LOG_FileID LOG_FileOpen(const char *name, const char *banner);
+extern void LOG_FileWrite(LOG_FileID id, const char *format, ...);
+
+extern void LOG_CreateLogFileDir(void);
+extern void LOG_CycleLogFiles(void);
+
 #endif /* GOT_LOGGING_H */
diff --git a/main.c b/main.c
index 99aff1729c31e891b7fff356795b8aec1edb6b9b..31f4a5c93efcc3a848042d4f7126ec8295b9a2fa 100644 (file)
--- a/main.c
+++ b/main.c
@@ -327,6 +327,8 @@ int main
   ACQ_Initialise();
   MNL_Initialise();
 
+  LOG_CreateLogFileDir();
+
   /* From now on, it is safe to do finalisation on exit */
   initialised = 1;
 
index 6e3abf76b9ed881b7c6a4242d24d5c7bdf57f874..866d120dcd8eb854030f1aa167ec91734474d50e 100644 (file)
 #include "keys.h"
 #include "md5.h"
 #include "addrfilt.h"
-#include "mkdirpp.h"
 #include "clientlog.h"
 
 /* ================================================== */
-/* File handle for file to which we write the measurement log */
-static FILE *logfile = NULL;
 
-static char *logfilename = NULL;
-static unsigned long logwrites=0;
-
-#define MEASUREMENTS_LOG "measurements.log" 
+static LOG_FileID logfileid;
 
 /* ================================================== */
 
@@ -217,24 +211,9 @@ static void determine_md5_delay(void);
 void
 NCR_Initialise(void)
 {
-  char *direc;
-
-  if (CNF_GetLogMeasurements()) {
-    direc = CNF_GetLogDir();
-    if (!mkdir_and_parents(direc)) {
-      LOG(LOGS_ERR, LOGF_NtpCore, "Could not create directory %s", direc);
-      logfile = NULL;
-    } else {
-      logfilename = MallocArray(char, 2 + strlen(direc) + strlen(MEASUREMENTS_LOG));
-      strcpy(logfilename, direc);
-      strcat(logfilename, "/");
-      strcat(logfilename, MEASUREMENTS_LOG);
-      logfile = fopen(logfilename, "a");
-      if (!logfile) {
-        LOG(LOGS_WARN, LOGF_NtpCore, "Couldn't open logfile %s for update", logfilename);
-      }
-    }
-  }
+  logfileid = CNF_GetLogMeasurements() ? LOG_FileOpen("measurements",
+      "   Date (UTC) Time     IP Address   L St 1234 ab 5678 LP RP SC  Offset     Peer del. Peer disp. Root del.  Root disp.")
+    : -1;
 
   access_auth_table = ADF_CreateTable();
 
@@ -247,10 +226,6 @@ NCR_Initialise(void)
 void
 NCR_Finalise(void)
 {
-  if (logfile) {
-    fclose(logfile);
-  }
-
   ADF_DestroyTable(access_auth_table);
 
 }
@@ -1260,15 +1235,8 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins
   }
 
   /* Do measurement logging */
-  if (logfile) {
-    if (((logwrites++) % 32) == 0) {
-      fprintf(logfile,
-              "=====================================================================================================================\n"
-              "   Date (UTC) Time     IP Address   L St 1234 ab 5678 LP RP SC  Offset     Peer del. Peer disp. Root del.  Root disp.\n"
-              "=====================================================================================================================\n");
-    }
-
-    fprintf(logfile, "%s %-15s %1c %2d %1d%1d%1d%1d %1d%1d %1d%1d%1d%1d %2d %2d %2d %10.3e %10.3e %10.3e %10.3e %10.3e\n",
+  if (logfileid != -1) {
+    LOG_FileWrite(logfileid, "%s %-15s %1c %2d %1d%1d%1d%1d %1d%1d %1d%1d%1d%1d %2d %2d %2d %10.3e %10.3e %10.3e %10.3e %10.3e",
             UTI_TimeToLogForm(sample_time.tv_sec),
             UTI_IPToString(&inst->remote_addr.ip_addr),
             sync_stats[pkt_leap],
@@ -1280,7 +1248,6 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins
             (inst->score),
             theta, delta, epsilon,
             pkt_root_delay, pkt_root_dispersion);
-    fflush(logfile);
   }            
 
 
@@ -1878,21 +1845,6 @@ NCR_CheckAccessRestriction(IPAddr *ip_addr)
 
 /* ================================================== */
 
-void
-NCR_CycleLogFile(void)
-{
-  if (logfile && logfilename) {
-    fclose(logfile);
-    logfile = fopen(logfilename, "a");
-    if (!logfile) {
-      LOG(LOGS_WARN, LOGF_NtpCore, "Could not reopen logfile %s", logfilename);
-    }
-    logwrites = 0;
-  }
-}
-
-/* ================================================== */
-
 void
 NCR_IncrementActivityCounters(NCR_Instance inst, int *online, int *offline,
                               int *burst_online, int *burst_offline)
index 1a8efcc275bd23ee0571321cecb8f63f2dafaea6..f269a559724ee246209368ea1dbc49f746a03a4e 100644 (file)
@@ -97,8 +97,6 @@ extern void NCR_ReportSource(NCR_Instance inst, RPT_SourceReport *report, struct
 extern int NCR_AddAccessRestriction(IPAddr *ip_addr, int subnet_bits, int allow, int all);
 extern int NCR_CheckAccessRestriction(IPAddr *ip_addr);
 
-extern void NCR_CycleLogFile(void);
-
 extern void NCR_IncrementActivityCounters(NCR_Instance inst, int *online, int *offline, 
                                           int *burst_online, int *burst_offline);
 
index f0770aa45bc8ef447834b04e88721cef006c3a6d..f8f9d62d56ab7069cf450776d1d8d35ddd81edf9 100644 (file)
@@ -35,7 +35,6 @@
 #include "logging.h"
 #include "regress.h"
 #include "sched.h"
-#include "mkdirpp.h"
 
 /* list of refclock drivers */
 extern RefclockDriver RCL_SHM_driver;
@@ -88,10 +87,7 @@ struct RCL_Instance_Record {
 static struct RCL_Instance_Record refclocks[MAX_RCL_SOURCES];
 static int n_sources = 0;
 
-#define REFCLOCKS_LOG "refclocks.log"
-static FILE *logfile = NULL;
-static char *logfilename = NULL;
-static unsigned long logwrites = 0;
+static LOG_FileID logfileid;
 
 static int valid_sample_time(RCL_Instance instance, struct timeval *tv);
 static int pps_stratum(RCL_Instance instance, struct timeval *tv);
@@ -117,17 +113,9 @@ RCL_Initialise(void)
 {
   CNF_AddRefclocks();
 
-  if (CNF_GetLogRefclocks()) {
-    char *logdir = CNF_GetLogDir();
-    if (!mkdir_and_parents(logdir)) {
-      LOG(LOGS_ERR, LOGF_Refclock, "Could not create directory %s", logdir);
-    } else {
-      logfilename = MallocArray(char, 2 + strlen(logdir) + strlen(REFCLOCKS_LOG));
-      strcpy(logfilename, logdir);
-      strcat(logfilename, "/");
-      strcat(logfilename, REFCLOCKS_LOG);
-    }
-  }
+  logfileid = CNF_GetLogRefclocks() ? LOG_FileOpen("refclocks",
+      "   Date (UTC) Time         Refid  DP L P  Raw offset   Cooked offset      Disp.")
+    : -1;
 }
 
 void
@@ -149,10 +137,6 @@ RCL_Finalise(void)
     LCL_RemoveParameterChangeHandler(slew_samples, NULL);
     LCL_RemoveDispersionNotifyHandler(add_dispersion, NULL);
   }
-
-  if (logfile)
-    fclose(logfile);
-  Free(logfilename);
 }
 
 int
@@ -463,16 +447,6 @@ RCL_AddPulse(RCL_Instance instance, struct timeval *pulse_time, double second)
   return 1;
 }
 
-void
-RCL_CycleLogFile(void)
-{
-  if (logfile) {
-    fclose(logfile);
-    logfile = NULL;
-    logwrites = 0;
-  }
-}
-
 static int
 valid_sample_time(RCL_Instance instance, struct timeval *tv)
 {
@@ -589,28 +563,11 @@ log_sample(RCL_Instance instance, struct timeval *sample_time, int filtered, int
 {
   char sync_stats[4] = {'N', '+', '-', '?'};
 
-  if (!logfilename)
+  if (logfileid == -1)
     return;
 
-  if (!logfile) {
-      logfile = fopen(logfilename, "a");
-      if (!logfile) {
-        LOG(LOGS_WARN, LOGF_Refclock, "Couldn't open logfile %s for update", logfilename);
-        Free(logfilename);
-        logfilename = NULL;
-        return;
-      }
-  }
-
-  if (((logwrites++) % 32) == 0) {
-    fprintf(logfile,
-        "===============================================================================\n"
-        "   Date (UTC) Time         Refid  DP L P  Raw offset   Cooked offset      Disp.\n"
-        "===============================================================================\n");
-  }
-
   if (!filtered) {
-    fprintf(logfile, "%s.%06d %-5s %3d %1c %1d %13.6e %13.6e %10.3e\n",
+    LOG_FileWrite(logfileid, "%s.%06d %-5s %3d %1c %1d %13.6e %13.6e %10.3e",
       UTI_TimeToLogForm(sample_time->tv_sec),
       (int)sample_time->tv_usec,
       UTI_RefidToString(instance->ref_id),
@@ -621,7 +578,7 @@ log_sample(RCL_Instance instance, struct timeval *sample_time, int filtered, int
       cooked_offset,
       dispersion);
   } else {
-    fprintf(logfile, "%s.%06d %-5s   - %1c -       -       %13.6e %10.3e\n",
+    LOG_FileWrite(logfileid, "%s.%06d %-5s   - %1c -       -       %13.6e %10.3e",
       UTI_TimeToLogForm(sample_time->tv_sec),
       (int)sample_time->tv_usec,
       UTI_RefidToString(instance->ref_id),
@@ -629,7 +586,6 @@ log_sample(RCL_Instance instance, struct timeval *sample_time, int filtered, int
       cooked_offset,
       dispersion);
   }
-  fflush(logfile);
 }
 
 static void
index 75c37b90b972621a9d156f0b117a35bb145f75d4..1e08a68affc8ea87b3813f787ef7bbd0620f90c3 100644 (file)
@@ -59,7 +59,6 @@ extern int RCL_AddRefclock(RefclockParameters *params);
 extern void RCL_StartRefclocks(void);
 extern void RCL_StartRefclocks(void);
 extern void RCL_ReportSource(RPT_SourceReport *report, struct timeval *now);
-extern void RCL_CycleLogFile(void);
 
 /* functions used by drivers */
 extern void RCL_SetDriverData(RCL_Instance instance, void *data);
index 3e85e35a9f549db2ff2bc334cc99d7538cf51478..dc705d9cff34f2a57d8bde5f4759b3d34c8279db 100644 (file)
@@ -37,7 +37,6 @@
 #include "conf.h"
 #include "logging.h"
 #include "local.h"
-#include "mkdirpp.h"
 
 /* ================================================== */
 
@@ -82,12 +81,8 @@ static void update_drift_file(double, double);
 #define MAIL_PROGRAM "/usr/lib/sendmail"
 
 /* ================================================== */
-/* File to which statistics are logged, NULL if none */
-static FILE *logfile = NULL;
-static char *logfilename = NULL;
-static unsigned long logwrites = 0;
 
-#define TRACKING_LOG "tracking.log"
+static LOG_FileID logfileid;
 
 /* ================================================== */
 
@@ -99,7 +94,6 @@ static unsigned long logwrites = 0;
 void
 REF_Initialise(void)
 {
-  char *direc;
   FILE *in;
   char line[1024];
   double file_freq_ppm, file_skew_ppm;
@@ -144,22 +138,9 @@ REF_Initialise(void)
     
   LCL_SetAbsoluteFrequency(our_frequency_ppm);
 
-  if (CNF_GetLogTracking()) {
-    direc = CNF_GetLogDir();
-    if (!mkdir_and_parents(direc)) {
-      LOG(LOGS_ERR, LOGF_Reference, "Could not create directory %s", direc);
-      logfile = NULL;
-    } else {
-      logfilename = MallocArray(char, 2 + strlen(direc) + strlen(TRACKING_LOG));
-      strcpy(logfilename, direc);
-      strcat(logfilename, "/");
-      strcat(logfilename, TRACKING_LOG);
-      logfile = fopen(logfilename, "a");
-      if (!logfile) {
-        LOG(LOGS_WARN, LOGF_Reference, "Couldn't open logfile %s for update", logfilename);
-      }
-    }
-  }
+  logfileid = CNF_GetLogTracking() ? LOG_FileOpen("tracking",
+      "   Date (UTC) Time     IP Address   St   Freq ppm   Skew ppm     Offset")
+    : -1;
 
   max_update_skew = fabs(CNF_GetMaxUpdateSkew()) * 1.0e-6;
 
@@ -186,10 +167,6 @@ REF_Finalise(void)
     LCL_SetLeap(0);
   }
 
-  if (logfile) {
-    fclose(logfile);
-  }
-
   initialised = 0;
   return;
 }
@@ -377,19 +354,9 @@ update_leap_status(NTP_Leap leap)
 static void
 write_log(struct timeval *ref_time, char *ref, int stratum, double freq, double skew, double offset)
 {
-  if (logfile) {
-
-    if (((logwrites++) % 32) == 0) {
-      fprintf(logfile,
-              "=======================================================================\n"
-              "   Date (UTC) Time     IP Address   St   Freq ppm   Skew ppm     Offset\n"
-              "=======================================================================\n");
-    }
-          
-    fprintf(logfile, "%s %-15s %2d %10.3f %10.3f %10.3e\n",
+  if (logfileid != -1) {
+    LOG_FileWrite(logfileid, "%s %-15s %2d %10.3f %10.3f %10.3e",
             UTI_TimeToLogForm(ref_time->tv_sec), ref, stratum, freq, skew, offset);
-    
-    fflush(logfile);
   }
 }
 
@@ -774,18 +741,3 @@ REF_GetTrackingReport(RPT_TrackingReport *rep)
 }
 
 /* ================================================== */
-
-void
-REF_CycleLogFile(void)
-{
-  if (logfile && logfilename) {
-    fclose(logfile);
-    logfile = fopen(logfilename, "a");
-    if (!logfile) {
-      LOG(LOGS_WARN, LOGF_Reference, "Could not reopen logfile %s", logfilename);
-    }
-    logwrites = 0;
-  }
-}
-
-/* ================================================== */
index 251ee36e1b06a88243066041bc9cc829f9e20c42..3186eb6792a12bf8d5db7a35e4679cb8ecf213aa 100644 (file)
@@ -144,6 +144,4 @@ extern int REF_IsLocalActive(void);
 
 extern void REF_GetTrackingReport(RPT_TrackingReport *rep);
 
-extern void REF_CycleLogFile(void);
-
 #endif /* GOT_REFERENCE_H */
diff --git a/rtc.c b/rtc.c
index 53c2df068b908046db30b4bc2e8416916340c25d..d4ec01e12ae00d6e512d4bffb95a7eb193c58631 100644 (file)
--- a/rtc.c
+++ b/rtc.c
@@ -50,7 +50,6 @@ static struct {
   int  (*write_parameters)(void);
   int  (*get_report)(RPT_RTC_Report *report);
   int  (*trim)(void);
-  void (*cycle_logfile)(void);
 } driver =
 {
 #if defined LINUX && defined FEAT_RTC
@@ -61,8 +60,7 @@ static struct {
   RTC_Linux_StartMeasurements,
   RTC_Linux_WriteParameters,
   RTC_Linux_GetReport,
-  RTC_Linux_Trim,
-  RTC_Linux_CycleLogFile
+  RTC_Linux_Trim
 #else
   NULL,
   NULL,
@@ -71,7 +69,6 @@ static struct {
   NULL,
   NULL,
   NULL,
-  NULL,
   NULL
 #endif 
 };
@@ -210,13 +207,3 @@ RTC_Trim(void)
 
 /* ================================================== */
 
-void
-RTC_CycleLogFile(void)
-{
-  if (driver_initialised) {
-    (driver.cycle_logfile)();
-  }
-}
-
-/* ================================================== */
-
diff --git a/rtc.h b/rtc.h
index db242cf38164ed082864e629330609652a45a9a4..da56a891cfd4abc8fbc13d0f1ef1e048cf1766ef 100644 (file)
--- a/rtc.h
+++ b/rtc.h
@@ -47,6 +47,4 @@ extern int RTC_WriteParameters(void);
 
 extern int RTC_Trim(void);
 
-extern void RTC_CycleLogFile(void);
-
 #endif /* GOT_RTC_H */
index 786e7f8fc1413cdf467285e72a21b77984caa475..3a7cd1ba662fb8c33584d87cd2016e0bc7447e4e 100644 (file)
@@ -61,7 +61,6 @@
 #include "io_linux.h"
 #include "conf.h"
 #include "memory.h"
-#include "mkdirpp.h"
 
 struct rtc_time {
        int tm_sec;
@@ -180,11 +179,7 @@ static int rtc_on_utc = 1;
 
 /* ================================================== */
 
-static FILE *logfile=NULL;
-static char *logfilename = NULL;
-static unsigned long logwrites=0;
-
-#define RTC_LOG "rtc.log"
+static LOG_FileID logfileid;
 
 /* ================================================== */
 
@@ -542,7 +537,6 @@ int
 RTC_Linux_Initialise(void)
 {
   int major, minor, patch;
-  char *direc;
 
   /* Check whether we can support the real time clock.
 
@@ -627,18 +621,9 @@ RTC_Linux_Initialise(void)
   /* Register slew handler */
   LCL_AddParameterChangeHandler(slew_samples, NULL);
 
-  if (CNF_GetLogRtc()) {
-    direc = CNF_GetLogDir();
-    if (!mkdir_and_parents(direc)) {
-      LOG(LOGS_ERR, LOGF_RtcLinux, "Could not create directory %s", direc);
-    } else {
-      logfilename = MallocArray(char, 2 + strlen(direc) + strlen(RTC_LOG));
-      strcpy(logfilename, direc);
-      strcat(logfilename, "/");
-      strcat(logfilename, RTC_LOG);
-    }
-  }
-
+  logfileid = CNF_GetLogRtc() ? LOG_FileOpen("rtc",
+      "   Date (UTC) Time   RTC fast (s) Val   Est fast (s)   Slope (ppm)  Ns  Nr Meas")
+    : -1;
   return 1;
 }
 
@@ -661,11 +646,6 @@ RTC_Linux_Finalise(void)
     (void) RTC_Linux_WriteParameters();
 
   }
-
-  if (logfile) {
-    fclose(logfile);
-  }
-  Free(logfilename);
 }
 
 /* ================================================== */
@@ -837,33 +817,14 @@ process_reading(time_t rtc_time, struct timeval *system_time)
   }  
 
 
-  if (logfilename) {
-    if (!logfile) {
-      logfile = fopen(logfilename, "a");
-      if (!logfile) {
-        LOG(LOGS_WARN, LOGF_RtcLinux, "Couldn't open logfile %s for update", logfilename);
-        Free(logfilename);
-        logfilename = NULL;
-        return;
-      }
-    }
-
+  if (logfileid != -1) {
     rtc_fast = (double)(rtc_time - system_time->tv_sec) - 1.0e-6 * (double) system_time->tv_usec;
 
-    if (((logwrites++) % 32) == 0) {
-      fprintf(logfile,
-              "===============================================================================\n"
-              "   Date (UTC) Time   RTC fast (s) Val   Est fast (s)   Slope (ppm)  Ns  Nr Meas\n"
-              "===============================================================================\n");
-    }
-    
-    fprintf(logfile, "%s %14.6f %1d  %14.6f  %12.3f  %2d  %2d %4d\n",
+    LOG_FileWrite(logfileid, "%s %14.6f %1d  %14.6f  %12.3f  %2d  %2d %4d",
             UTI_TimeToLogForm(system_time->tv_sec),
             rtc_fast,
             coefs_valid,
             coef_seconds_fast, coef_gain_rate * 1.0e6, n_samples, n_runs, measurement_period);
-
-    fflush(logfile);
   }    
 
 }
@@ -892,10 +853,6 @@ read_from_device(void *any)
     switch_interrupts(0); /* Likely to raise error too, but just to be sure... */
     close(fd);
     fd = -1;
-    if (logfile) {
-      fclose(logfile);
-      logfile = NULL;
-    }
     return;
   }    
 
@@ -1180,16 +1137,4 @@ RTC_Linux_Trim(void)
 
 /* ================================================== */
 
-void
-RTC_Linux_CycleLogFile(void)
-{
-  if (logfile) {
-    fclose(logfile);
-    logfile = NULL;
-    logwrites = 0;
-  }
-}
-
-/* ================================================== */
-
 #endif /* defined LINUX */
index 2ecae899c7e2b30f5d01e270a4247ae0b765a3f4..3ce5127d9f5e18e54afed17aed8a28c0a9448c2b 100644 (file)
@@ -38,7 +38,6 @@
 #include "conf.h"
 #include "logging.h"
 #include "local.h"
-#include "mkdirpp.h"
 
 /* ================================================== */
 /* Define the maxumum number of samples that we want
 #define MJD_1970 40587
 
 /* ================================================== */
-/* File to which statistics are logged, NULL if none */
-static FILE *logfile = NULL;
-static char *logfilename = NULL;
-static unsigned long logwrites = 0;
 
-#define STATISTICS_LOG "statistics.log"
+static LOG_FileID logfileid;
 
 /* ================================================== */
 /* This data structure is used to hold the history of data from the
@@ -154,24 +149,9 @@ struct SST_Stats_Record {
 void
 SST_Initialise(void)
 {
-  char *direc;
-
-  if (CNF_GetLogStatistics()) {
-    direc = CNF_GetLogDir();
-    if (!mkdir_and_parents(direc)) {
-      LOG(LOGS_ERR, LOGF_SourceStats, "Could not create directory %s", direc);
-      logfile = NULL;
-    } else {
-      logfilename = MallocArray(char, 2 + strlen(direc) + strlen(STATISTICS_LOG));
-      strcpy(logfilename, direc);
-      strcat(logfilename, "/");
-      strcat(logfilename, STATISTICS_LOG);
-      logfile = fopen(logfilename, "a");
-      if (!logfile) {
-        LOG(LOGS_WARN, LOGF_SourceStats, "Couldn't open logfile %s for update", logfilename);
-      }
-    }
-  }
+  logfileid = CNF_GetLogStatistics() ? LOG_FileOpen("statistics",
+      "   Date (UTC) Time     IP Address    Std dev'n Est offset  Offset sd  Diff freq   Est skew  Stress  Ns  Bs  Nr")
+    : -1;
 }
 
 /* ================================================== */
@@ -179,9 +159,6 @@ SST_Initialise(void)
 void
 SST_Finalise(void)
 {
-  if (logfile) {
-    fclose(logfile);
-  }
 }
 
 /* ================================================== */
@@ -463,17 +440,8 @@ SST_DoNewRegression(SST_Stats inst)
       }
     }
 
-    if (logfile) {
-
-      if (((logwrites++) % 32) == 0) {
-        fprintf(logfile,
-                "==============================================================================================================\n"
-                "   Date (UTC) Time     IP Address    Std dev'n Est offset  Offset sd  Diff freq   Est skew  Stress  Ns  Bs  Nr\n"
-                "==============================================================================================================\n");
-      }
-      
-            
-      fprintf(logfile, "%s %-15s %10.3e %10.3e %10.3e %10.3e %10.3e %7.1e %3d %3d %3d\n",
+    if (logfileid != -1) {
+      LOG_FileWrite(logfileid, "%s %-15s %10.3e %10.3e %10.3e %10.3e %10.3e %7.1e %3d %3d %3d",
               UTI_TimeToLogForm(inst->offset_time.tv_sec),
               inst->ip_addr ? UTI_IPToString(inst->ip_addr) : UTI_RefidToString(inst->refid),
               sqrt(inst->variance),
@@ -484,8 +452,6 @@ SST_DoNewRegression(SST_Stats inst)
               stress,
               inst->n_samples,
               best_start, nruns);
-
-      fflush(logfile);
     }
 
     prune_register(inst, best_start, bad_points);
@@ -928,18 +894,3 @@ SST_DoSourcestatsReport(SST_Stats inst, RPT_SourcestatsReport *report, struct ti
 }
 
 /* ================================================== */
-
-void
-SST_CycleLogFile(void)
-{
-  if (logfile && logfilename) {
-    fclose(logfile);
-    logfile = fopen(logfilename, "a");
-    if (!logfile) {
-      LOG(LOGS_WARN, LOGF_SourceStats, "Could not reopen logfile %s", logfilename);
-    }
-    logwrites = 0;
-  }
-}
-
-/* ================================================== */
index 5e787d8eec1a6b1ead5d82438a778b0beaf81594..c48af7b7e842c48849e359874ee9bd2d74cd5d6b 100644 (file)
@@ -153,7 +153,5 @@ typedef enum {
 
 extern SST_Skew_Direction SST_LastSkewChange(SST_Stats inst);
 
-extern void SST_CycleLogFile(void);
-
 #endif /* GOT_SOURCESTATS_H */