]> git.sur5r.net Git - i3/i3/blobdiff - src/log.c
Merge branch 'master' into next
[i3/i3] / src / log.c
index 14819e9e13d646f63f80f4c19430d0484a712300..7b7eca5045e7b22537d71806ccb2249e9672747a 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -23,6 +23,7 @@
 #include "log.h"
 #include "i3.h"
 #include "libi3.h"
+#include "shmlog.h"
 
 /* loglevels.h is autogenerated at make time */
 #include "loglevels.h"
@@ -52,6 +53,20 @@ static int logbuffer_size;
 /* 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()).
@@ -103,8 +118,9 @@ void init_logging() {
             logbuffer = NULL;
             return;
         }
-        logwalk = logbuffer;
+        logwalk = logbuffer + sizeof(i3_shmlog_header);
         loglastwrap = logbuffer + logbuffer_size;
+        store_log_markers();
     }
 }
 
@@ -141,17 +157,6 @@ void add_loglevel(const char *level) {
     }
 }
 
-/*
- * 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
@@ -208,7 +213,7 @@ static void vlog(const bool print, const char *fmt, va_list args) {
          * 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
@@ -217,6 +222,8 @@ static void vlog(const bool print, const char *fmt, va_list args) {
         logwalk[len] = '\0';
         logwalk += len + 1;
 
+        store_log_markers();
+
         if (print)
             fwrite(message, len, 1, stdout);
     }