2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * log.c: Setting of loglevels, logging functions.
27 /* loglevels.h is autogenerated at make time */
28 #include "loglevels.h"
30 static uint64_t loglevel = 0;
31 static bool verbose = false;
32 static FILE *errorfile;
35 /* SHM logging variables */
37 /* The name for the SHM (/i3-log-%pid). Will end up on /dev/shm on most
38 * systems. Global so that we can clean up at exit. */
39 char *shmlogname = "";
40 /* Size limit for the SHM log, by default 25 MiB. Can be overwritten using the
41 * flag --shmlog-size. */
43 /* If enabled, logbuffer will point to a memory mapping of the i3 SHM log. */
44 static char *logbuffer;
45 /* A pointer (within logbuffer) where data will be written to next. */
47 /* A pointer to the byte where we last wrapped. Necessary to not print the
48 * left-overs at the end of the ringbuffer. */
49 static char *loglastwrap;
50 /* Size (in bytes) of the i3 SHM log. */
51 static int logbuffer_size;
52 /* File descriptor for shm_open. */
53 static int logbuffer_shm;
56 * Initializes logging by creating an error logfile in /tmp (or
57 * XDG_RUNTIME_DIR, see get_process_filename()).
59 * Will be called twice if --shmlog-size is specified.
64 if (!(errorfilename = get_process_filename("errorlog")))
65 ELOG("Could not initialize errorlog\n");
67 errorfile = fopen(errorfilename, "w");
68 if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
69 ELOG("Could not set close-on-exec flag\n");
74 /* If this is a debug build (not a release version), we will enable SHM
75 * logging by default, unless the user turned it off explicitly. */
76 if (logbuffer == NULL && shmlog_size > 0) {
77 /* Reserve 1% of the RAM for the logfile, but at max 25 MiB.
78 * For 512 MiB of RAM this will lead to a 5 MiB log buffer.
79 * At the moment (2011-12-10), no testcase leads to an i3 log
80 * of more than ~ 600 KiB. */
81 long long physical_mem_bytes = (long long)sysconf(_SC_PHYS_PAGES) *
82 sysconf(_SC_PAGESIZE);
83 logbuffer_size = min(physical_mem_bytes * 0.01, shmlog_size);
84 sasprintf(&shmlogname, "/i3-log-%d", getpid());
85 logbuffer_shm = shm_open(shmlogname, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
86 if (logbuffer_shm == -1) {
87 ELOG("Could not shm_open SHM segment for the i3 log: %s\n", strerror(errno));
91 if (ftruncate(logbuffer_shm, logbuffer_size) == -1) {
93 shm_unlink("/i3-log-");
94 ELOG("Could not ftruncate SHM segment for the i3 log: %s\n", strerror(errno));
98 logbuffer = mmap(NULL, logbuffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
99 if (logbuffer == MAP_FAILED) {
100 close(logbuffer_shm);
101 shm_unlink("/i3-log-");
102 ELOG("Could not mmap SHM segment for the i3 log: %s\n", strerror(errno));
107 loglastwrap = logbuffer + logbuffer_size;
112 * Set verbosity of i3. If verbose is set to true, informative messages will
113 * be printed to stdout. If verbose is set to false, only errors will be
117 void set_verbosity(bool _verbose) {
122 * Enables the given loglevel.
125 void add_loglevel(const char *level) {
126 /* Handle the special loglevel "all" */
127 if (strcasecmp(level, "all") == 0) {
128 loglevel = UINT64_MAX;
132 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
133 if (strcasecmp(loglevels[i], level) != 0)
136 /* The position in the array (plus one) is the amount of times
137 * which we need to shift 1 to the left to get our bitmask for
138 * the specific loglevel. */
139 loglevel |= (1 << (i+1));
145 * Returns the offsets for the next write and for the last wrap.
146 * Necessary to print the i3 SHM log in the correct order.
149 void get_log_markers(int *offset_next_write, int *offset_last_wrap, int *size) {
150 *offset_next_write = (logwalk - logbuffer);
151 *offset_last_wrap = (loglastwrap - logbuffer);
152 *size = logbuffer_size;
156 * Logs the given message to stdout (if print is true) while prefixing the
157 * current time to it. Additionally, the message will be saved in the i3 SHM
159 * This is to be called by *LOG() which includes filename/linenumber/function.
162 static void vlog(const bool print, const char *fmt, va_list args) {
163 /* Precisely one page to not consume too much memory but to hold enough
164 * data to be useful. */
165 static char message[4096];
166 static struct tm result;
168 static struct tm *tmp;
171 /* Get current time */
173 /* Convert time to local time (determined by the locale) */
174 tmp = localtime_r(&t, &result);
175 /* Generate time prefix */
176 len = strftime(message, sizeof(message), "%x %X - ", tmp);
181 * true true format message, save, print
182 * true false format message, save
183 * false true print message only
184 * false false INVALID, never called
189 gettimeofday(&tv, NULL);
190 printf("%s%d.%d - ", message, tv.tv_sec, tv.tv_usec);
192 printf("%s", message);
196 len += vsnprintf(message + len, sizeof(message) - len, fmt, args);
198 fprintf(stderr, "BUG: something is overflowing here. Dropping the log entry\n");
202 if (len >= sizeof(message)) {
203 fprintf(stderr, "BUG: single log message > 4k\n");
206 /* If there is no space for the current message (plus trailing
207 * nullbyte) in the ringbuffer, we need to wrap and write to the
208 * beginning again. */
209 if ((len+1) >= (logbuffer_size - (logwalk - logbuffer))) {
210 loglastwrap = logwalk;
214 /* Copy the buffer, terminate it, move the write pointer to the byte after
215 * our current message. */
216 strncpy(logwalk, message, len);
221 fwrite(message, len, 1, stdout);
226 * Logs the given message to stdout while prefixing the current time to it,
227 * but only if verbose mode is activated.
230 void verboselog(char *fmt, ...) {
233 if (!logbuffer && !verbose)
237 vlog(verbose, fmt, args);
242 * Logs the given message to stdout while prefixing the current time to it.
245 void errorlog(char *fmt, ...) {
249 vlog(true, fmt, args);
252 /* also log to the error logfile, if opened */
254 vfprintf(errorfile, fmt, args);
260 * Logs the given message to stdout while prefixing the current time to it,
261 * but only if the corresponding debug loglevel was activated.
262 * This is to be called by DLOG() which includes filename/linenumber
265 void debuglog(uint64_t lev, char *fmt, ...) {
268 if (!logbuffer && !(loglevel & lev))
272 vlog((loglevel & lev), fmt, args);