]> git.sur5r.net Git - i3/i3/blob - src/log.c
call tree_render() only after commands which require it
[i3/i3] / src / log.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * src/log.c: handles the setting of loglevels, contains the logging functions.
11  *
12  */
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdbool.h>
17
18 #include "util.h"
19 #include "log.h"
20
21 /* loglevels.h is autogenerated at make time */
22 #include "loglevels.h"
23
24 static uint64_t loglevel = 0;
25 static bool verbose = true;
26
27 /*
28  * Set verbosity of i3. If verbose is set to true, informative messages will
29  * be printed to stdout. If verbose is set to false, only errors will be
30  * printed.
31  *
32  */
33 void set_verbosity(bool _verbose) {
34     verbose = _verbose;
35 }
36
37 /*
38  * Enables the given loglevel.
39  *
40  */
41 void add_loglevel(const char *level) {
42     /* Handle the special loglevel "all" */
43     if (strcasecmp(level, "all") == 0) {
44         loglevel = UINT64_MAX;
45         return;
46     }
47
48     for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
49         if (strcasecmp(loglevels[i], level) != 0)
50             continue;
51
52         /* The position in the array (plus one) is the amount of times
53          * which we need to shift 1 to the left to get our bitmask for
54          * the specific loglevel. */
55         loglevel |= (1 << (i+1));
56         break;
57     }
58 }
59
60 /*
61  * Logs the given message to stdout while prefixing the current time to it.
62  * This is to be called by *LOG() which includes filename/linenumber/function.
63  *
64  */
65 void vlog(char *fmt, va_list args) {
66     char timebuf[64];
67
68     /* Get current time */
69     time_t t = time(NULL);
70     /* Convert time to local time (determined by the locale) */
71     struct tm *tmp = localtime(&t);
72     /* Generate time prefix */
73     strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
74     printf("%s", timebuf);
75     vprintf(fmt, args);
76 }
77
78 /*
79  * Logs the given message to stdout while prefixing the current time to it,
80  * but only if verbose mode is activated.
81  *
82  */
83 void verboselog(char *fmt, ...) {
84     va_list args;
85
86     if (!verbose)
87         return;
88
89     va_start(args, fmt);
90     vlog(fmt, args);
91     va_end(args);
92 }
93
94 /*
95  * Logs the given message to stdout while prefixing the current time to it.
96  *
97  */
98 void errorlog(char *fmt, ...) {
99     va_list args;
100
101     va_start(args, fmt);
102     vlog(fmt, args);
103     va_end(args);
104 }
105
106 /*
107  * Logs the given message to stdout while prefixing the current time to it,
108  * but only if the corresponding debug loglevel was activated.
109  * This is to be called by DLOG() which includes filename/linenumber
110  *
111  */
112 void debuglog(uint64_t lev, char *fmt, ...) {
113     va_list args;
114
115     if ((loglevel & lev) == 0)
116         return;
117
118     va_start(args, fmt);
119     vlog(fmt, args);
120     va_end(args);
121 }