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) {
92 /* Get current time */
93 time_t t = time(NULL);
94 /* Convert time to local time (determined by the locale) */
95 struct tm *tmp = localtime(&t);
96 /* Generate time prefix */
97 strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
100 gettimeofday(&tv, NULL);
101 printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
103 printf("%s", timebuf);
109 * Logs the given message to stdout while prefixing the current time to it,
110 * but only if verbose mode is activated.
113 void verboselog(char *fmt, ...) {
125 * Logs the given message to stdout while prefixing the current time to it.
128 void errorlog(char *fmt, ...) {
135 /* also log to the error logfile, if opened */
137 vfprintf(errorfile, fmt, args);
143 * Logs the given message to stdout while prefixing the current time to it,
144 * but only if the corresponding debug loglevel was activated.
145 * This is to be called by DLOG() which includes filename/linenumber
148 void debuglog(uint64_t lev, char *fmt, ...) {
151 if ((loglevel & lev) == 0)