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, see <https://www.gnu.org/licenses/>.
29 * logthres - holds the minimum log level we'll output
31 * This variable keeps track of the threshold we've set for outputting
32 * logs - if we're asked to log something below this level we won't output
35 static loglevels logthres = LOGTHING_NOTICE;
38 * logappname - the name of the application using us.
40 * This holds information about the name of the application we're being
41 * called by. It's set when we're initialized.
43 static char *logappname = NULL;
46 * logfilename - the file to log to.
48 * The full name and path of the file we should log to.
50 static char *logfilename = NULL;
53 * initlogthing - initialize the logging module
54 * @appname: The application name to use in the log.
55 * @filename: The filename to log to. NULL means stderr.
57 * This function sets up the logging module ready to log. The appname is
58 * written as part of every log entry and the filename is the file we
59 * should log to. If the appname is NULL then none is written. If the
60 * filename is NULL all output is sent to stderr.
62 int initlogthing(const char *appname, const char *filename)
64 if (appname != NULL) {
65 logappname = strdup(appname);
68 if (filename != NULL) {
69 logfilename = strdup(filename);
76 * cleanuplogthing - clean up the logging module
78 * This function cleans up the logging module after use.
80 void cleanuplogthing(void)
82 if (logappname != NULL) {
87 if (logfilename != NULL) {
96 * setlogthreshold - set the threshold for log output
97 * @loglevel: The minimum log level we should output
99 * Sets the threshold for log output; anything logged with a log level
100 * lower than this will be silently dropped. Returns the old log threshold
103 loglevels setlogthreshold(loglevels loglevel)
114 * getlogthreshold - get the threshold for log output
116 * Returns the threshold for log output; anything logged with a log level
117 * lower than this will be silently dropped.
119 loglevels getlogthreshold(void)
125 * vflog - write a log entry to an already opened log file.
126 * @logfile: The FILE * handle of the open log file.
127 * @format: A format string.
128 * @ap: The va_list of the parmeters for the format string.
130 * This function outputs a log entry to an opened file. A leading
131 * time/date stamp and a trailing newline are automatically added. The
132 * format parameter is of the same nature as that used in vprintf.
134 static void vflog(FILE *logfile, const char *format, va_list ap)
140 localtime_r(&timer, ×tamp);
142 fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
144 timestamp.tm_mon + 1,
145 timestamp.tm_year + 1900,
149 (logappname == NULL) ? "" : logappname,
151 vfprintf(logfile, format, ap);
152 fprintf(logfile, "\n");
158 * flog - write a log entry to an already opened log file.
159 * @logfile: The FILE * handle of the open log file.
160 * @format: A format string.
162 * This function outputs a log entry to an opened file. A leading
163 * time/date stamp and a trailing newline are automatically added. The
164 * format parameter is of the same nature as that used in printf.
166 static void flog(FILE *logfile, const char *format, ...)
170 va_start(ap, format);
171 vflog(logfile, format, ap);
176 * logthing - output a log entry
177 * @loglevel: The level of the log.
178 * @format: A format string, followed by any parameters required.
180 * This function outputs a log entry. A leading time/date stamp and a
181 * trailing newline are automatically added. The loglevel is compared to
182 * the current log threshold and if equal or above the log entry is
183 * output. The format parameter is of the same nature as that used in
186 int logthing(loglevels loglevel, const char *format, ...)
188 FILE *logfile = NULL;
191 if (loglevel >= logthres) {
192 if (logfilename != NULL) {
193 logfile = fopen(logfilename, "a");
194 if (logfile != NULL) {
198 flog(logfile, "Couldn't open logfile: %s",
205 va_start(ap, format);
206 vflog(logfile, format, ap);
209 if (logfile != stderr) {
210 funlockfile(logfile);