]> git.sur5r.net Git - i3/i3/blob - src/log.c
Merge branch 'master' into next
[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     char timebuf[64];
91
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);
98 #ifdef DEBUG_TIMING
99     struct timeval tv;
100     gettimeofday(&tv, NULL);
101     printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
102 #else
103     printf("%s", timebuf);
104 #endif
105     vprintf(fmt, args);
106 }
107
108 /*
109  * Logs the given message to stdout while prefixing the current time to it,
110  * but only if verbose mode is activated.
111  *
112  */
113 void verboselog(char *fmt, ...) {
114     va_list args;
115
116     if (!verbose)
117         return;
118
119     va_start(args, fmt);
120     vlog(fmt, args);
121     va_end(args);
122 }
123
124 /*
125  * Logs the given message to stdout while prefixing the current time to it.
126  *
127  */
128 void errorlog(char *fmt, ...) {
129     va_list args;
130
131     va_start(args, fmt);
132     vlog(fmt, args);
133     va_end(args);
134
135     /* also log to the error logfile, if opened */
136     va_start(args, fmt);
137     vfprintf(errorfile, fmt, args);
138     fflush(errorfile);
139     va_end(args);
140 }
141
142 /*
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
146  *
147  */
148 void debuglog(uint64_t lev, char *fmt, ...) {
149     va_list args;
150
151     if ((loglevel & lev) == 0)
152         return;
153
154     va_start(args, fmt);
155     vlog(fmt, args);
156     va_end(args);
157 }