From 7ced426dcbac270e05d48913f2611f34e48eeb40 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Sun, 3 Jun 2012 11:14:04 +0200 Subject: [PATCH] core: Edhance and rewrite debug messages. --- include/core/GP_Debug.h | 28 +++++++++++++++++++--------- libs/core/GP_Debug.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/include/core/GP_Debug.h b/include/core/GP_Debug.h index 235f001e..a09ba69a 100644 --- a/include/core/GP_Debug.h +++ b/include/core/GP_Debug.h @@ -16,7 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * - * Copyright (C) 2009-2011 Cyril Hrubis * + * Copyright (C) 2009-2012 Cyril Hrubis * * * *****************************************************************************/ @@ -33,6 +33,10 @@ Debug level > 1 is intended for more verbose reporting, like inner cycles or loop debugging. + Debug levels with negative level are special. Debug level -1 means TODO, + level -2 says WARNING while -2 means BUG (i.e. library get into unconsistent + state). + */ #ifndef GP_DEBUG_H @@ -45,17 +49,23 @@ #define GP_DEFAULT_DEBUG_LEVEL 0 -#define GP_DEBUG(level, ...) do { \ - if (level <= GP_GetDebugLevel()) { \ - fprintf(stderr, "%u: %s:%s():%u: ", level, __FILE__, \ - __FUNCTION__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); \ - fputc('\n', stderr); \ - } \ -} while (0) +#define GP_DEBUG(level, ...) \ + GP_DebugPrint(level, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) + +#define GP_TODO(...) \ + GP_DebugPrint(-1, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) + +#define GP_WARN(...) \ + GP_DebugPrint(-2, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) + +#define GP_BUG(...) \ + GP_DebugPrint(-3, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) void GP_SetDebugLevel(unsigned int level); unsigned int GP_GetDebugLevel(void); +void GP_DebugPrint(int level, const char *file, const char *function, int line, + const char *fmt, ...) __attribute__ ((format (printf, 5, 6))); + #endif /* GP_DEBUG_H */ diff --git a/libs/core/GP_Debug.c b/libs/core/GP_Debug.c index d3b8cbe2..ecef2161 100644 --- a/libs/core/GP_Debug.c +++ b/libs/core/GP_Debug.c @@ -16,20 +16,57 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * - * Copyright (C) 2009-2011 Cyril Hrubis * + * Copyright (C) 2009-2012 Cyril Hrubis * * * *****************************************************************************/ +#include + #include "GP_Debug.h" -static unsigned int GP_debug_level = GP_DEFAULT_DEBUG_LEVEL; +static unsigned int debug_level = GP_DEFAULT_DEBUG_LEVEL; void GP_SetDebugLevel(unsigned int level) { - GP_debug_level = level; + debug_level = level; } unsigned int GP_GetDebugLevel(void) { - return GP_debug_level; + return debug_level; +} + +void GP_DebugPrint(int level, const char *file, const char *function, int line, + const char *fmt, ...) +{ + int i; + + if (level > (int)debug_level) + return; + + for (i = 1; i < level; i++) + fputc(' ', stderr); + + switch (level) { + case -3: + fprintf(stderr, "*** BUG: %s:%s():%u: ", file, function, line); + break; + case -2: + fprintf(stderr, "*** WARNING: %s:%s():%u: ", file, function, line); + break; + case -1: + fprintf(stderr, "*** TODO: %s:%s():%u: ", file, function, line); + break; + default: + fprintf(stderr, "%u: %s:%s():%u: ", + level, file, function, line); + break; + } + + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); + + fputc('\n', stderr); } -- 2.11.4.GIT