2 #define I3__FILE__ "log.c"
24 #if defined(__APPLE__)
25 #include <sys/types.h>
26 #include <sys/sysctl.h>
84 fprintf(stderr,
"Could not initialize errorlog\n");
87 if (fcntl(fileno(
errorfile), F_SETFD, FD_CLOEXEC)) {
88 fprintf(stderr,
"Could not set close-on-exec flag\n");
111 long long physical_mem_bytes;
112 #if defined(__APPLE__)
113 int mib[2] = { CTL_HW, HW_MEMSIZE };
114 size_t length =
sizeof(
long long);
115 sysctl(mib, 2, &physical_mem_bytes, &length, NULL, 0);
117 physical_mem_bytes = (
long long)sysconf(_SC_PHYS_PAGES) *
118 sysconf(_SC_PAGESIZE);
121 #if defined(__FreeBSD__)
128 fprintf(stderr,
"Could not shm_open SHM segment for the i3 log: %s\n", strerror(errno));
132 #if defined(__OpenBSD__) || defined(__APPLE__)
134 fprintf(stderr,
"Could not ftruncate SHM segment for the i3 log: %s\n", strerror(errno));
138 fprintf(stderr,
"Could not ftruncate SHM segment for the i3 log: %s\n", strerror(ret));
148 fprintf(stderr,
"Could not mmap SHM segment for the i3 log: %s\n", strerror(errno));
157 pthread_condattr_t cond_attr;
158 pthread_condattr_init(&cond_attr);
159 if (pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED) != 0)
160 fprintf(stderr,
"pthread_condattr_setpshared() failed, i3-dump-log -f will not work!\n");
161 pthread_cond_init(&(header->
condvar), &cond_attr);
212 static void vlog(
const bool print,
const char *fmt, va_list args) {
215 static char message[4096];
216 static struct tm result;
218 static struct tm *tmp;
224 tmp = localtime_r(&t, &result);
226 len = strftime(message,
sizeof(message),
"%x %X - ", tmp);
239 gettimeofday(&tv, NULL);
240 printf(
"%s%d.%d - ", message, tv.tv_sec, tv.tv_usec);
242 printf(
"%s", message);
246 len += vsnprintf(message + len,
sizeof(message) - len, fmt, args);
247 if (len >=
sizeof(message)) {
248 fprintf(stderr,
"BUG: single log message > 4k\n");
262 strncpy(
logwalk, message, len);
268 pthread_cond_broadcast(&(header->
condvar));
271 fwrite(message, len, 1, stdout);
299 vlog(
true, fmt, args);