2 * log.c - Simple logging framework.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2003 Project Purple
8 * $Id: log.c,v 1.5 2004/03/23 12:33:47 noodles Exp $
21 * logthres - holds the minimum log level we'll output
23 * This variable keeps track of the threshold we've set for outputting
24 * logs - if we're asked to log something below this level we won't output
27 static loglevels logthres = LOGTHING_NOTICE;
30 * logappname - the name of the application using us.
32 * This holds information about the name of the application we're being
33 * called by. It's set when we're initialized.
35 static char *logappname = NULL;
38 * logfilename - the file to log to.
40 * The full name and path of the file we should log to.
42 static char *logfilename = NULL;
45 * initlogthing - initialize the logging module
46 * @appname: The application name to use in the log.
47 * @filename: The filename to log to. NULL means stderr.
49 * This function sets up the logging module ready to log. The appname is
50 * written as part of every log entry and the filename is the file we
51 * should log to. If the appname is NULL then none is written. If the
52 * filename is NULL all output is sent to stderr.
54 int initlogthing(const char *appname, const char *filename)
56 if (appname != NULL) {
57 logappname = strdup(appname);
60 if (filename != NULL) {
61 logfilename = strdup(filename);
68 * cleanuplogthing - clean up the logging module
70 * This function cleans up the logging module after use.
72 void cleanuplogthing(void)
74 if (logappname != NULL) {
79 if (logfilename != NULL) {
88 * setlogthreshold - set the threshold for log output
89 * @loglevel: The minimum log level we should output
91 * Sets the threshold for log output; anything logged with a log level
92 * lower than this will be silently dropped. Returns the old log threshold
95 loglevels setlogthreshold(loglevels loglevel)
106 * vflog - write a log entry to an already opened log file.
107 * @logfile: The FILE * handle of the open log file.
108 * @format: A format string.
109 * @ap: The va_list of the parmeters for the format string.
111 * This function outputs a log entry to an opened file. A leading
112 * time/date stamp and a trailing newline are automatically added. The
113 * format parameter is of the same nature as that used in vprintf.
115 static void vflog(FILE *logfile, const char *format, va_list ap)
117 struct tm *timestamp = NULL;
121 timestamp = localtime(&timer);
123 fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
125 timestamp->tm_mon + 1,
126 timestamp->tm_year + 1900,
130 (logappname == NULL) ? "" : logappname,
132 vfprintf(logfile, format, ap);
133 fprintf(logfile, "\n");
139 * flog - write a log entry to an already opened log file.
140 * @logfile: The FILE * handle of the open log file.
141 * @format: A format string.
143 * This function outputs a log entry to an opened file. A leading
144 * time/date stamp and a trailing newline are automatically added. The
145 * format parameter is of the same nature as that used in printf.
147 static void flog(FILE *logfile, const char *format, ...)
151 va_start(ap, format);
152 vflog(logfile, format, ap);
157 * logthing - output a log entry
158 * @loglevel: The level of the log.
159 * @format: A format string, followed by any parameters required.
161 * This function outputs a log entry. A leading time/date stamp and a
162 * trailing newline are automatically added. The loglevel is compared to
163 * the current log threshold and if equal or above the log entry is
164 * output. The format parameter is of the same nature as that used in
167 int logthing(loglevels loglevel, const char *format, ...)
169 FILE *logfile = NULL;
172 if (loglevel >= logthres) {
173 if (logfilename != NULL) {
174 logfile = fopen(logfilename, "a");
175 if (logfile != NULL) {
179 flog(logfile, "Couldn't open logfile: %s",
186 va_start(ap, format);
187 vflog(logfile, format, ap);
190 if (logfile != stderr) {
191 funlockfile(logfile);