From: Jonathan McDowell Date: Mon, 31 May 2004 23:47:37 +0000 (+0000) Subject: cscvs to tla changeset 72 X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/501b09dd94d46430a2463ac5fba7608b0be59005?hp=1aa3569ed8afe434ef0ae75799e962fe7f58b6d5 cscvs to tla changeset 72 Author: noodles Date: 2003/04/20 22:42:55 Fall back to stderr if we can't open the logfile. --- diff --git a/log.c b/log.c index 3bc8017..1c9f139 100644 --- a/log.c +++ b/log.c @@ -100,6 +100,57 @@ loglevels setlogthreshold(loglevels loglevel) return oldlevel; } +/* + * vflog - write a log entry to an already opened log file. + * @logfile: The FILE * handle of the open log file. + * @format: A format string. + * @ap: The va_list of the parmeters for the format string. + * + * This function outputs a log entry to an opened file. A leading + * time/date stamp and a trailing newline are automatically added. The + * format parameter is of the same nature as that used in vprintf. + */ +static void vflog(FILE *logfile, const char *format, va_list ap) +{ + struct tm *timestamp = NULL; + time_t timer = 0; + + timer = time(NULL); + timestamp = localtime(&timer); + + fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ", + timestamp->tm_mday, + timestamp->tm_mon + 1, + timestamp->tm_year + 1900, + timestamp->tm_hour, + timestamp->tm_min, + timestamp->tm_sec, + (logappname == NULL) ? "" : logappname, + getpid()); + vfprintf(logfile, format, ap); + fprintf(logfile, "\n"); + + return; +} + +/* + * flog - write a log entry to an already opened log file. + * @logfile: The FILE * handle of the open log file. + * @format: A format string. + * + * This function outputs a log entry to an opened file. A leading + * time/date stamp and a trailing newline are automatically added. The + * format parameter is of the same nature as that used in printf. + */ +static void flog(FILE *logfile, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vflog(logfile, format, ap); + va_end(ap); +} + /* * logthing - output a log entry * @loglevel: The level of the log. @@ -114,37 +165,27 @@ loglevels setlogthreshold(loglevels loglevel) int logthing(loglevels loglevel, const char *format, ...) { FILE *logfile = NULL; - struct tm *timestamp = NULL; - time_t timer = 0; va_list ap; if (loglevel >= logthres) { - timer = time(NULL); - timestamp = localtime(&timer); - if (logfilename != NULL) { logfile = fopen(logfilename, "a"); - flockfile(logfile); + if (logfile != NULL) { + flockfile(logfile); + } else { + logfile = stderr; + flog(logfile, "Couldn't open logfile: %s", + logfilename); + } } else { logfile = stderr; } - fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ", - timestamp->tm_mday, - timestamp->tm_mon + 1, - timestamp->tm_year + 1900, - timestamp->tm_hour, - timestamp->tm_min, - timestamp->tm_sec, - (logappname == NULL) ? "" : logappname, - getpid()); va_start(ap, format); - vfprintf(logfile, format, ap); + vflog(logfile, format, ap); va_end(ap); - fprintf(logfile, "\n"); - - if (logfilename != NULL) { + if (logfile != stderr) { funlockfile(logfile); fclose(logfile); logfile = NULL;