X-Git-Url: https://the.earth.li/gitweb/?p=onak.git;a=blobdiff_plain;f=log.c;h=45ec17e01bfef44420a52834034043594bfa5982;hp=5d067bc7f606f71ea119ec4b1f854b0c88f42fdd;hb=3da81770b841f841c5145f91a9ccedc296e13f4b;hpb=3b5b9db0bc2dbe93b3b79e722997606c71ecafb9 diff --git a/log.c b/log.c index 5d067bc..45ec17e 100644 --- a/log.c +++ b/log.c @@ -1,9 +1,19 @@ /* * log.c - Simple logging framework. * - * Jonathan McDowell + * Copyright 2003 Jonathan McDowell * - * Copyright 2003 Project Purple + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . */ #include @@ -22,7 +32,7 @@ * logs - if we're asked to log something below this level we won't output * it. */ -static loglevels logthres = LOGTHING_DEBUG; +static loglevels logthres = LOGTHING_NOTICE; /* * logappname - the name of the application using us. @@ -100,6 +110,68 @@ loglevels setlogthreshold(loglevels loglevel) return oldlevel; } +/* + * getlogthreshold - get the threshold for log output + * + * Returns the threshold for log output; anything logged with a log level + * lower than this will be silently dropped. + */ +loglevels getlogthreshold(void) +{ + return logthres; +} + +/* + * 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 +186,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, - 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;