From: Matt Signorini Date: Thu, 27 Dec 2012 05:00:44 +0000 (+1100) Subject: Added code to facilitate logging to a separate file. X-Git-Tag: ver3_1_1~6^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d3ca5f313cb0103a7a0cac82edd1b9f912108c5c;p=fcron Added code to facilitate logging to a separate file. - modified fcronconf.c and .h to parse an fcronlog entry in the conf file. - modified xopenlog and xcloselog to handle a file, if specified, and added a procedure fcronlog, which prints a message to the log file. --- diff --git a/fcronconf.c b/fcronconf.c index cbfeb19..9250b01 100644 --- a/fcronconf.c +++ b/fcronconf.c @@ -40,6 +40,7 @@ char *pidfile = NULL; char *fifofile = NULL; char *fcronallow = NULL; char *fcrondeny = NULL; +char *fcronlog = NULL; char *shell = NULL; char *sendmail = NULL; char *editor = NULL; @@ -160,6 +161,7 @@ read_conf(void) else if ( strncmp(ptr1, "fifofile", namesize) == 0 ) { Set(fifofile , ptr2); } else if ( strncmp(ptr1, "fcronallow", namesize) == 0 ) { Set(fcronallow , ptr2); } else if ( strncmp(ptr1, "fcrondeny", namesize) == 0 ) { Set(fcrondeny , ptr2); } + else if ( strncmp(ptr1, "fcronlog", namesize) == 0 ) { Set(fcronlog, ptr2); } else if ( strncmp(ptr1, "shell", namesize) == 0 ) { Set(shell , ptr2); } else if ( strncmp(ptr1, "sendmail", namesize) == 0 ) { Set(sendmail , ptr2); } else if ( strncmp(ptr1, "editor", namesize) == 0 ) { Set(editor , ptr2); } diff --git a/fcronconf.h b/fcronconf.h index d211419..f1f19d7 100644 --- a/fcronconf.h +++ b/fcronconf.h @@ -33,6 +33,7 @@ extern char *fcronconf; extern char *fcronallow; extern char *fcrondeny; extern char *fcrontabs; +extern char *fcronlog; extern char *pidfile; extern char *fifofile; extern char *editor; diff --git a/log.c b/log.c index d5a8d2e..f27b672 100644 --- a/log.c +++ b/log.c @@ -47,6 +47,7 @@ void log_console_str(char *msg); void log_fd_str(int fd, char *msg); static void log_syslog(int priority, int fd, char *fmt, va_list args); static void log_e(int priority, char *fmt, va_list args); +static void fcronlog(int priority, char *msg); #ifdef HAVE_LIBPAM static void log_pame(int priority, pam_handle_t *pamh, int pamerrno, char *fmt, va_list args); @@ -54,22 +55,42 @@ static void log_pame(int priority, pam_handle_t *pamh, int pamerrno, static char truncated[] = " (truncated)"; static int log_open = 0; +static FILE *logfd = NULL; +/* Initialise logging to either syslog or a file specified in fcron.conf, + * or take no action if logging is suppressed. + */ static void xopenlog(void) { - if (!log_open) { + if (log_open) + return; + + // are we using syslog? + if (dosyslog) { openlog(prog_name, LOG_PID, SYSLOG_FACILITY); - log_open=1; + } else if (fcronlog != NULL) { + logfd = fopen(fcronlog, "a+"); } + + log_open = 1; } void xcloselog() { - if (log_open) closelog(); + if (!log_open) + return; + + // check whether we need to close syslog, or a file. + if (dosyslog) { + closelog(); + } else if (fcronlog != NULL) { + fclose(logfd); + } + log_open = 0; } @@ -99,13 +120,16 @@ make_msg(const char *append, char *fmt, va_list args) } -/* log a simple string to syslog if needed */ +/* log a simple string to syslog/log file if needed */ void log_syslog_str(int priority, char *msg) { + xopenlog(); + if (dosyslog) { - xopenlog(); syslog(priority, "%s", msg); + } else if ( logfd != NULL ) { + fcronlog(priority, msg); } } @@ -174,6 +198,39 @@ log_e(int priority, char *fmt, va_list args) Free_safe(msg); } +/* write a message to the file specified by logfd. */ +static void +fcronlog(int priority, char *msg) +{ + time_t t = time(NULL); + struct tm *ft; + char date[30]; + char type[30] + + // print the current time as a string. + ft = localtime(&t); + date[0] = '\0'; + strftime(date, sizeof(date), "%H:%M:%S", ft); + + // is it an info/warning/error/debug message? + switch(priority) { + case EXPLAIN_LEVEL: + type = "Information"; + break; + case WARNING_LEVEL: + type = "Warning"; + break; + case COMPLAIN_LEVEL: + type = "Error"; + break; + case DEBUG_LEVEL: + type = "Debug"; + } + + // print the log message. + fprintf(logfd, "%s [ %s ]:\n%s\n\n", date, type, msg); +} + #ifdef HAVE_LIBPAM /* Same as log_syslog(), but also appends an error description corresponding