]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libcommon: add common debugging routines
authorOndrej Oprala <ooprala@redhat.com>
Tue, 11 Mar 2014 10:27:13 +0000 (11:27 +0100)
committerOndrej Oprala <ooprala@redhat.com>
Wed, 12 Mar 2014 13:37:41 +0000 (14:37 +0100)
Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
include/Makemodule.am
include/debug.h [new file with mode: 0644]

index a93be1973c7e1ca6fa8c5199550b655e8c8da5c4..53e0ce45444cd1a69652f401841fe45a630e5bfe 100644 (file)
@@ -12,6 +12,7 @@ dist_noinst_HEADERS += \
        include/cpuset.h \
        include/crc32.h \
        include/crc64.h \
+       include/debug.h \
        include/env.h \
        include/exec_shell.h \
        include/exitcodes.h \
diff --git a/include/debug.h b/include/debug.h
new file mode 100644 (file)
index 0000000..51d09b8
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
+ *
+ * This file may be distributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+#ifndef UTIL_LINUX_DEBUG_H
+#define UTIL_LINUX_DEBUG_H
+
+#define _UL_DEBUG
+#ifdef _UL_DEBUG
+
+#include <stdarg.h>
+
+# define __UL_DBG(l, p, m, x) \
+                       do { \
+                               if ((p ## m) & l ## _debug_mask) { \
+                                       fprintf(stderr, "%d: %s: %8s: ", getpid(), # l, # m); \
+                                       x; \
+                               } \
+                       } while (0)
+
+#define __UL_INIT_DEBUG(lib, pref, mask, env) do { \
+       if (lib ## _debug_mask & pref ## INIT) \
+       ; \
+       else if (!mask) { \
+               char *str = getenv(# env); \
+               if (str) \
+                       lib ## _debug_mask = strtoul(str, 0, 0); \
+       } else \
+               lib ## _debug_mask = mask; \
+       lib ## _debug_mask |= pref ## INIT; \
+       if (lib ## _debug_mask != pref ## INIT) { \
+               const char *ver = NULL; \
+               __UL_DBG(lib, pref, INIT, ul_debug("library debug mask: 0x%04x", \
+                               lib ## _debug_mask)); \
+               /* ul_get_library_version(&ver); \
+               __UL_DBG(lib, pref, INIT, ul_debug("library version: %s", ver));*/ \
+       } \
+} while (0)
+
+static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
+ul_debug(const char *mesg, ...)
+{
+       va_list ap;
+       va_start(ap, mesg);
+       vfprintf(stderr, mesg, ap);
+       va_end(ap);
+       fputc('\n', stderr);
+}
+
+static inline void __attribute__ ((__format__ (__printf__, 2, 3)))
+ul_debugobj(void *handler, const char *mesg, ...)
+{
+       va_list ap;
+
+       if (handler)
+               fprintf(stderr, "[%p]: ", handler);
+       va_start(ap, mesg);
+       vfprintf(stderr, mesg, ap);
+       va_end(ap);
+       fputc('\n', stderr);
+}
+
+#else /* !_UL_DEBUG */
+# define __UL_DBG(l, p, m, x) do { ; } while (0)
+#define __UL_INIT_DEBUG(lib, pref, mask, env) do { ; } while (0)
+#endif
+
+#endif /* UTIL_LINUX_DEBUG_H */