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));
135 fprintf(stderr,
"Could not ftruncate SHM segment for the i3 log: %s\n", strerror(errno));
142 fprintf(stderr,
"Could not mmap SHM segment for the i3 log: %s\n", strerror(errno));
151 pthread_condattr_t cond_attr;
152 pthread_condattr_init(&cond_attr);
153 if (pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED) != 0)
154 fprintf(stderr,
"pthread_condattr_setpshared() failed, i3-dump-log -f will not work!\n");
155 pthread_cond_init(&(header->
condvar), &cond_attr);
206 static void vlog(
const bool print,
const char *fmt, va_list args) {
209 static char message[4096];
210 static struct tm result;
212 static struct tm *tmp;
218 tmp = localtime_r(&t, &result);
220 len = strftime(message,
sizeof(message),
"%x %X - ", tmp);
233 gettimeofday(&tv, NULL);
234 printf(
"%s%d.%d - ", message, tv.tv_sec, tv.tv_usec);
236 printf(
"%s", message);
240 len += vsnprintf(message + len,
sizeof(message) - len, fmt, args);
241 if (len >=
sizeof(message)) {
242 fprintf(stderr,
"BUG: single log message > 4k\n");
256 strncpy(
logwalk, message, len);
262 pthread_cond_broadcast(&(header->
condvar));
265 fwrite(message, len, 1, stdout);
293 vlog(
true, fmt, args);