2 * log.c - Simple logging framework.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2003 Project Purple
19 * logthres - holds the minimum log level we'll output
21 * This variable keeps track of the threshold we've set for outputting
22 * logs - if we're asked to log something below this level we won't output
25 static loglevels logthres = LOGTHING_DEBUG;
28 * logappname - the name of the application using us.
30 * This holds information about the name of the application we're being
31 * called by. It's set when we're initialized.
33 static char *logappname = NULL;
36 * logfilename - the file to log to.
38 * The full name and path of the file we should log to.
40 static char *logfilename = NULL;
43 * initlogthing - initialize the logging module
44 * @appname: The application name to use in the log.
45 * @filename: The filename to log to. NULL means stderr.
47 * This function sets up the logging module ready to log. The appname is
48 * written as part of every log entry and the filename is the file we
49 * should log to. If the appname is NULL then none is written. If the
50 * filename is NULL all output is sent to stderr.
52 int initlogthing(const char *appname, const char *filename)
54 if (appname != NULL) {
55 logappname = strdup(appname);
58 if (filename != NULL) {
59 logfilename = strdup(filename);
66 * cleanuplogthing - clean up the logging module
68 * This function cleans up the logging module after use.
70 void cleanuplogthing(void)
72 if (logappname != NULL) {
77 if (logfilename != NULL) {
86 * setlogthreshold - set the threshold for log output
87 * @loglevel: The minimum log level we should output
89 * Sets the threshold for log output; anything logged with a log level
90 * lower than this will be silently dropped. Returns the old log threshold
93 loglevels setlogthreshold(loglevels loglevel)
104 * vflog - write a log entry to an already opened log file.
105 * @logfile: The FILE * handle of the open log file.
106 * @format: A format string.
107 * @ap: The va_list of the parmeters for the format string.
109 * This function outputs a log entry to an opened file. A leading
110 * time/date stamp and a trailing newline are automatically added. The
111 * format parameter is of the same nature as that used in vprintf.
113 static void vflog(FILE *logfile, const char *format, va_list ap)
115 struct tm *timestamp = NULL;
119 timestamp = localtime(&timer);
121 fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
123 timestamp->tm_mon + 1,
124 timestamp->tm_year + 1900,
128 (logappname == NULL) ? "" : logappname,
130 vfprintf(logfile, format, ap);
131 fprintf(logfile, "\n");
137 * flog - write a log entry to an already opened log file.
138 * @logfile: The FILE * handle of the open log file.
139 * @format: A format string.
141 * This function outputs a log entry to an opened file. A leading
142 * time/date stamp and a trailing newline are automatically added. The
143 * format parameter is of the same nature as that used in printf.
145 static void flog(FILE *logfile, const char *format, ...)
149 va_start(ap, format);
150 vflog(logfile, format, ap);
155 * logthing - output a log entry
156 * @loglevel: The level of the log.
157 * @format: A format string, followed by any parameters required.
159 * This function outputs a log entry. A leading time/date stamp and a
160 * trailing newline are automatically added. The loglevel is compared to
161 * the current log threshold and if equal or above the log entry is
162 * output. The format parameter is of the same nature as that used in
165 int logthing(loglevels loglevel, const char *format, ...)
167 FILE *logfile = NULL;
170 if (loglevel >= logthres) {
171 if (logfilename != NULL) {
172 logfile = fopen(logfilename, "a");
173 if (logfile != NULL) {
177 flog(logfile, "Couldn't open logfile: %s",
184 va_start(ap, format);
185 vflog(logfile, format, ap);
188 if (logfile != stderr) {
189 funlockfile(logfile);