From 000a676164789647800cf6b265eb6b5e75bb9fdf Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 6 Aug 2016 20:00:34 +0200 Subject: [PATCH] libcollectdclient: Don't print anything to STDOUT by default. Even with --enable-debug. This behavior is now controlled by the "COLLECTD_TRACE" environment variable. Fixes: #105 --- src/libcollectdclient/client.c | 29 +++++++++++++++++-------- src/libcollectdclient/collectd/client.h | 7 ++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/libcollectdclient/client.c b/src/libcollectdclient/client.c index 802e6439..c90107d1 100644 --- a/src/libcollectdclient/client.c +++ b/src/libcollectdclient/client.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -91,12 +92,6 @@ (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \ } while (0) -#if COLLECT_DEBUG -# define LCC_DEBUG(...) printf (__VA_ARGS__) -#else -# define LCC_DEBUG(...) /**/ -#endif - /* * Types */ @@ -118,6 +113,22 @@ typedef struct lcc_response_s lcc_response_t; /* * Private functions */ +static int lcc_tracef(char const *format, ...) +{ + va_list ap; + int status; + + char const *trace = getenv (LCC_TRACE_ENV); + if (!trace || (strcmp ("", trace) == 0) || (strcmp ("0", trace) == 0)) + return 0; + + va_start (ap, format); + status = vprintf (format, ap); + va_end (ap); + + return status; +} + /* Even though Posix requires "strerror_r" to return an "int", * some systems (e.g. the GNU libc) return a "char *" _and_ * ignore the second argument ... -tokkee */ @@ -250,7 +261,7 @@ static int lcc_send (lcc_connection_t *c, const char *command) /* {{{ */ { int status; - LCC_DEBUG ("send: --> %s\n", command); + lcc_tracef ("send: --> %s\n", command); status = fprintf (c->fh, "%s\r\n", command); if (status < 0) @@ -281,7 +292,7 @@ static int lcc_receive (lcc_connection_t *c, /* {{{ */ return (-1); } lcc_chomp (buffer); - LCC_DEBUG ("receive: <-- %s\n", buffer); + lcc_tracef ("receive: <-- %s\n", buffer); /* Convert the leading status to an integer and make `ptr' to point to the * beginning of the message. */ @@ -329,7 +340,7 @@ static int lcc_receive (lcc_connection_t *c, /* {{{ */ break; } lcc_chomp (buffer); - LCC_DEBUG ("receive: <-- %s\n", buffer); + lcc_tracef ("receive: <-- %s\n", buffer); res.lines[i] = strdup (buffer); if (res.lines[i] == NULL) diff --git a/src/libcollectdclient/collectd/client.h b/src/libcollectdclient/collectd/client.h index 6ae85984..8e7f4b60 100644 --- a/src/libcollectdclient/collectd/client.h +++ b/src/libcollectdclient/collectd/client.h @@ -29,6 +29,13 @@ #include "lcc_features.h" +/* COLLECTD_TRACE is the environment variable used to control trace output. When + * set to something non-zero, all lines sent to / received from the daemon are + * printed to STDOUT. */ +#ifndef LCC_TRACE_ENV +# define LCC_TRACE_ENV "COLLECTD_TRACE" +#endif + /* * Includes (for data types) */ -- 2.30.2