Skip to content

Commit

Permalink
include/debug: introduce __UL_INIT_DEBUG_FROM_STRING()
Browse files Browse the repository at this point in the history
Let's make it possible to use debug.h without environment variables.

Suggested-by: J William Piggott <elseifthen@gmx.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Jan 17, 2018
1 parent de4568f commit a15dca2
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion disk-utils/cfdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ UL_DEBUG_DEFINE_MASKNAMES(cfdisk) = UL_DEBUG_EMPTY_MASKNAMES;

static void cfdisk_init_debug(void)
{
__UL_INIT_DEBUG(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
}

/* Initialize output columns -- we follow libfdisk fields (usually specific
Expand Down
2 changes: 1 addition & 1 deletion disk-utils/fdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ UL_DEBUG_DEFINE_MASKNAMES(fdisk) = UL_DEBUG_EMPTY_MASKNAMES;

static void fdiskprog_init_debug(void)
{
__UL_INIT_DEBUG(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG);
}

static void reply_sighandler(int sig __attribute__((unused)))
Expand Down
2 changes: 1 addition & 1 deletion disk-utils/sfdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct sfdisk {

static void sfdiskprog_init_debug(void)
{
__UL_INIT_DEBUG(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
}


Expand Down
19 changes: 12 additions & 7 deletions include/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,12 @@ struct ul_debug_maskname {
} \
} while (0)


#define __UL_INIT_DEBUG(lib, pref, mask, env) \
#define __UL_INIT_DEBUG_FROM_STRING(lib, pref, mask, str) \
do { \
if (lib ## _debug_mask & pref ## INIT) \
; \
else if (!mask) { \
char *str = getenv(# env); \
if (str) \
lib ## _debug_mask = ul_debug_parse_envmask(lib ## _masknames, str); \
else if (!mask && str) { \
lib ## _debug_mask = ul_debug_parse_mask(lib ## _masknames, str); \
} else \
lib ## _debug_mask = mask; \
if (lib ## _debug_mask) { \
Expand All @@ -104,6 +101,14 @@ struct ul_debug_maskname {
} while (0)


#define __UL_INIT_DEBUG_FROM_ENV(lib, pref, mask, env) \
do { \
const char *envstr = mask ? NULL : getenv(# env); \
__UL_INIT_DEBUG_FROM_STRING(lib, pref, mask, envstr); \
} while (0)



static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
ul_debug(const char *mesg, ...)
{
Expand All @@ -114,7 +119,7 @@ ul_debug(const char *mesg, ...)
fputc('\n', stderr);
}

static inline int ul_debug_parse_envmask(
static inline int ul_debug_parse_mask(
const struct ul_debug_maskname flagnames[],
const char *mask)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ static int colors_read_schemes(struct ul_color_ctl *cc)

static void termcolors_init_debug(void)
{
__UL_INIT_DEBUG(termcolors, TERMCOLORS_DEBUG_, 0, TERMINAL_COLORS_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(termcolors, TERMCOLORS_DEBUG_, 0, TERMINAL_COLORS_DEBUG);
}

static int colors_terminal_is_ready(void)
Expand Down
2 changes: 1 addition & 1 deletion lib/loopdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static void loopdev_init_debug(void)
{
if (loopdev_debug_mask)
return;
__UL_INIT_DEBUG(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion libblkid/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void blkid_init_debug(int mask)
if (libblkid_debug_mask)
return;

__UL_INIT_DEBUG(libblkid, BLKID_DEBUG_, mask, LIBBLKID_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(libblkid, BLKID_DEBUG_, mask, LIBBLKID_DEBUG);

if (libblkid_debug_mask != BLKID_DEBUG_INIT
&& libblkid_debug_mask != (BLKID_DEBUG_HELP|BLKID_DEBUG_INIT)) {
Expand Down
2 changes: 1 addition & 1 deletion libfdisk/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void fdisk_init_debug(int mask)
if (libfdisk_debug_mask)
return;

__UL_INIT_DEBUG(libfdisk, LIBFDISK_DEBUG_, mask, LIBFDISK_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(libfdisk, LIBFDISK_DEBUG_, mask, LIBFDISK_DEBUG);


if (libfdisk_debug_mask != LIBFDISK_DEBUG_INIT
Expand Down
2 changes: 1 addition & 1 deletion libmount/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void mnt_init_debug(int mask)
if (libmount_debug_mask)
return;

__UL_INIT_DEBUG(libmount, MNT_DEBUG_, mask, LIBMOUNT_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(libmount, MNT_DEBUG_, mask, LIBMOUNT_DEBUG);

if (libmount_debug_mask != MNT_DEBUG_INIT
&& libmount_debug_mask != (MNT_DEBUG_HELP|MNT_DEBUG_INIT)) {
Expand Down
2 changes: 1 addition & 1 deletion libsmartcols/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void scols_init_debug(int mask)
if (libsmartcols_debug_mask)
return;

__UL_INIT_DEBUG(libsmartcols, SCOLS_DEBUG_, mask, LIBSMARTCOLS_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(libsmartcols, SCOLS_DEBUG_, mask, LIBSMARTCOLS_DEBUG);

if (libsmartcols_debug_mask != SCOLS_DEBUG_INIT
&& libsmartcols_debug_mask != (SCOLS_DEBUG_HELP|SCOLS_DEBUG_INIT)) {
Expand Down
2 changes: 1 addition & 1 deletion login-utils/su-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ su_catch_sig(int sig)

static void su_init_debug(void)
{
__UL_INIT_DEBUG(su, SU_DEBUG_, 0, SU_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(su, SU_DEBUG_, 0, SU_DEBUG);
}

static void init_tty(struct su_context *su)
Expand Down
2 changes: 1 addition & 1 deletion misc-utils/lsblk.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ struct blkdev_cxt {

static void lsblk_init_debug(void)
{
__UL_INIT_DEBUG(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
}

static int is_maj_excluded(int maj)
Expand Down
2 changes: 1 addition & 1 deletion misc-utils/whereis.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static const char *srcdirs[] = {

static void whereis_init_debug(void)
{
__UL_INIT_DEBUG(whereis, WHEREIS_DEBUG_, 0, WHEREIS_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(whereis, WHEREIS_DEBUG_, 0, WHEREIS_DEBUG);
}

static const char *whereis_type_to_name(int type)
Expand Down
2 changes: 1 addition & 1 deletion sys-utils/lsns.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static int netlink_fd = -1;

static void lsns_init_debug(void)
{
__UL_INIT_DEBUG(lsns, LSNS_DEBUG_, 0, LSNS_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(lsns, LSNS_DEBUG_, 0, LSNS_DEBUG);
}

static int ns_name2type(const char *name)
Expand Down
2 changes: 1 addition & 1 deletion term-utils/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct script_control {

static void script_init_debug(void)
{
__UL_INIT_DEBUG(script, SCRIPT_DEBUG_, 0, SCRIPT_DEBUG);
__UL_INIT_DEBUG_FROM_ENV(script, SCRIPT_DEBUG_, 0, SCRIPT_DEBUG);
}

/*
Expand Down

0 comments on commit a15dca2

Please sign in to comment.