X-Git-Url: https://the.earth.li/gitweb/?a=blobdiff_plain;f=log.c;fp=log.c;h=5d067bc7f606f71ea119ec4b1f854b0c88f42fdd;hb=3b5b9db0bc2dbe93b3b79e722997606c71ecafb9;hp=0000000000000000000000000000000000000000;hpb=62c94dacbe471278972813b59776a5fadbd8a543;p=onak.git diff --git a/log.c b/log.c new file mode 100644 index 0000000..5d067bc --- /dev/null +++ b/log.c @@ -0,0 +1,155 @@ +/* + * log.c - Simple logging framework. + * + * Jonathan McDowell + * + * Copyright 2003 Project Purple + */ + +#include +#include +#include +#include +#include +#include + +#include "log.h" + +/* + * logthres - holds the minimum log level we'll output + * + * This variable keeps track of the threshold we've set for outputting + * logs - if we're asked to log something below this level we won't output + * it. + */ +static loglevels logthres = LOGTHING_DEBUG; + +/* + * logappname - the name of the application using us. + * + * This holds information about the name of the application we're being + * called by. It's set when we're initialized. + */ +static char *logappname = NULL; + +/* + * logfilename - the file to log to. + * + * The full name and path of the file we should log to. + */ +static char *logfilename = NULL; + +/* + * initlogthing - initialize the logging module + * @appname: The application name to use in the log. + * @filename: The filename to log to. NULL means stderr. + * + * This function sets up the logging module ready to log. The appname is + * written as part of every log entry and the filename is the file we + * should log to. If the appname is NULL then none is written. If the + * filename is NULL all output is sent to stderr. + */ +int initlogthing(const char *appname, const char *filename) +{ + if (appname != NULL) { + logappname = strdup(appname); + } + + if (filename != NULL) { + logfilename = strdup(filename); + } + + return 0; +} + +/* + * cleanuplogthing - clean up the logging module + * + * This function cleans up the logging module after use. + */ +void cleanuplogthing(void) +{ + if (logappname != NULL) { + free(logappname); + logappname = NULL; + } + + if (logfilename != NULL) { + free(logfilename); + logfilename = NULL; + } + + return; +} + +/* + * setlogthreshold - set the threshold for log output + * @loglevel: The minimum log level we should output + * + * Sets the threshold for log output; anything logged with a log level + * lower than this will be silently dropped. Returns the old log threshold + * value. + */ +loglevels setlogthreshold(loglevels loglevel) +{ + loglevels oldlevel; + + oldlevel = logthres; + logthres = loglevel; + + return oldlevel; +} + +/* + * logthing - output a log entry + * @loglevel: The level of the log. + * @format: A format string, followed by any parameters required. + * + * This function outputs a log entry. A leading time/date stamp and a + * trailing newline are automatically added. The loglevel is compared to + * the current log threshold and if equal or above the log entry is + * output. The format parameter is of the same nature as that used in + * printf. + */ +int logthing(loglevels loglevel, const char *format, ...) +{ + FILE *logfile = NULL; + struct tm *timestamp = NULL; + time_t timer = 0; + va_list ap; + + if (loglevel >= logthres) { + timer = time(NULL); + timestamp = localtime(&timer); + + if (logfilename != NULL) { + logfile = fopen(logfilename, "a"); + flockfile(logfile); + } else { + logfile = stderr; + } + + fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ", + timestamp->tm_mday, + timestamp->tm_mon, + timestamp->tm_year + 1900, + timestamp->tm_hour, + timestamp->tm_min, + timestamp->tm_sec, + (logappname == NULL) ? "" : logappname, + getpid()); + va_start(ap, format); + vfprintf(logfile, format, ap); + va_end(ap); + fprintf(logfile, "\n"); + + + if (logfilename != NULL) { + funlockfile(logfile); + fclose(logfile); + logfile = NULL; + } + } + + return 0; +}