From: Michael Stapelberg Date: Sat, 19 Dec 2009 21:37:15 +0000 (+0100) Subject: add log.c/log.h which contain all the log related macros and functions X-Git-Tag: 3.e~6^2~199 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=4226cc61de6d02dfc6c4ce34864b225fa89cbcc2;p=i3%2Fi3 add log.c/log.h which contain all the log related macros and functions --- diff --git a/include/log.h b/include/log.h new file mode 100644 index 00000000..def99fc1 --- /dev/null +++ b/include/log.h @@ -0,0 +1,65 @@ +/* + * vim:ts=8:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + */ +#ifndef _LOG_H +#define _LOG_H + +#include + +/** ##__VA_ARGS__ means: leave out __VA_ARGS__ completely if it is empty, that + is, delete the preceding comma */ +#define LOG(fmt, ...) verboselog(fmt, ##__VA_ARGS__) +#define ELOG(fmt, ...) errorlog("ERROR: " fmt, ##__VA_ARGS__) +#define DLOG(fmt, ...) debuglog(LOGLEVEL, "%s:%s:%d - " fmt, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__) + +extern char *loglevels[]; + +/** + * Enables the given loglevel. + * + */ +void add_loglevel(const char *level); + +/** + * Set verbosity of i3. If verbose is set to true, informative messages will + * be printed to stdout. If verbose is set to false, only errors will be + * printed. + * + */ +void set_verbosity(bool _verbose); + +/** + * Logs the given message to stdout while prefixing the current time to it, + * but only if the corresponding debug loglevel was activated. + * + */ +void debuglog(int lev, char *fmt, ...); + +/** + * Logs the given message to stdout while prefixing the current time to it. + * + */ +void errorlog(char *fmt, ...); + +/** + * Logs the given message to stdout while prefixing the current time to it, + * but only if verbose mode is activated. + * + */ +void verboselog(char *fmt, ...); + +/** + * Logs the given message to stdout while prefixing the current time to it. + * This is to be called by LOG() which includes filename/linenumber + * + */ +void slog(char *fmt, va_list args); + +#endif diff --git a/src/log.c b/src/log.c new file mode 100644 index 00000000..1fcf70cb --- /dev/null +++ b/src/log.c @@ -0,0 +1,121 @@ +/* + * vim:ts=8:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + * src/log.c: handles the setting of loglevels, contains the logging functions. + * + */ +#include +#include +#include +#include + +#include "util.h" +#include "log.h" + +/* loglevels.h is autogenerated at make time */ +#include "loglevels.h" + +static uint32_t loglevel = 0; +static bool verbose = false; + +/** + * Set verbosity of i3. If verbose is set to true, informative messages will + * be printed to stdout. If verbose is set to false, only errors will be + * printed. + * + */ +void set_verbosity(bool _verbose) { + verbose = _verbose; +} + +/** + * Enables the given loglevel. + * + */ +void add_loglevel(const char *level) { + /* Handle the special loglevel "all" */ + if (strcasecmp(level, "all") == 0) { + loglevel = UINT32_MAX; + return; + } + + for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) { + if (strcasecmp(loglevels[i], level) != 0) + continue; + + /* The position in the array (plus one) is the amount of times + * which we need to shift 1 to the left to get our bitmask for + * the specific loglevel. */ + loglevel |= (1 << (i+1)); + break; + } +} + +/* + * Logs the given message to stdout while prefixing the current time to it. + * This is to be called by *LOG() which includes filename/linenumber/function. + * + */ +void vlog(char *fmt, va_list args) { + char timebuf[64]; + + /* Get current time */ + time_t t = time(NULL); + /* Convert time to local time (determined by the locale) */ + struct tm *tmp = localtime(&t); + /* Generate time prefix */ + strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp); + printf("%s", timebuf); + vprintf(fmt, args); +} + +/** + * Logs the given message to stdout while prefixing the current time to it, + * but only if verbose mode is activated. + * + */ +void verboselog(char *fmt, ...) { + va_list args; + + if (!verbose) + return; + + va_start(args, fmt); + vlog(fmt, args); + va_end(args); +} + +/** + * Logs the given message to stdout while prefixing the current time to it. + * + */ +void errorlog(char *fmt, ...) { + va_list args; + + va_start(args, fmt); + vlog(fmt, args); + va_end(args); +} + +/* + * Logs the given message to stdout while prefixing the current time to it, + * but only if the corresponding debug loglevel was activated. + * This is to be called by DLOG() which includes filename/linenumber + * + */ +void debuglog(int lev, char *fmt, ...) { + va_list args; + + if ((loglevel & lev) == 0) + return; + + va_start(args, fmt); + vlog(fmt, args); + va_end(args); +}