2 #define I3__FILE__ "log.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
9 * log.c: Logging functions.
23 #if defined(__APPLE__)
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
34 static bool debug_logging = false;
35 static bool verbose = false;
36 static FILE *errorfile;
39 /* SHM logging variables */
41 /* The name for the SHM (/i3-log-%pid). Will end up on /dev/shm on most
42 * systems. Global so that we can clean up at exit. */
43 char *shmlogname = "";
44 /* Size limit for the SHM log, by default 25 MiB. Can be overwritten using the
45 * flag --shmlog-size. */
47 /* If enabled, logbuffer will point to a memory mapping of the i3 SHM log. */
48 static char *logbuffer;
49 /* A pointer (within logbuffer) where data will be written to next. */
51 /* A pointer to the byte where we last wrapped. Necessary to not print the
52 * left-overs at the end of the ringbuffer. */
53 static char *loglastwrap;
54 /* Size (in bytes) of the i3 SHM log. */
55 static int logbuffer_size;
56 /* File descriptor for shm_open. */
57 static int logbuffer_shm;
60 * Writes the offsets for the next write and for the last wrap to the
62 * Necessary to print the i3 SHM log in the correct order.
65 static void store_log_markers(void) {
66 i3_shmlog_header *header = (i3_shmlog_header*)logbuffer;
68 header->offset_next_write = (logwalk - logbuffer);
69 header->offset_last_wrap = (loglastwrap - logbuffer);
70 header->size = logbuffer_size;
74 * Initializes logging by creating an error logfile in /tmp (or
75 * XDG_RUNTIME_DIR, see get_process_filename()).
77 * Will be called twice if --shmlog-size is specified.
80 void init_logging(void) {
82 if (!(errorfilename = get_process_filename("errorlog")))
83 ELOG("Could not initialize errorlog\n");
85 errorfile = fopen(errorfilename, "w");
86 if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
87 ELOG("Could not set close-on-exec flag\n");
92 /* If this is a debug build (not a release version), we will enable SHM
93 * logging by default, unless the user turned it off explicitly. */
94 if (logbuffer == NULL && shmlog_size > 0) {
95 /* Reserve 1% of the RAM for the logfile, but at max 25 MiB.
96 * For 512 MiB of RAM this will lead to a 5 MiB log buffer.
97 * At the moment (2011-12-10), no testcase leads to an i3 log
98 * of more than ~ 600 KiB. */
99 long long physical_mem_bytes;
100 #if defined(__APPLE__)
101 int mib[2] = { CTL_HW, HW_MEMSIZE };
102 size_t length = sizeof(long long);
103 sysctl(mib, 2, &physical_mem_bytes, &length, NULL, 0);
105 physical_mem_bytes = (long long)sysconf(_SC_PHYS_PAGES) *
106 sysconf(_SC_PAGESIZE);
108 logbuffer_size = min(physical_mem_bytes * 0.01, shmlog_size);
109 sasprintf(&shmlogname, "/i3-log-%d", getpid());
110 logbuffer_shm = shm_open(shmlogname, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
111 if (logbuffer_shm == -1) {
112 ELOG("Could not shm_open SHM segment for the i3 log: %s\n", strerror(errno));
116 if (ftruncate(logbuffer_shm, logbuffer_size) == -1) {
117 close(logbuffer_shm);
118 shm_unlink("/i3-log-");
119 ELOG("Could not ftruncate SHM segment for the i3 log: %s\n", strerror(errno));
123 logbuffer = mmap(NULL, logbuffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
124 if (logbuffer == MAP_FAILED) {
125 close(logbuffer_shm);
126 shm_unlink("/i3-log-");
127 ELOG("Could not mmap SHM segment for the i3 log: %s\n", strerror(errno));
131 logwalk = logbuffer + sizeof(i3_shmlog_header);
132 loglastwrap = logbuffer + logbuffer_size;
135 atexit(purge_zerobyte_logfile);
139 * Set verbosity of i3. If verbose is set to true, informative messages will
140 * be printed to stdout. If verbose is set to false, only errors will be
144 void set_verbosity(bool _verbose) {
152 void set_debug_logging(const bool _debug_logging) {
153 debug_logging = _debug_logging;
157 * Logs the given message to stdout (if print is true) while prefixing the
158 * current time to it. Additionally, the message will be saved in the i3 SHM
160 * This is to be called by *LOG() which includes filename/linenumber/function.
163 static void vlog(const bool print, const char *fmt, va_list args) {
164 /* Precisely one page to not consume too much memory but to hold enough
165 * data to be useful. */
166 static char message[4096];
167 static struct tm result;
169 static struct tm *tmp;
172 /* Get current time */
174 /* Convert time to local time (determined by the locale) */
175 tmp = localtime_r(&t, &result);
176 /* Generate time prefix */
177 len = strftime(message, sizeof(message), "%x %X - ", tmp);
182 * true true format message, save, print
183 * true false format message, save
184 * false true print message only
185 * false false INVALID, never called
190 gettimeofday(&tv, NULL);
191 printf("%s%d.%d - ", message, tv.tv_sec, tv.tv_usec);
193 printf("%s", message);
197 len += vsnprintf(message + len, sizeof(message) - len, fmt, args);
198 if (len >= sizeof(message)) {
199 fprintf(stderr, "BUG: single log message > 4k\n");
202 /* If there is no space for the current message (plus trailing
203 * nullbyte) in the ringbuffer, we need to wrap and write to the
204 * beginning again. */
205 if ((len+1) >= (logbuffer_size - (logwalk - logbuffer))) {
206 loglastwrap = logwalk;
207 logwalk = logbuffer + sizeof(i3_shmlog_header);
210 /* Copy the buffer, terminate it, move the write pointer to the byte after
211 * our current message. */
212 strncpy(logwalk, message, len);
219 fwrite(message, len, 1, stdout);
224 * Logs the given message to stdout while prefixing the current time to it,
225 * but only if verbose mode is activated.
228 void verboselog(char *fmt, ...) {
231 if (!logbuffer && !verbose)
235 vlog(verbose, fmt, args);
240 * Logs the given message to stdout while prefixing the current time to it.
243 void errorlog(char *fmt, ...) {
247 vlog(true, fmt, args);
250 /* also log to the error logfile, if opened */
252 vfprintf(errorfile, fmt, args);
258 * Logs the given message to stdout while prefixing the current time to it,
259 * but only if debug logging was activated.
260 * This is to be called by DLOG() which includes filename/linenumber
263 void debuglog(char *fmt, ...) {
266 if (!logbuffer && !(debug_logging))
270 vlog(debug_logging, fmt, args);
275 * Deletes the unused log files. Useful if i3 exits immediately, eg.
276 * because --get-socketpath was called. We don't care for syscall
277 * failures. This function is invoked automatically when exiting.
279 void purge_zerobyte_logfile(void) {
286 /* don't delete the log file if it contains something */
287 if ((stat(errorfilename, &st)) == -1 || st.st_size > 0)
290 if (unlink(errorfilename) == -1)
293 if ((slash = strrchr(errorfilename, '/')) != NULL) {
295 /* possibly fails with ENOTEMPTY if there are files (or
297 rmdir(errorfilename);