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;
#include "sysincl.h"
#include "main.h"
+#include "conf.h"
#include "logging.h"
#include "version.h"
+#include "mkdirpp.h"
/* ================================================== */
/* Flag indicating we have initialised */
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 */
}
#endif
+ LOG_CycleLogFiles();
+
initialised = 0;
return;
}
}
/* ================================================== */
+
+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;
+ }
+}
+
+/* ================================================== */
#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 */
ACQ_Initialise();
MNL_Initialise();
+ LOG_CreateLogFileDir();
+
/* From now on, it is safe to do finalisation on exit */
initialised = 1;
#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;
/* ================================================== */
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();
void
NCR_Finalise(void)
{
- if (logfile) {
- fclose(logfile);
- }
-
ADF_DestroyTable(access_auth_table);
}
}
/* 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],
(inst->score),
theta, delta, epsilon,
pkt_root_delay, pkt_root_dispersion);
- fflush(logfile);
}
/* ================================================== */
-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)
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);
#include "logging.h"
#include "regress.h"
#include "sched.h"
-#include "mkdirpp.h"
/* list of refclock drivers */
extern RefclockDriver RCL_SHM_driver;
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);
{
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
LCL_RemoveParameterChangeHandler(slew_samples, NULL);
LCL_RemoveDispersionNotifyHandler(add_dispersion, NULL);
}
-
- if (logfile)
- fclose(logfile);
- Free(logfilename);
}
int
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)
{
{
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),
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),
cooked_offset,
dispersion);
}
- fflush(logfile);
}
static void
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);
#include "conf.h"
#include "logging.h"
#include "local.h"
-#include "mkdirpp.h"
/* ================================================== */
#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;
/* ================================================== */
void
REF_Initialise(void)
{
- char *direc;
FILE *in;
char line[1024];
double file_freq_ppm, file_skew_ppm;
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;
LCL_SetLeap(0);
}
- if (logfile) {
- fclose(logfile);
- }
-
initialised = 0;
return;
}
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);
}
}
}
/* ================================================== */
-
-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;
- }
-}
-
-/* ================================================== */
extern void REF_GetTrackingReport(RPT_TrackingReport *rep);
-extern void REF_CycleLogFile(void);
-
#endif /* GOT_REFERENCE_H */
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
RTC_Linux_StartMeasurements,
RTC_Linux_WriteParameters,
RTC_Linux_GetReport,
- RTC_Linux_Trim,
- RTC_Linux_CycleLogFile
+ RTC_Linux_Trim
#else
NULL,
NULL,
NULL,
NULL,
NULL,
- NULL,
NULL
#endif
};
/* ================================================== */
-void
-RTC_CycleLogFile(void)
-{
- if (driver_initialised) {
- (driver.cycle_logfile)();
- }
-}
-
-/* ================================================== */
-
extern int RTC_Trim(void);
-extern void RTC_CycleLogFile(void);
-
#endif /* GOT_RTC_H */
#include "io_linux.h"
#include "conf.h"
#include "memory.h"
-#include "mkdirpp.h"
struct rtc_time {
int tm_sec;
/* ================================================== */
-static FILE *logfile=NULL;
-static char *logfilename = NULL;
-static unsigned long logwrites=0;
-
-#define RTC_LOG "rtc.log"
+static LOG_FileID logfileid;
/* ================================================== */
RTC_Linux_Initialise(void)
{
int major, minor, patch;
- char *direc;
/* Check whether we can support the real time clock.
/* 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;
}
(void) RTC_Linux_WriteParameters();
}
-
- if (logfile) {
- fclose(logfile);
- }
- Free(logfilename);
}
/* ================================================== */
}
- 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);
}
}
switch_interrupts(0); /* Likely to raise error too, but just to be sure... */
close(fd);
fd = -1;
- if (logfile) {
- fclose(logfile);
- logfile = NULL;
- }
return;
}
/* ================================================== */
-void
-RTC_Linux_CycleLogFile(void)
-{
- if (logfile) {
- fclose(logfile);
- logfile = NULL;
- logwrites = 0;
- }
-}
-
-/* ================================================== */
-
#endif /* defined LINUX */
#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
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;
}
/* ================================================== */
void
SST_Finalise(void)
{
- if (logfile) {
- fclose(logfile);
- }
}
/* ================================================== */
}
}
- 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),
stress,
inst->n_samples,
best_start, nruns);
-
- fflush(logfile);
}
prune_register(inst, best_start, bad_points);
}
/* ================================================== */
-
-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;
- }
-}
-
-/* ================================================== */
extern SST_Skew_Direction SST_LastSkewChange(SST_Stats inst);
-extern void SST_CycleLogFile(void);
-
#endif /* GOT_SOURCESTATS_H */