From 18f1f68be75b3b157ff3cb94b0e33e63575cb897 Mon Sep 17 00:00:00 2001
From: Sam Hocevar <sam@hocevar.net>
Date: Sun, 12 Nov 2006 13:11:01 +0000
Subject: [PATCH]   * Add a debug() function and a --enable-debug configure
 flag.

---
 common.h       | 25 +++++++++++++++++++++++++
 configure.ac   |  6 ++++++
 cucul/export.c | 14 ++++++++++++++
 3 files changed, 45 insertions(+)

diff --git a/common.h b/common.h
index 34827db..daef8d4 100644
--- a/common.h
+++ b/common.h
@@ -32,6 +32,31 @@ typedef long int intptr_t;
 typedef unsigned long int uintptr_t;
 #endif
 
+#if defined DEBUG && !defined __KERNEL__
+#   include <stdio.h>
+#   include <stdarg.h>
+#   if defined(HAVE_ERRNO_H)
+#       include <errno.h>
+#   endif
+static inline void debug(const char *format, ...)
+{
+#   if defined(HAVE_ERRNO_H)
+    int saved_errno = errno;
+#   endif
+    va_list args;
+    va_start(args, format);
+    fprintf(stderr, "** libcaca debug ** ");
+    vfprintf(stderr, format, args);
+    fprintf(stderr, "\n");
+    va_end(args);
+#   if defined(HAVE_ERRNO_H)
+    errno = saved_errno;
+#   endif
+}
+#else
+#   define debug(format, ...) do {} while(0)
+#endif
+
 #if defined HAVE_HTONS
 #   if defined __KERNEL__
         /* Nothing to do */
diff --git a/configure.ac b/configure.ac
index a8389b7..79d166f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -62,6 +62,8 @@ AC_ARG_ENABLE(imlib2,
   [  --enable-imlib2         Imlib2 graphics support (default enabled)])
 
 dnl conditional builds
+AC_ARG_ENABLE(debug,
+  [  --enable-debug          build debug versions of the library])
 AC_ARG_ENABLE(plugins,
   [  --enable-plugins        build X11 and GL drivers as plugins])
 AC_ARG_ENABLE(doc,
@@ -226,6 +228,10 @@ if test "${enable_vga}" = "yes"; then
 fi
 AM_CONDITIONAL(USE_KERNEL, test "${ac_cv_my_have_vga}" = "yes")
 
+if test "${enable_debug}" = "yes"; then
+  AC_DEFINE(DEBUG, 1, Define to 1 to activate debug)
+fi
+
 if test "${enable_plugins}" = "yes"; then
   ac_cv_my_have_plugins="yes"
   AC_DEFINE(USE_PLUGINS, 1, Define to 1 to activate plugins)
diff --git a/cucul/export.c b/cucul/export.c
index b2b3574..dbf1a9b 100644
--- a/cucul/export.c
+++ b/cucul/export.c
@@ -276,6 +276,8 @@ static void *export_utf8(cucul_canvas_t *cv, unsigned long int *bytes, int cr)
     }
 
     /* Crop to really used size */
+    debug("utf8 export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);
 
@@ -354,6 +356,8 @@ static void *export_ansi(cucul_canvas_t *cv, unsigned long int *bytes)
     }
 
     /* Crop to really used size */
+    debug("ansi export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);
 
@@ -428,6 +432,8 @@ static void *export_html(cucul_canvas_t *cv, unsigned long int *bytes)
     cur += sprintf(cur, "</div></body></html>\n");
 
     /* Crop to really used size */
+    debug("html export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);
 
@@ -521,6 +527,8 @@ static void *export_html3(cucul_canvas_t *cv, unsigned long int *bytes)
     cur += sprintf(cur, "</table>\n");
 
     /* Crop to really used size */
+    debug("html3 export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);
 
@@ -626,6 +634,8 @@ static void *export_irc(cucul_canvas_t *cv, unsigned long int *bytes)
     }
 
     /* Crop to really used size */
+    debug("IRC export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);
 
@@ -733,6 +743,8 @@ static void *export_ps(cucul_canvas_t *cv, unsigned long int *bytes)
     cur += sprintf(cur, "showpage\n");
 
     /* Crop to really used size */
+    debug("PS export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);
 
@@ -817,6 +829,8 @@ static void *export_svg(cucul_canvas_t *cv, unsigned long int *bytes)
     cur += sprintf(cur, "</svg>\n");
 
     /* Crop to really used size */
+    debug("SVG export: alloc %li bytes, realloc %li\n",
+          (long int)*bytes, (long int)(uintptr_t)(cur - data));
     *bytes = (uintptr_t)(cur - data);
     data = realloc(data, *bytes);