Each single file in our source code redefines the same DPRINTF macro. What we want instead is to have the macro shared and also we might to avoid global variable shared across all the code.
Signed-off-by: Michal Privoznik <mpriv...@redhat.com> --- src/libvirt-php.c | 15 ++------------- src/sockets.c | 9 +-------- src/util.c | 34 +++++++++++++++++++++++++++++++++- src/util.h | 12 ++++++++++-- src/vncfunc.c | 9 +-------- 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index faa7200..e129481 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -31,18 +31,7 @@ #include "vncfunc.h" #include "sockets.h" -#ifdef DEBUG_SUPPORT -int gdebug; -#endif - -#ifdef DEBUG_CORE -#define DPRINTF(fmt, ...) \ -if (LIBVIRT_G(debug)) \ -do { fprintf(stderr, "[%s ", get_datetime()); fprintf(stderr, "libvirt-php/core ]: " fmt , ## __VA_ARGS__); fflush(stderr); } while (0) -#else -#define DPRINTF(fmt, ...) \ -do {} while(0) -#endif +DEBUG_INIT("core"); /* PHP functions are prefixed with `zif_` so strip it */ #define PHPFUNC (__FUNCTION__ + 4) @@ -819,7 +808,7 @@ PHP_INI_END() void change_debug(int val TSRMLS_DC) { LIBVIRT_G(debug) = val; - gdebug = val; + setDebug(val); } /* PHP requires to have this function defined */ diff --git a/src/sockets.c b/src/sockets.c index 6620e17..0a3e3c2 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -26,14 +26,7 @@ #include "sockets.h" #include "util.h" -#ifdef DEBUG_SOCKETS -#define DPRINTF(fmt, ...) \ -if (gdebug) \ -do { fprintf(stderr, "[%s ", get_datetime()); fprintf(stderr, "libvirt-php/sockets]: " fmt , ## __VA_ARGS__); fflush(stderr); } while (0) -#else -#define DPRINTF(fmt, ...) \ -do {} while(0) -#endif +DEBUG_INIT("sockets"); /* Function macro */ #define PHPFUNC __FUNCTION__ diff --git a/src/util.c b/src/util.c index 7d2b457..53096ae 100644 --- a/src/util.c +++ b/src/util.c @@ -9,11 +9,15 @@ #include <config.h> +#include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <time.h> #include "util.h" +int gdebug; + /* * Private function name: get_datetime * Since version: 0.4.2 @@ -21,7 +25,8 @@ * Arguments: None * Returns: Date/time string in `YYYY-mm-dd HH:mm:ss` format */ -char *get_datetime(void) +static char * +get_datetime(void) { /* Caution: Function cannot use DPRINTF() macro otherwise the neverending loop will be met! */ char *outstr = NULL; @@ -39,3 +44,30 @@ char *get_datetime(void) return outstr; } + +void debugPrint(const char *source, + const char *fmt, ...) +{ + char *datetime; + va_list args; + + if (!gdebug) + return; + + datetime = get_datetime(); + fprintf(stderr, "[%s libvirt-php/%s ]: ", datetime, source); + free(datetime); + + if (fmt) { + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + } + fprintf(stderr, "\n"); + fflush(stderr); +} + +void setDebug(int level) +{ + gdebug = level; +} diff --git a/src/util.h b/src/util.h index e907a49..c2b7324 100644 --- a/src/util.h +++ b/src/util.h @@ -17,9 +17,14 @@ # ifdef DEBUG_SUPPORT # define DEBUG_CORE # define DEBUG_VNC -extern int gdebug; # endif +# define DEBUG_INIT(source) \ + static const char *debugSource = "" source "" + +# define DPRINTF(fmt, ...) \ + debugPrint(debugSource, fmt, __VA_ARGS__) + # define ARRAY_CARDINALITY(array) (sizeof(array) / sizeof(array[0])) # define IS_BIGENDIAN (*(uint16_t *)"\0\xff" < 0x100) @@ -60,6 +65,9 @@ extern int gdebug; ((uint32_t)var[2] << 8) + \ ((uint32_t)var[3])) -char *get_datetime(void); +void debugPrint(const char *source, + const char *fmt, ...); + +void setDebug(int level); #endif /* __UTIL_H__ */ diff --git a/src/vncfunc.c b/src/vncfunc.c index cb98341..45f4007 100644 --- a/src/vncfunc.c +++ b/src/vncfunc.c @@ -23,14 +23,7 @@ #include "util.h" #include "sockets.h" -#ifdef DEBUG_VNC -#define DPRINTF(fmt, ...) \ -if (gdebug) \ -do { fprintf(stderr, "[%s ", get_datetime()); fprintf(stderr, "libvirt-php/vnc ]: " fmt , ## __VA_ARGS__); fflush(stderr); } while (0) -#else -#define DPRINTF(fmt, ...) \ -do {} while(0) -#endif +DEBUG_INIT("vncfunc"); /* Function macro */ #define PHPFUNC __FUNCTION__ -- 2.8.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list