[augeas-devel] augeas: master - A simple debug facility

David Lutterkort lutter at fedoraproject.org
Fri Jan 15 01:31:18 UTC 2010


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=dd26b7c77331b5beb6e319bc168da05c953443ae
Commit:        dd26b7c77331b5beb6e319bc168da05c953443ae
Parent:        919db52da611be583c0ab9371bbaae721268e4f6
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Fri Dec 18 15:31:39 2009 -0800
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Thu Jan 14 14:48:38 2010 -0800

A simple debug facility

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 |    6 +++++-
 configure.ac   |    9 +++++++++
 src/internal.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/internal.h |   17 +++++++++++++++++
 4 files changed, 78 insertions(+), 1 deletions(-)

diff --git a/Makefile.maint b/Makefile.maint
index cc7e2c5..55f8ae2 100644
--- a/Makefile.maint
+++ b/Makefile.maint
@@ -28,8 +28,12 @@ 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
+	./autogen.sh CFLAGS=-g --prefix=/data/share/ --gnulib-srcdir=${HOME}/code/gnulib/ --enable-compile-warnings=error --enable-debug=yes
 
 .PHONY: build-rpm
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 a50025d..0489dd5 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 7daa43a..c9a8de8 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -495,6 +495,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
 
 




More information about the augeas-devel mailing list