2 * log.c - Simple logging framework.
4 * Copyright 2003 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * logthres - holds the minimum log level we'll output
32 * This variable keeps track of the threshold we've set for outputting
33 * logs - if we're asked to log something below this level we won't output
36 static loglevels logthres = LOGTHING_NOTICE;
39 * logappname - the name of the application using us.
41 * This holds information about the name of the application we're being
42 * called by. It's set when we're initialized.
44 static char *logappname = NULL;
47 * logfilename - the file to log to.
49 * The full name and path of the file we should log to.
51 static char *logfilename = NULL;
54 * initlogthing - initialize the logging module
55 * @appname: The application name to use in the log.
56 * @filename: The filename to log to. NULL means stderr.
58 * This function sets up the logging module ready to log. The appname is
59 * written as part of every log entry and the filename is the file we
60 * should log to. If the appname is NULL then none is written. If the
61 * filename is NULL all output is sent to stderr.
63 int initlogthing(const char *appname, const char *filename)
65 if (appname != NULL) {
66 logappname = strdup(appname);
69 if (filename != NULL) {
70 logfilename = strdup(filename);
77 * cleanuplogthing - clean up the logging module
79 * This function cleans up the logging module after use.
81 void cleanuplogthing(void)
83 if (logappname != NULL) {
88 if (logfilename != NULL) {
97 * setlogthreshold - set the threshold for log output
98 * @loglevel: The minimum log level we should output
100 * Sets the threshold for log output; anything logged with a log level
101 * lower than this will be silently dropped. Returns the old log threshold
104 loglevels setlogthreshold(loglevels loglevel)
115 * vflog - write a log entry to an already opened log file.
116 * @logfile: The FILE * handle of the open log file.
117 * @format: A format string.
118 * @ap: The va_list of the parmeters for the format string.
120 * This function outputs a log entry to an opened file. A leading
121 * time/date stamp and a trailing newline are automatically added. The
122 * format parameter is of the same nature as that used in vprintf.
124 static void vflog(FILE *logfile, const char *format, va_list ap)
126 struct tm *timestamp = NULL;
130 timestamp = localtime(&timer);
132 fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
134 timestamp->tm_mon + 1,
135 timestamp->tm_year + 1900,
139 (logappname == NULL) ? "" : logappname,
141 vfprintf(logfile, format, ap);
142 fprintf(logfile, "\n");
148 * flog - write a log entry to an already opened log file.
149 * @logfile: The FILE * handle of the open log file.
150 * @format: A format string.
152 * This function outputs a log entry to an opened file. A leading
153 * time/date stamp and a trailing newline are automatically added. The
154 * format parameter is of the same nature as that used in printf.
156 static void flog(FILE *logfile, const char *format, ...)
160 va_start(ap, format);
161 vflog(logfile, format, ap);
166 * logthing - output a log entry
167 * @loglevel: The level of the log.
168 * @format: A format string, followed by any parameters required.
170 * This function outputs a log entry. A leading time/date stamp and a
171 * trailing newline are automatically added. The loglevel is compared to
172 * the current log threshold and if equal or above the log entry is
173 * output. The format parameter is of the same nature as that used in
176 int logthing(loglevels loglevel, const char *format, ...)
178 FILE *logfile = NULL;
181 if (loglevel >= logthres) {
182 if (logfilename != NULL) {
183 logfile = fopen(logfilename, "a");
184 if (logfile != NULL) {
188 flog(logfile, "Couldn't open logfile: %s",
195 va_start(ap, format);
196 vflog(logfile, format, ap);
199 if (logfile != stderr) {
200 funlockfile(logfile);