2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * src/log.c: handles the setting of loglevels, contains the logging functions.
22 /* loglevels.h is autogenerated at make time */
23 #include "loglevels.h"
25 static uint64_t loglevel = 0;
26 static bool verbose = true;
27 static FILE *errorfile;
31 * Initializes logging by creating an error logfile in /tmp (or
32 * XDG_RUNTIME_DIR, see get_process_filename()).
36 errorfilename = get_process_filename("errorlog");
37 if (errorfilename == NULL) {
38 ELOG("Could not initialize errorlog\n");
42 errorfile = fopen(errorfilename, "w");
46 * Set verbosity of i3. If verbose is set to true, informative messages will
47 * be printed to stdout. If verbose is set to false, only errors will be
51 void set_verbosity(bool _verbose) {
56 * Enables the given loglevel.
59 void add_loglevel(const char *level) {
60 /* Handle the special loglevel "all" */
61 if (strcasecmp(level, "all") == 0) {
62 loglevel = UINT64_MAX;
66 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
67 if (strcasecmp(loglevels[i], level) != 0)
70 /* The position in the array (plus one) is the amount of times
71 * which we need to shift 1 to the left to get our bitmask for
72 * the specific loglevel. */
73 loglevel |= (1 << (i+1));
79 * Logs the given message to stdout while prefixing the current time to it.
80 * This is to be called by *LOG() which includes filename/linenumber/function.
83 void vlog(char *fmt, va_list args) {
86 /* Get current time */
87 time_t t = time(NULL);
88 /* Convert time to local time (determined by the locale) */
89 struct tm *tmp = localtime(&t);
90 /* Generate time prefix */
91 strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
92 printf("%s", timebuf);
97 * Logs the given message to stdout while prefixing the current time to it,
98 * but only if verbose mode is activated.
101 void verboselog(char *fmt, ...) {
113 * Logs the given message to stdout while prefixing the current time to it.
116 void errorlog(char *fmt, ...) {
123 /* also log to the error logfile, if opened */
125 vfprintf(errorfile, fmt, args);
131 * Logs the given message to stdout while prefixing the current time to it,
132 * but only if the corresponding debug loglevel was activated.
133 * This is to be called by DLOG() which includes filename/linenumber
136 void debuglog(uint64_t lev, char *fmt, ...) {
139 if ((loglevel & lev) == 0)