#include "log.h"
#include "i3.h"
#include "libi3.h"
+#include "shmlog.h"
/* loglevels.h is autogenerated at make time */
#include "loglevels.h"
/* File descriptor for shm_open. */
static int logbuffer_shm;
+/*
+ * Writes the offsets for the next write and for the last wrap to the
+ * shmlog_header.
+ * Necessary to print the i3 SHM log in the correct order.
+ *
+ */
+static void store_log_markers() {
+ i3_shmlog_header *header = (i3_shmlog_header*)logbuffer;
+
+ header->offset_next_write = (logwalk - logbuffer);
+ header->offset_last_wrap = (loglastwrap - logbuffer);
+ header->size = logbuffer_size;
+}
+
/*
* Initializes logging by creating an error logfile in /tmp (or
* XDG_RUNTIME_DIR, see get_process_filename()).
logbuffer = NULL;
return;
}
- logwalk = logbuffer;
+ logwalk = logbuffer + sizeof(i3_shmlog_header);
loglastwrap = logbuffer + logbuffer_size;
+ store_log_markers();
}
}
}
}
-/*
- * Returns the offsets for the next write and for the last wrap.
- * Necessary to print the i3 SHM log in the correct order.
- *
- */
-void get_log_markers(int *offset_next_write, int *offset_last_wrap, int *size) {
- *offset_next_write = (logwalk - logbuffer);
- *offset_last_wrap = (loglastwrap - logbuffer);
- *size = logbuffer_size;
-}
-
/*
* Logs the given message to stdout (if print is true) while prefixing the
* current time to it. Additionally, the message will be saved in the i3 SHM
vprintf(fmt, args);
} else {
len += vsnprintf(message + len, sizeof(message) - len, fmt, args);
- if (len == sizeof(message)) {
+ if (len < 0 ) {
+ fprintf(stderr, "BUG: something is overflowing here. Dropping the log entry\n");
+ return;
+ }
+
+ if (len >= sizeof(message)) {
fprintf(stderr, "BUG: single log message > 4k\n");
}
+
/* If there is no space for the current message (plus trailing
* nullbyte) in the ringbuffer, we need to wrap and write to the
* beginning again. */
if ((len+1) >= (logbuffer_size - (logwalk - logbuffer))) {
loglastwrap = logwalk;
- logwalk = logbuffer;
+ logwalk = logbuffer + sizeof(i3_shmlog_header);
}
/* Copy the buffer, terminate it, move the write pointer to the byte after
logwalk[len] = '\0';
logwalk += len + 1;
+ store_log_markers();
+
if (print)
fwrite(message, len, 1, stdout);
}