From 14ad2353ccabc330412baff5fe86592f2618cdee Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Thu, 31 Jul 2014 13:23:07 +0200 Subject: libs/debug: accept human readable names for _DEBUG= For example $ LIBMOUNT_DEBUG=tab,cache findmnt to debug only TAB and CACHE subsystem. Signed-off-by: Ondrej Oprala Signed-off-by: Karel Zak --- include/debug.h | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'include/debug.h') diff --git a/include/debug.h b/include/debug.h index 2eb9d4421..1497490b5 100644 --- a/include/debug.h +++ b/include/debug.h @@ -8,6 +8,7 @@ #define UTIL_LINUX_DEBUG_H #include +#include #define UL_DEBUG_DEFINE_MASK(m) int m ## _debug_mask #define UL_DEBUG_DECLARE_MASK(m) extern UL_DEBUG_DEFINE_MASK(m) @@ -47,7 +48,7 @@ else if (!mask) { \ char *str = getenv(# env); \ if (str) \ - lib ## _debug_mask = strtoul(str, 0, 0); \ + lib ## _debug_mask = parse_envmask(lib ## _masknames, str); \ } else \ lib ## _debug_mask = mask; \ lib ## _debug_mask |= pref ## INIT; \ @@ -57,6 +58,8 @@ } \ } while (0) +struct dbg_mask { char *mname; int val; }; + static inline void __attribute__ ((__format__ (__printf__, 1, 2))) ul_debug(const char *mesg, ...) { @@ -80,4 +83,41 @@ ul_debugobj(void *handler, const char *mesg, ...) fputc('\n', stderr); } +static inline int parse_envmask(const struct dbg_mask const flagnames[], + const char *mask) +{ + int res; + char *ptr; + + /* let's check for a numeric mask first */ + res = strtoul(mask, &ptr, 0); + + /* perhaps it's a comma-separated string? */ + if (*ptr != '\0') { + char *msbuf, *ms, *name; + res = 0; + + ms = msbuf = strdup(mask); + if (!ms) + return res; + + while ((name = strtok_r(ms, ",", &ptr))) { + size_t i = 0; + ms = ptr; + + while (flagnames[i].mname) { + if (!strcmp(name, flagnames[i].mname)) { + res |= flagnames[i].val; + break; + } + ++i; + } + /* nothing else we can do by OR-ing the mask */ + if (res == 0xffff) + break; + } + free(msbuf); + } + return res; +} #endif /* UTIL_LINUX_DEBUG_H */ -- cgit v1.2.3-55-g7522