[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] [PATCH 03/16] A simple debugging facility



From: David Lutterkort <lutter redhat com>

Debugging is disabled by default. When configured with --enable-debug=yes,
Augeas will look at runtime at the environment variable AUGEAS_DEBUG, which
should contain a colon-separated list of categories. Besides printing
various thigs on stdout, some debug output is also left in files in the
directory set by the environment variable AUGEAS_DEBUG_DIR, e.g., dot
graphs of various internal data structures.

  * configure.ac: add --enable-debug option
  * src/internal.h (debugging, debug_file): new functions
  * src/internal.c (debugging, debug_file): new functions
---
 Makefile.maint |    4 ++++
 configure.ac   |    9 +++++++++
 src/internal.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/internal.h |   17 +++++++++++++++++
 4 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/Makefile.maint b/Makefile.maint
index cc7e2c5..1e4ec0e 100644
--- a/Makefile.maint
+++ b/Makefile.maint
@@ -28,6 +28,10 @@ upload:
 tag-release:
 	@git tag -s release-$(VERSION)
 
+# Print all the debug categories in use
+debug-categories:
+	@fgrep 'debugging("' src/*.c | sed -r -e 's/^.*debugging\("([^"]+)"\).*$$/\1/' | sort -u
+
 # This is how I run autogen.sh locally
 autogen:
 	./autogen.sh CFLAGS=-g --prefix=/data/share/ --gnulib-srcdir=${HOME}/code/gnulib/ --enable-compile-warnings=error
diff --git a/configure.ac b/configure.ac
index 39d007a..e43b65c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -55,6 +55,15 @@ AC_ARG_WITH([failmalloc],
 
 AM_CONDITIONAL([WITH_FAILMALLOC], [test x$with_failmalloc != xno])
 
+dnl --enable-debug=(yes|no)
+AC_ARG_ENABLE([debug],
+              [AC_HELP_STRING([--enable-debug=no/yes],
+                             [enable debugging output])],[],[enable_debug=yes])
+AM_CONDITIONAL([ENABLE_DEBUG], test x"$enable_debug" = x"yes")
+if test x"$enable_debug" = x"yes"; then
+   AC_DEFINE([ENABLE_DEBUG], [1], [whether debugging is enabled])
+fi
+
 dnl Version info in libtool's notation
 AC_SUBST([LIBAUGEAS_VERSION_INFO], [10:0:10])
 AC_SUBST([LIBFA_VERSION_INFO], [3:0:2])
diff --git a/src/internal.c b/src/internal.c
index 318c79a..77f2fd7 100644
--- a/src/internal.c
+++ b/src/internal.c
@@ -438,6 +438,53 @@ int regexp_c_locale(char **u, size_t *len) {
 }
 #endif
 
+#if ENABLE_DEBUG
+bool debugging(const char *category) {
+    const char *debug = getenv("AUGEAS_DEBUG");
+    const char *s;
+
+    if (debug == NULL)
+        return false;
+
+    for (s = debug; s != NULL; ) {
+        if (STREQLEN(s, category, strlen(category)))
+            return true;
+        s = strchr(s, ':');
+        if (s != NULL)
+            s+=1;
+    }
+    return false;
+}
+
+FILE *debug_fopen(const char *format, ...) {
+    va_list ap;
+    FILE *result = NULL;
+    const char *dir;
+    char *name = NULL, *path = NULL;
+    int r;
+
+    dir = getenv("AUGEAS_DEBUG_DIR");
+    if (dir == NULL)
+        goto error;
+
+    va_start(ap, format);
+    r = vasprintf(&name, format, ap);
+    va_end(ap);
+    if (r < 0)
+        goto error;
+
+    r = xasprintf(&path, "%s/%s", dir, name);
+    if (r < 0)
+        goto error;
+
+    result = fopen(path, "w");
+
+ error:
+    free(name);
+    free(path);
+    return result;
+}
+#endif
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/src/internal.h b/src/internal.h
index 732a31e..c3ed984 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -472,6 +472,23 @@ int pathx_symtab_undefine(struct pathx_symtab **symtab, const char *name);
 void pathx_symtab_remove_descendants(struct pathx_symtab *symtab,
                                      const struct tree *tree);
 void free_symtab(struct pathx_symtab *symtab);
+
+/* Debug helpers, all defined in internal.c. When ENABLE_DEBUG is not
+ * set, they compile to nothing.
+ */
+#  if ENABLE_DEBUG
+  /* Return true if debugging for CATEGORY is turned on */
+  bool debugging(const char *category);
+  /* Format the arguments into a file name, prepend it with the directory
+   * from the environment variable AUGEAS_DEBUG_DIR, and open the file for
+   * writing.
+  */
+  FILE *debug_fopen(const char *format, ...)
+    ATTRIBUTE_FORMAT(printf, 1, 2);
+#  else
+#    define debugging(facility) (0)
+#    define debug_fopen(format ...) (NULL)
+#  endif
 #endif
 
 
-- 
1.6.5.2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]