]> git.sur5r.net Git - i3/i3/blob - src/log.c
Merge branch 'tree' 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
20 #include "util.h"
21 #include "log.h"
22
23 /* loglevels.h is autogenerated at make time */
24 #include "loglevels.h"
25
26 static uint64_t loglevel = 0;
27 static bool verbose = true;
28 static FILE *errorfile;
29 char *errorfilename;
30
31 /*
32  * Initializes logging by creating an error logfile in /tmp (or
33  * XDG_RUNTIME_DIR, see get_process_filename()).
34  *
35  */
36 void init_logging() {
37     errorfilename = get_process_filename("errorlog");
38     if (errorfilename == NULL) {
39         ELOG("Could not initialize errorlog\n");
40         return;
41     }
42
43     errorfile = fopen(errorfilename, "w");
44 }
45
46 /*
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
49  * printed.
50  *
51  */
52 void set_verbosity(bool _verbose) {
53     verbose = _verbose;
54 }
55
56 /*
57  * Enables the given loglevel.
58  *
59  */
60 void add_loglevel(const char *level) {
61     /* Handle the special loglevel "all" */
62     if (strcasecmp(level, "all") == 0) {
63         loglevel = UINT64_MAX;
64         return;
65     }
66
67     for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
68         if (strcasecmp(loglevels[i], level) != 0)
69             continue;
70
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));
75         break;
76     }
77 }
78
79 /*
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.
82  *
83  */
84 void vlog(char *fmt, va_list args) {
85     char timebuf[64];
86
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);
93 #ifdef DEBUG_TIMING
94     struct timeval tv;
95     gettimeofday(&tv, NULL);
96     printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
97 #else
98     printf("%s", timebuf);
99 #endif
100     vprintf(fmt, args);
101 }
102
103 /*
104  * Logs the given message to stdout while prefixing the current time to it,
105  * but only if verbose mode is activated.
106  *
107  */
108 void verboselog(char *fmt, ...) {
109     va_list args;
110
111     if (!verbose)
112         return;
113
114     va_start(args, fmt);
115     vlog(fmt, args);
116     va_end(args);
117 }
118
119 /*
120  * Logs the given message to stdout while prefixing the current time to it.
121  *
122  */
123 void errorlog(char *fmt, ...) {
124     va_list args;
125
126     va_start(args, fmt);
127     vlog(fmt, args);
128     va_end(args);
129
130     /* also log to the error logfile, if opened */
131     va_start(args, fmt);
132     vfprintf(errorfile, fmt, args);
133     fflush(errorfile);
134     va_end(args);
135 }
136
137 /*
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
141  *
142  */
143 void debuglog(uint64_t lev, char *fmt, ...) {
144     va_list args;
145
146     if ((loglevel & lev) == 0)
147         return;
148
149     va_start(args, fmt);
150     vlog(fmt, args);
151     va_end(args);
152 }