2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * log.c: Setting of loglevels, logging functions.
22 /* loglevels.h is autogenerated at make time */
23 #include "loglevels.h"
25 static uint64_t loglevel = 0;
26 static bool verbose = false;
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");
43 if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
44 ELOG("Could not set close-on-exec flag\n");
49 * Set verbosity of i3. If verbose is set to true, informative messages will
50 * be printed to stdout. If verbose is set to false, only errors will be
54 void set_verbosity(bool _verbose) {
59 * Enables the given loglevel.
62 void add_loglevel(const char *level) {
63 /* Handle the special loglevel "all" */
64 if (strcasecmp(level, "all") == 0) {
65 loglevel = UINT64_MAX;
69 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
70 if (strcasecmp(loglevels[i], level) != 0)
73 /* The position in the array (plus one) is the amount of times
74 * which we need to shift 1 to the left to get our bitmask for
75 * the specific loglevel. */
76 loglevel |= (1 << (i+1));
82 * Logs the given message to stdout while prefixing the current time to it.
83 * This is to be called by *LOG() which includes filename/linenumber/function.
86 void vlog(char *fmt, va_list args) {
87 static char timebuf[64];
88 static struct tm result;
90 /* Get current time */
91 time_t t = time(NULL);
92 /* Convert time to local time (determined by the locale) */
93 struct tm *tmp = localtime_r(&t, &result);
94 /* Generate time prefix */
95 strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
98 gettimeofday(&tv, NULL);
99 printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
101 printf("%s", timebuf);
107 * Logs the given message to stdout while prefixing the current time to it,
108 * but only if verbose mode is activated.
111 void verboselog(char *fmt, ...) {
123 * Logs the given message to stdout while prefixing the current time to it.
126 void errorlog(char *fmt, ...) {
133 /* also log to the error logfile, if opened */
135 vfprintf(errorfile, fmt, args);
141 * Logs the given message to stdout while prefixing the current time to it,
142 * but only if the corresponding debug loglevel was activated.
143 * This is to be called by DLOG() which includes filename/linenumber
146 void debuglog(uint64_t lev, char *fmt, ...) {
149 if ((loglevel & lev) == 0)