summaryrefslogtreecommitdiffstats
path: root/include/debug.h
diff options
context:
space:
mode:
authorKarel Zak2014-03-21 10:59:58 +0100
committerKarel Zak2014-03-21 10:59:58 +0100
commit9a7769141a71fdccae518e74ac5657c19c78b653 (patch)
tree9f7f657e87f13c166cf41ea80e95af71c3a2058f /include/debug.h
parentbuild-sys: gettexts 0.18 -> 0.18.2 due to MKDIR_P (diff)
parentclean up redundant macros and defines (diff)
downloadkernel-qcow2-util-linux-9a7769141a71fdccae518e74ac5657c19c78b653.tar.gz
kernel-qcow2-util-linux-9a7769141a71fdccae518e74ac5657c19c78b653.tar.xz
kernel-qcow2-util-linux-9a7769141a71fdccae518e74ac5657c19c78b653.zip
Merge branch 'common_debug' of https://github.com/ooprala/util-linux
* 'common_debug' of https://github.com/ooprala/util-linux: clean up redundant macros and defines libfdisk: use include/debug.h libblkid: use include/debug.h libmount: further debug.h integration libcommon: don't mention lib versions in debug macros libcommon: define more debugging macros libmount: use macros from include/debug.h libcommon: add common debugging routines
Diffstat (limited to 'include/debug.h')
-rw-r--r--include/debug.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/include/debug.h b/include/debug.h
new file mode 100644
index 000000000..dfb640f6b
--- /dev/null
+++ b/include/debug.h
@@ -0,0 +1,66 @@
+/*
+ * 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
+
+#include <stdarg.h>
+
+#define UL_DEBUG_DEFINE_MASK(m) int m ## _debug_mask
+#define UL_DEBUG_DECLARE_MASK(m) extern UL_DEBUG_DEFINE_MASK(m)
+
+/* p - flag prefix, m - flag postfix */
+#define UL_DEBUG_DEFINE_FLAG(p, m) p ## m
+
+/* l - library name, p - flag prefix, m - flag postfix, x - function */
+# 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) { \
+ __UL_DBG(lib, pref, INIT, ul_debug("library debug mask: 0x%04x", \
+ lib ## _debug_mask)); \
+ } \
+} 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);
+}
+
+#endif /* UTIL_LINUX_DEBUG_H */