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.
28 /* loglevels.h is autogenerated at make time */
29 #include "loglevels.h"
31 static uint64_t loglevel = 0;
32 static bool verbose = false;
33 static FILE *errorfile;
36 /* SHM logging variables */
38 /* The name for the SHM (/i3-log-%pid). Will end up on /dev/shm on most
39 * systems. Global so that we can clean up at exit. */
40 char *shmlogname = "";
41 /* Size limit for the SHM log, by default 25 MiB. Can be overwritten using the
42 * flag --shmlog-size. */
44 /* If enabled, logbuffer will point to a memory mapping of the i3 SHM log. */
45 static char *logbuffer;
46 /* A pointer (within logbuffer) where data will be written to next. */
48 /* A pointer to the byte where we last wrapped. Necessary to not print the
49 * left-overs at the end of the ringbuffer. */
50 static char *loglastwrap;
51 /* Size (in bytes) of the i3 SHM log. */
52 static int logbuffer_size;
53 /* File descriptor for shm_open. */
54 static int logbuffer_shm;
57 * Writes the offsets for the next write and for the last wrap to the
59 * Necessary to print the i3 SHM log in the correct order.
62 static void store_log_markers() {
63 i3_shmlog_header *header = (i3_shmlog_header*)logbuffer;
65 header->offset_next_write = (logwalk - logbuffer);
66 header->offset_last_wrap = (loglastwrap - logbuffer);
67 header->size = logbuffer_size;
71 * Initializes logging by creating an error logfile in /tmp (or
72 * XDG_RUNTIME_DIR, see get_process_filename()).
74 * Will be called twice if --shmlog-size is specified.
79 if (!(errorfilename = get_process_filename("errorlog")))
80 ELOG("Could not initialize errorlog\n");
82 errorfile = fopen(errorfilename, "w");
83 if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
84 ELOG("Could not set close-on-exec flag\n");
89 /* If this is a debug build (not a release version), we will enable SHM
90 * logging by default, unless the user turned it off explicitly. */
91 if (logbuffer == NULL && shmlog_size > 0) {
92 /* Reserve 1% of the RAM for the logfile, but at max 25 MiB.
93 * For 512 MiB of RAM this will lead to a 5 MiB log buffer.
94 * At the moment (2011-12-10), no testcase leads to an i3 log
95 * of more than ~ 600 KiB. */
96 long long physical_mem_bytes = (long long)sysconf(_SC_PHYS_PAGES) *
97 sysconf(_SC_PAGESIZE);
98 logbuffer_size = min(physical_mem_bytes * 0.01, shmlog_size);
99 sasprintf(&shmlogname, "/i3-log-%d", getpid());
100 logbuffer_shm = shm_open(shmlogname, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
101 if (logbuffer_shm == -1) {
102 ELOG("Could not shm_open SHM segment for the i3 log: %s\n", strerror(errno));
106 if (ftruncate(logbuffer_shm, logbuffer_size) == -1) {
107 close(logbuffer_shm);
108 shm_unlink("/i3-log-");
109 ELOG("Could not ftruncate SHM segment for the i3 log: %s\n", strerror(errno));
113 logbuffer = mmap(NULL, logbuffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
114 if (logbuffer == MAP_FAILED) {
115 close(logbuffer_shm);
116 shm_unlink("/i3-log-");
117 ELOG("Could not mmap SHM segment for the i3 log: %s\n", strerror(errno));
121 logwalk = logbuffer + sizeof(i3_shmlog_header);
122 loglastwrap = logbuffer + logbuffer_size;
128 * Set verbosity of i3. If verbose is set to true, informative messages will
129 * be printed to stdout. If verbose is set to false, only errors will be
133 void set_verbosity(bool _verbose) {
138 * Enables the given loglevel.
141 void add_loglevel(const char *level) {
142 /* Handle the special loglevel "all" */
143 if (strcasecmp(level, "all") == 0) {
144 loglevel = UINT64_MAX;
148 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
149 if (strcasecmp(loglevels[i], level) != 0)
152 /* The position in the array (plus one) is the amount of times
153 * which we need to shift 1 to the left to get our bitmask for
154 * the specific loglevel. */
155 loglevel |= (1 << (i+1));
161 * Logs the given message to stdout (if print is true) while prefixing the
162 * current time to it. Additionally, the message will be saved in the i3 SHM
164 * This is to be called by *LOG() which includes filename/linenumber/function.
167 static void vlog(const bool print, const char *fmt, va_list args) {
168 /* Precisely one page to not consume too much memory but to hold enough
169 * data to be useful. */
170 static char message[4096];
171 static struct tm result;
173 static struct tm *tmp;
176 /* Get current time */
178 /* Convert time to local time (determined by the locale) */
179 tmp = localtime_r(&t, &result);
180 /* Generate time prefix */
181 len = strftime(message, sizeof(message), "%x %X - ", tmp);
186 * true true format message, save, print
187 * true false format message, save
188 * false true print message only
189 * false false INVALID, never called
194 gettimeofday(&tv, NULL);
195 printf("%s%d.%d - ", message, tv.tv_sec, tv.tv_usec);
197 printf("%s", message);
201 len += vsnprintf(message + len, sizeof(message) - len, fmt, args);
203 fprintf(stderr, "BUG: something is overflowing here. Dropping the log entry\n");
207 if (len >= sizeof(message)) {
208 fprintf(stderr, "BUG: single log message > 4k\n");
211 /* If there is no space for the current message (plus trailing
212 * nullbyte) in the ringbuffer, we need to wrap and write to the
213 * beginning again. */
214 if ((len+1) >= (logbuffer_size - (logwalk - logbuffer))) {
215 loglastwrap = logwalk;
216 logwalk = logbuffer + sizeof(i3_shmlog_header);
219 /* Copy the buffer, terminate it, move the write pointer to the byte after
220 * our current message. */
221 strncpy(logwalk, message, len);
228 fwrite(message, len, 1, stdout);
233 * Logs the given message to stdout while prefixing the current time to it,
234 * but only if verbose mode is activated.
237 void verboselog(char *fmt, ...) {
240 if (!logbuffer && !verbose)
244 vlog(verbose, fmt, args);
249 * Logs the given message to stdout while prefixing the current time to it.
252 void errorlog(char *fmt, ...) {
256 vlog(true, fmt, args);
259 /* also log to the error logfile, if opened */
261 vfprintf(errorfile, fmt, args);
267 * Logs the given message to stdout while prefixing the current time to it,
268 * but only if the corresponding debug loglevel was activated.
269 * This is to be called by DLOG() which includes filename/linenumber
272 void debuglog(uint64_t lev, char *fmt, ...) {
275 if (!logbuffer && !(loglevel & lev))
279 vlog((loglevel & lev), fmt, args);