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 * getlogthreshold - get the threshold for log output
117 * Returns the threshold for log output; anything logged with a log level
118 * lower than this will be silently dropped.
120 loglevels getlogthreshold(void)
126 * vflog - write a log entry to an already opened log file.
127 * @logfile: The FILE * handle of the open log file.
128 * @format: A format string.
129 * @ap: The va_list of the parmeters for the format string.
131 * This function outputs a log entry to an opened file. A leading
132 * time/date stamp and a trailing newline are automatically added. The
133 * format parameter is of the same nature as that used in vprintf.
135 static void vflog(FILE *logfile, const char *format, va_list ap)
137 struct tm *timestamp = NULL;
141 timestamp = localtime(&timer);
143 fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
145 timestamp->tm_mon + 1,
146 timestamp->tm_year + 1900,
150 (logappname == NULL) ? "" : logappname,
152 vfprintf(logfile, format, ap);
153 fprintf(logfile, "\n");
159 * flog - write a log entry to an already opened log file.
160 * @logfile: The FILE * handle of the open log file.
161 * @format: A format string.
163 * This function outputs a log entry to an opened file. A leading
164 * time/date stamp and a trailing newline are automatically added. The
165 * format parameter is of the same nature as that used in printf.
167 static void flog(FILE *logfile, const char *format, ...)
171 va_start(ap, format);
172 vflog(logfile, format, ap);
177 * logthing - output a log entry
178 * @loglevel: The level of the log.
179 * @format: A format string, followed by any parameters required.
181 * This function outputs a log entry. A leading time/date stamp and a
182 * trailing newline are automatically added. The loglevel is compared to
183 * the current log threshold and if equal or above the log entry is
184 * output. The format parameter is of the same nature as that used in
187 int logthing(loglevels loglevel, const char *format, ...)
189 FILE *logfile = NULL;
192 if (loglevel >= logthres) {
193 if (logfilename != NULL) {
194 logfile = fopen(logfilename, "a");
195 if (logfile != NULL) {
199 flog(logfile, "Couldn't open logfile: %s",
206 va_start(ap, format);
207 vflog(logfile, format, ap);
210 if (logfile != stderr) {
211 funlockfile(logfile);