[PATCH] lpd: write debug messages to stdout with compile-time or run-time flags

Mauricio Faria de Oliveira <[email protected]> Tue, 8 Nov 2016 15:27:52 -0200
Newsgroups gmane.linux.tools.diag.devel
Message-ID <[email protected]>
If LPD_DEBUG is set at compile time (e.g., make CFLAGS='-DLPD_DEBUG'),
then always write the messages at runtime.

If it's not (default), check if the LPD_DEBUG environment variable
is set at runtime (e.g., export LPD_DEBUG=1), and only write if it is.

Test: compile-time flag only
----------------------------

    ppc64-diag $ make CFLAGS='-DLPD_DEBUG'

    ppc64-diag $ sudo ./lpd/usysident 
    <...>
    DEBUG: get_mv_indicator(): ATA Index: '1', Port Number: '3', Port Mode: '5' ('GPIO_DATA_OUT[n]')
    -B0-T0-L0        [off]
    <...>
    DEBUG: get_mv_indicator(): ATA Index: '2', Port Number: '4', Port Mode: '5' ('GPIO_DATA_OUT[n]')
    -B0-T0-L0        [off]

Test: run-time env var only
---------------------------

    ppc64-diag $ rm lpd/files.o
    ppc64-diag $ make  # without CFLAGS for LPD_DEBUG

    ppc64-diag $ sudo ./lpd/usysident # without debug messages by default
    -B0-T0-L0        [off]
    -B0-T0-L0        [off]

    ppc64-diag $ sudo LPD_DEBUG=1 ./lpd/usysident # with debug messages with env var
    <...>
    DEBUG: get_mv_indicator(): ATA Index: '1', Port Number: '3', Port Mode: '5' ('GPIO_DATA_OUT[n]')
    -B0-T0-L0        [off]
    <...>
    DEBUG: get_mv_indicator(): ATA Index: '2', Port Number: '4', Port Mode: '5' ('GPIO_DATA_OUT[n]')
    -B0-T0-L0        [off]

Signed-off-by: Mauricio Faria de Oliveira <[email protected]>
---
 lpd/files.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lpd/files.c b/lpd/files.c
index 39c3a4e..fe6ff2c 100644
--- a/lpd/files.c
+++ b/lpd/files.c
@@ -154,11 +154,21 @@ insert_time(char *buf, int size)
 void
 _dbg(const char *fmt, ...)
 {
-#ifdef LPD_DEBUG
 	int     len = 0;
 	char    buf[LP_ERROR_LOG_MAX];
 	va_list	ap;
 
+/*
+ * If LPD_DEBUG is set at compile time (e.g., make CFLAGS='-DLPD_DEBUG'),
+ * then always write the messages at runtime.
+ * If it's not (default), check if the LPD_DEBUG environment variable
+ * is set at runtime (e.g., export LPD_DEBUG=1), and only write if it is.
+ */
+#ifndef LPD_DEBUG
+	if (!getenv("LPD_DEBUG"))
+		return;
+#endif
+
 	va_start(ap, fmt);
 	len = snprintf(buf, LP_ERROR_LOG_MAX, "DEBUG: ");
 	len += vsnprintf(buf + len, LP_ERROR_LOG_MAX - len, fmt, ap);
@@ -170,7 +180,6 @@ _dbg(const char *fmt, ...)
 	fprintf(stdout, buf);
 	fprintf(stdout, "\n");
 	fflush(stdout);
-#endif
 }
 
 /**
-- 
1.8.3.1


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi