]> git.sur5r.net Git - i3/i3/blobdiff - src/log.c
Merge branch 'tree' into next
[i3/i3] / src / log.c
index 0371e9be618eba73a315490ff3e2bcc3c3671868..22b7fffe8774986b63fabce036f0cbfcde597cc0 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -14,6 +14,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdbool.h>
+#include <stdlib.h>
+#include <sys/time.h>
 
 #include "util.h"
 #include "log.h"
 
 static uint64_t loglevel = 0;
 static bool verbose = true;
+static FILE *errorfile;
+char *errorfilename;
+
+/*
+ * Initializes logging by creating an error logfile in /tmp (or
+ * XDG_RUNTIME_DIR, see get_process_filename()).
+ *
+ */
+void init_logging() {
+    errorfilename = get_process_filename("errorlog");
+    if (errorfilename == NULL) {
+        ELOG("Could not initialize errorlog\n");
+        return;
+    }
+
+    errorfile = fopen(errorfilename, "w");
+}
 
 /*
  * Set verbosity of i3. If verbose is set to true, informative messages will
@@ -71,7 +90,13 @@ void vlog(char *fmt, va_list args) {
     struct tm *tmp = localtime(&t);
     /* Generate time prefix */
     strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
+#ifdef DEBUG_TIMING
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
+#else
     printf("%s", timebuf);
+#endif
     vprintf(fmt, args);
 }
 
@@ -101,6 +126,12 @@ void errorlog(char *fmt, ...) {
     va_start(args, fmt);
     vlog(fmt, args);
     va_end(args);
+
+    /* also log to the error logfile, if opened */
+    va_start(args, fmt);
+    vfprintf(errorfile, fmt, args);
+    fflush(errorfile);
+    va_end(args);
 }
 
 /*