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.
25 /* loglevels.h is autogenerated at make time */
26 #include "loglevels.h"
28 static uint64_t loglevel = 0;
29 static bool verbose = false;
30 static FILE *errorfile;
34 * Initializes logging by creating an error logfile in /tmp (or
35 * XDG_RUNTIME_DIR, see get_process_filename()).
39 errorfilename = get_process_filename("errorlog");
40 if (errorfilename == NULL) {
41 ELOG("Could not initialize errorlog\n");
45 errorfile = fopen(errorfilename, "w");
46 if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
47 ELOG("Could not set close-on-exec flag\n");
52 * Set verbosity of i3. If verbose is set to true, informative messages will
53 * be printed to stdout. If verbose is set to false, only errors will be
57 void set_verbosity(bool _verbose) {
62 * Enables the given loglevel.
65 void add_loglevel(const char *level) {
66 /* Handle the special loglevel "all" */
67 if (strcasecmp(level, "all") == 0) {
68 loglevel = UINT64_MAX;
72 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
73 if (strcasecmp(loglevels[i], level) != 0)
76 /* The position in the array (plus one) is the amount of times
77 * which we need to shift 1 to the left to get our bitmask for
78 * the specific loglevel. */
79 loglevel |= (1 << (i+1));
85 * Logs the given message to stdout while prefixing the current time to it.
86 * This is to be called by *LOG() which includes filename/linenumber/function.
89 void vlog(char *fmt, va_list args) {
90 static char timebuf[64];
91 static struct tm result;
93 /* Get current time */
94 time_t t = time(NULL);
95 /* Convert time to local time (determined by the locale) */
96 struct tm *tmp = localtime_r(&t, &result);
97 /* Generate time prefix */
98 strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
101 gettimeofday(&tv, NULL);
102 printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
104 printf("%s", timebuf);
110 * Logs the given message to stdout while prefixing the current time to it,
111 * but only if verbose mode is activated.
114 void verboselog(char *fmt, ...) {
126 * Logs the given message to stdout while prefixing the current time to it.
129 void errorlog(char *fmt, ...) {
136 /* also log to the error logfile, if opened */
138 vfprintf(errorfile, fmt, args);
144 * Logs the given message to stdout while prefixing the current time to it,
145 * but only if the corresponding debug loglevel was activated.
146 * This is to be called by DLOG() which includes filename/linenumber
149 void debuglog(uint64_t lev, char *fmt, ...) {
152 if ((loglevel & lev) == 0)