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.
23 /* loglevels.h is autogenerated at make time */
24 #include "loglevels.h"
26 static uint64_t loglevel = 0;
27 static bool verbose = true;
28 static FILE *errorfile;
32 * Initializes logging by creating an error logfile in /tmp (or
33 * XDG_RUNTIME_DIR, see get_process_filename()).
37 errorfilename = get_process_filename("errorlog");
38 if (errorfilename == NULL) {
39 ELOG("Could not initialize errorlog\n");
43 errorfile = fopen(errorfilename, "w");
47 * Set verbosity of i3. If verbose is set to true, informative messages will
48 * be printed to stdout. If verbose is set to false, only errors will be
52 void set_verbosity(bool _verbose) {
57 * Enables the given loglevel.
60 void add_loglevel(const char *level) {
61 /* Handle the special loglevel "all" */
62 if (strcasecmp(level, "all") == 0) {
63 loglevel = UINT64_MAX;
67 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
68 if (strcasecmp(loglevels[i], level) != 0)
71 /* The position in the array (plus one) is the amount of times
72 * which we need to shift 1 to the left to get our bitmask for
73 * the specific loglevel. */
74 loglevel |= (1 << (i+1));
80 * Logs the given message to stdout while prefixing the current time to it.
81 * This is to be called by *LOG() which includes filename/linenumber/function.
84 void vlog(char *fmt, va_list args) {
87 /* Get current time */
88 time_t t = time(NULL);
89 /* Convert time to local time (determined by the locale) */
90 struct tm *tmp = localtime(&t);
91 /* Generate time prefix */
92 strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
95 gettimeofday(&tv, NULL);
96 printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
98 printf("%s", timebuf);
104 * Logs the given message to stdout while prefixing the current time to it,
105 * but only if verbose mode is activated.
108 void verboselog(char *fmt, ...) {
120 * Logs the given message to stdout while prefixing the current time to it.
123 void errorlog(char *fmt, ...) {
130 /* also log to the error logfile, if opened */
132 vfprintf(errorfile, fmt, args);
138 * Logs the given message to stdout while prefixing the current time to it,
139 * but only if the corresponding debug loglevel was activated.
140 * This is to be called by DLOG() which includes filename/linenumber
143 void debuglog(uint64_t lev, char *fmt, ...) {
146 if ((loglevel & lev) == 0)