From 9f7b1073ad4e1cc8f17e83fb13b85e4a20d5320e Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Tue, 5 Nov 2013 13:42:27 +0300
Subject: add libxml2 error logging.

---
 src/utils/xml.cpp | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index 5f691fd68..a81e1b4ef 100644
--- a/src/utils/xml.cpp
+++ b/src/utils/xml.cpp
@@ -34,9 +34,39 @@
 
 #include "debug.h"
 
-static void xmlNullLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...)
+static void xmlErrorLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...)
+#ifdef __GNUC__
+#ifdef __OpenBSD__
+    __attribute__((__format__(printf, 2, 3)))
+#else
+    __attribute__((__format__(gnu_printf, 2, 3)))
+#endif
+#endif
+;
+
+static void xmlErrorLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...)
 {
-    // Does nothing, that's the whole point of it
+    unsigned size = 1024;
+    const unsigned msgSize = strlen(msg);
+    if (msgSize * 3 > size)
+        size = static_cast<unsigned>(msgSize * 3);
+
+    char* buf = new char[size + 1];
+    va_list ap;
+
+    // Use a temporary buffer to fill in the variables
+    va_start(ap, msg);
+    vsnprintf(buf, size, msg, ap);
+    buf[size] = 0;
+    va_end(ap);
+
+    if (logger)
+        logger->log(buf);
+    else
+        puts(buf);
+
+    // Delete temporary buffer
+    delete [] buf;
 }
 
 namespace XML
@@ -213,7 +243,7 @@ namespace XML
         LIBXML_TEST_VERSION;
 
         // Suppress libxml2 error messages
-        xmlSetGenericErrorFunc(nullptr, &xmlNullLogger);
+        xmlSetGenericErrorFunc(nullptr, &xmlErrorLogger);
     }
 
     // Shutdown libxml
-- 
cgit v1.2.3-70-g09d2