]> git.sur5r.net Git - i3/i3/blob - src/log.c
c258b19ba38414a6b9b1717a5ae9be7bb05d0feb
[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 #include <stdlib.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21
22 #include "util.h"
23 #include "log.h"
24
25 /* loglevels.h is autogenerated at make time */
26 #include "loglevels.h"
27
28 static uint64_t loglevel = 0;
29 static bool verbose = false;
30 static FILE *errorfile;
31 char *errorfilename;
32
33 /*
34  * Initializes logging by creating an error logfile in /tmp (or
35  * XDG_RUNTIME_DIR, see get_process_filename()).
36  *
37  */
38 void init_logging() {
39     errorfilename = get_process_filename("errorlog");
40     if (errorfilename == NULL) {
41         ELOG("Could not initialize errorlog\n");
42         return;
43     }
44
45     errorfile = fopen(errorfilename, "w");
46     if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
47         ELOG("Could not set close-on-exec flag\n");
48     }
49 }
50
51 /*
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
54  * printed.
55  *
56  */
57 void set_verbosity(bool _verbose) {
58     verbose = _verbose;
59 }
60
61 /*
62  * Enables the given loglevel.
63  *
64  */
65 void add_loglevel(const char *level) {
66     /* Handle the special loglevel "all" */
67     if (strcasecmp(level, "all") == 0) {
68         loglevel = UINT64_MAX;
69         return;
70     }
71
72     for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
73         if (strcasecmp(loglevels[i], level) != 0)
74             continue;
75
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));
80         break;
81     }
82 }
83
84 /*
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.
87  *
88  */
89 void vlog(char *fmt, va_list args) {
90     static char timebuf[64];
91     static struct tm result;
92
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);
99 #ifdef DEBUG_TIMING
100     struct timeval tv;
101     gettimeofday(&tv, NULL);
102     printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
103 #else
104     printf("%s", timebuf);
105 #endif
106     vprintf(fmt, args);
107 }
108
109 /*
110  * Logs the given message to stdout while prefixing the current time to it,
111  * but only if verbose mode is activated.
112  *
113  */
114 void verboselog(char *fmt, ...) {
115     va_list args;
116
117     if (!verbose)
118         return;
119
120     va_start(args, fmt);
121     vlog(fmt, args);
122     va_end(args);
123 }
124
125 /*
126  * Logs the given message to stdout while prefixing the current time to it.
127  *
128  */
129 void errorlog(char *fmt, ...) {
130     va_list args;
131
132     va_start(args, fmt);
133     vlog(fmt, args);
134     va_end(args);
135
136     /* also log to the error logfile, if opened */
137     va_start(args, fmt);
138     vfprintf(errorfile, fmt, args);
139     fflush(errorfile);
140     va_end(args);
141 }
142
143 /*
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
147  *
148  */
149 void debuglog(uint64_t lev, char *fmt, ...) {
150     va_list args;
151
152     if ((loglevel & lev) == 0)
153         return;
154
155     va_start(args, fmt);
156     vlog(fmt, args);
157     va_end(args);
158 }