From 2896ae80574c237afb64427a6a5412994c87fa30 Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <michael@stapelberg.de>
Date: Mon, 13 Aug 2012 13:27:00 +0200
Subject: [PATCH] logging: make libi3 use verboselog()/errorlog(), provide it
 in each caller

While this is a bit ugly, it makes the log messages end up where they
are supposed to: in the shmlog/stdout in case of i3 and on stdout in
case of utilities such as i3-input
---
 i3-config-wizard/main.c | 20 ++++++++++++++++++++
 i3-input/main.c         | 20 ++++++++++++++++++++
 i3-nagbar/main.c        | 20 ++++++++++++++++++++
 i3bar/src/main.c        | 22 +++++++++++++++++++++-
 include/libi3.h         |  6 ++++--
 libi3/font.c            |  7 +++----
 6 files changed, 88 insertions(+), 7 deletions(-)

diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c
index 46cf8aa8..679c5e6d 100644
--- a/i3-config-wizard/main.c
+++ b/i3-config-wizard/main.c
@@ -84,6 +84,26 @@ Display *dpy;
 char *rewrite_binding(const char *bindingline);
 static void finish();
 
+/*
+ * Having verboselog() and errorlog() is necessary when using libi3.
+ *
+ */
+void verboselog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stdout, fmt, args);
+    va_end(args);
+}
+
+void errorlog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
+}
+
 /*
  * This function resolves ~ in pathnames.
  * It may resolve wildcards in the first part of the path, but if no match
diff --git a/i3-input/main.c b/i3-input/main.c
index d18502cd..3172387d 100644
--- a/i3-input/main.c
+++ b/i3-input/main.c
@@ -56,6 +56,26 @@ xcb_window_t root;
 xcb_connection_t *conn;
 xcb_screen_t *root_screen;
 
+/*
+ * Having verboselog() and errorlog() is necessary when using libi3.
+ *
+ */
+void verboselog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stdout, fmt, args);
+    va_end(args);
+}
+
+void errorlog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
+}
+
 /*
  * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
  * rendering it (UCS-2) or sending it to i3 (UTF-8).
diff --git a/i3-nagbar/main.c b/i3-nagbar/main.c
index 1588c356..7aee191c 100644
--- a/i3-nagbar/main.c
+++ b/i3-nagbar/main.c
@@ -56,6 +56,26 @@ xcb_window_t root;
 xcb_connection_t *conn;
 xcb_screen_t *root_screen;
 
+/*
+ * Having verboselog() and errorlog() is necessary when using libi3.
+ *
+ */
+void verboselog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stdout, fmt, args);
+    va_end(args);
+}
+
+void errorlog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
+}
+
 /*
  * Starts the given application by passing it through a shell. We use double fork
  * to avoid zombie processes. As the started application’s parent exits (immediately),
diff --git a/i3bar/src/main.c b/i3bar/src/main.c
index e648e00e..ea605647 100644
--- a/i3bar/src/main.c
+++ b/i3bar/src/main.c
@@ -2,7 +2,7 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3bar - an xcb-based status- and ws-bar for i3
- * © 2010-2011 Axel Wagner and contributors (see also: LICENSE)
+ * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
  *
  */
 #include <stdio.h>
@@ -17,6 +17,26 @@
 
 #include "common.h"
 
+/*
+ * Having verboselog() and errorlog() is necessary when using libi3.
+ *
+ */
+void verboselog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stdout, fmt, args);
+    va_end(args);
+}
+
+void errorlog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
+}
+
 /*
  * Glob path, i.e. expand ~
  *
diff --git a/include/libi3.h b/include/libi3.h
index 45ba970f..d4df901f 100644
--- a/include/libi3.h
+++ b/include/libi3.h
@@ -69,10 +69,12 @@ struct Font {
 /* Since this file also gets included by utilities which don’t use the i3 log
  * infrastructure, we define a fallback. */
 #if !defined(LOG)
-#define LOG(fmt, ...) fprintf(stdout, "[libi3] " __FILE__ " " fmt, ##__VA_ARGS__)
+void verboselog(char *fmt, ...);
+#define LOG(fmt, ...) verboselog("[libi3] " __FILE__ " " fmt, ##__VA_ARGS__)
 #endif
 #if !defined(ELOG)
-#define ELOG(fmt, ...) fprintf(stderr, "[libi3] ERROR: " fmt, ##__VA_ARGS__)
+void errorlog(char *fmt, ...);
+#define ELOG(fmt, ...) errorlog("[libi3] ERROR: " fmt, ##__VA_ARGS__)
 #endif
 
 /**
diff --git a/libi3/font.c b/libi3/font.c
index 0688bdbc..08725844 100644
--- a/libi3/font.c
+++ b/libi3/font.c
@@ -38,13 +38,12 @@ static double pango_font_blue;
 static bool load_pango_font(i3Font *font, const char *desc) {
     /* Load the font description */
     font->specific.pango_desc = pango_font_description_from_string(desc);
-    if (!font->specific.pango_desc)
-    {
+    if (!font->specific.pango_desc) {
         ELOG("Could not open font %s with Pango, fallback to X font.\n", desc);
         return false;
     }
 
-    LOG("Using Pango font %s, size %d",
+    LOG("Using Pango font %s, size %d\n",
         pango_font_description_get_family(font->specific.pango_desc),
         pango_font_description_get_size(font->specific.pango_desc)
         );
@@ -186,7 +185,7 @@ i3Font load_font(const char *pattern, const bool fallback) {
         }
     }
 
-    LOG("Using X font %s", pattern);
+    LOG("Using X font %s\n", pattern);
 
     /* Get information (height/name) for this font */
     if (!(font.specific.xcb.info = xcb_query_font_reply(conn, info_cookie, NULL)))