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 * logthing - output a log entry
105 * @loglevel: The level of the log.
106 * @format: A format string, followed by any parameters required.
108 * This function outputs a log entry. A leading time/date stamp and a
109 * trailing newline are automatically added. The loglevel is compared to
110 * the current log threshold and if equal or above the log entry is
111 * output. The format parameter is of the same nature as that used in
114 int logthing(loglevels loglevel, const char *format, ...)
116 FILE *logfile = NULL;
117 struct tm *timestamp = NULL;
121 if (loglevel >= logthres) {
123 timestamp = localtime(&timer);
125 if (logfilename != NULL) {
126 logfile = fopen(logfilename, "a");
132 fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
135 timestamp->tm_year + 1900,
139 (logappname == NULL) ? "" : logappname,
141 va_start(ap, format);
142 vfprintf(logfile, format, ap);
144 fprintf(logfile, "\n");
147 if (logfilename != NULL) {
148 funlockfile(logfile);