]> err.no Git - util-linux/commitdiff
libmount: add debug support
authorKarel Zak <kzak@redhat.com>
Thu, 26 Nov 2009 23:33:37 +0000 (00:33 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Jun 2010 13:20:10 +0000 (15:20 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
shlibs/mount/src/Makefile.am
shlibs/mount/src/init.c [new file with mode: 0644]
shlibs/mount/src/mountP.h
shlibs/mount/src/test.c [new file with mode: 0644]

index 2ac06bd35d4ddab5257dca190039b2cbf973fa18..0483367a3dc26409200c32498049eac79ecd2342 100644 (file)
@@ -11,9 +11,7 @@ nodist_mountinc_HEADERS = mount.h
 usrlib_exec_LTLIBRARIES = libmount.la
 libmount_la_SOURCES = $(mountinc_HEADERS)
 
-nodist_libmount_la_SOURCES = mount.h \
-                            version.c \
-                            utils.c
+nodist_libmount_la_SOURCES = mount.h version.c utils.c test.c init.c
 
 libmount_la_LIBADD = $(ul_libblkid_la)
 
diff --git a/shlibs/mount/src/init.c b/shlibs/mount/src/init.c
new file mode 100644 (file)
index 0000000..33da47b
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+
+#include <stdlib.h>
+
+#include "mountP.h"
+
+#ifdef CONFIG_LIBMOUNT_DEBUG
+int libmount_debug_mask;
+
+void mnt_init_debug(int mask)
+{
+       if (libmount_debug_mask & DEBUG_INIT)
+               return;
+       if (!mask) {
+               char *str = mnt_getenv_safe("LIBMOUNT_DEBUG");
+               if (str)
+                       libmount_debug_mask = strtoul(str, 0, 0);
+       } else
+               libmount_debug_mask = mask;
+
+       if (libmount_debug_mask)
+               printf("libmount: debug mask set to 0x%04x.\n",
+                               libmount_debug_mask);
+       libmount_debug_mask |= DEBUG_INIT;
+}
+#endif
index 172341349d3fc141e570a8df5e91db6215731ebc..5ac3b6bc01cdfc37c5614f7b0d4fe98e6fa2ff8c 100644 (file)
 /* features */
 #define CONFIG_CDROM_NOMEDIUM_RETRIES    5
 #define CONFIG_LIBMOUNT_ASSERT
+#define CONFIG_LIBMOUNT_DEBUG
 
 #ifdef CONFIG_LIBMOUNT_ASSERT
 #include <assert.h>
 #endif
 
+/*
+ * Debug
+ */
+#if defined(TEST_PROGRAM) && !defined(LIBMOUNT_DEBUG)
+#define CONFIG_LIBMOUNT_DEBUG
+#endif
+
+#define DEBUG_INIT     (1 << 1)
+#define DEBUG_ALL      0xFFFF
+
+#ifdef CONFIG_LIBMOUNT_DEBUG
+#include <stdio.h>
+extern int libmount_debug_mask;
+extern void mnt_init_debug(int mask);
+#define DBG(m,x)       if ((m) & libmount_debug_mask) x;
+#else
+#define DBG(m,x)
+#define mnt_init_debug(x)
+#endif
+
+#ifdef TEST_PROGRAM
+struct mtest {
+       const char      *name;
+       int             (*body)(struct mtest *ts, int argc, char *argv[]);
+       const char      *usage;
+};
+
+/* utils.c */
+extern int mnt_run_test(struct mtest *tests, int argc, char *argv[]);
+#endif
+
 /* utils.c */
 extern char *mnt_getenv_safe(const char *arg);
 #ifndef HAVE_STRNLEN
diff --git a/shlibs/mount/src/test.c b/shlibs/mount/src/test.c
new file mode 100644 (file)
index 0000000..eb4f2d1
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ *
+ * Routines for TEST_PROGRAMs
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifndef TEST_PROGRAM
+#define TEST_PROGRAM
+#endif
+
+#include "mountP.h"
+
+int mnt_run_test(struct mtest *tests, int argc, char *argv[])
+{
+       int rc = -1;
+       struct mtest *ts;
+
+       assert(tests);
+       assert(argc);
+       assert(argv);
+
+       if (argc < 2 ||
+           strcmp(argv[1], "--help") == 0 ||
+           strcmp(argv[1], "-h") == 0)
+               goto usage;
+
+       mnt_init_debug(0);
+
+       for (ts = tests; ts->name; ts++) {
+               if (strcmp(ts->name, argv[1]) == 0) {
+                       rc = ts->body(ts, argc - 1, argv + 1);
+                       if (rc)
+                               printf("FAILED [rc=%d]", rc);
+                       break;
+               }
+       }
+
+       if (rc == -1 && ts->name == NULL)
+               goto usage;
+
+       return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+usage:
+       printf("\nUsage:\n\t%s <test> [testoptions]\nTests:\n",
+                       program_invocation_short_name);
+       for (ts = tests; ts->name; ts++) {
+               printf("\t%-15s", ts->name);
+               if (ts->usage)
+                       printf(" %s\n", ts->usage);
+       }
+       printf("\n");
+       return EXIT_FAILURE;
+}