From b8ffd5621f5495b0ad1a92a67b51b7e0e15b0b6e Mon Sep 17 00:00:00 2001 From: Tharre Date: Fri, 5 Dec 2014 08:52:50 +0100 Subject: Improve error message handling for debugging --- src/dbg.h | 15 ++++++++++++--- src/util.c | 2 +- src/util.h | 5 +---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/dbg.h b/src/dbg.h index ef86cac..961128f 100644 --- a/src/dbg.h +++ b/src/dbg.h @@ -21,7 +21,18 @@ /* helper functions which help in replacing the GNU extension ##__VA_ARGS__ */ #define STRINGIFY(x) #x -#define LOG_HELPER(f,l,...) fprintf(stderr, "("f":"STRINGIFY(l)"): "__VA_ARGS__) +#define PREFIX(...) PREFIX_HELPER(_FILENAME, __LINE__, __VA_ARGS__) +#define SUFFIX(S, M, ...) M S, __VA_ARGS__ + +#define log_err(...) fprintf(stderr, PREFIX(__VA_ARGS__)) +#define die(...) die_(PREFIX(__VA_ARGS__)) +#define fatal(...) die(SUFFIX(": %s\n", __VA_ARGS__, strerror(errno))) + +#ifdef NDEBUG +#define PREFIX_HELPER(f,l,...) __VA_ARGS__ +#else +#define PREFIX_HELPER(f,l,...) "(" f ":" STRINGIFY(l) "): " __VA_ARGS__ +#endif #ifdef NDEBUG #define debug(...) @@ -29,8 +40,6 @@ #define debug(...) log_err(__VA_ARGS__) #endif -#define log_err(...) LOG_HELPER(_FILENAME, __LINE__, __VA_ARGS__) - #define assert_str_equal(a,b) ({ \ if (strcmp(a, b)) { \ log_err("Assertion error: '%s' == '%s'\n", a, b); \ diff --git a/src/util.c b/src/util.c index a871588..bd7e0bf 100644 --- a/src/util.c +++ b/src/util.c @@ -21,7 +21,7 @@ /* Print a given formated error message and die. */ -extern void __attribute__((noreturn)) die(const char *err, ...) { +extern void __attribute__((noreturn)) die_(const char *err, ...) { assert(err); va_list ap; va_start(ap, err); diff --git a/src/util.h b/src/util.h index db02cd4..5e46166 100644 --- a/src/util.h +++ b/src/util.h @@ -11,10 +11,7 @@ #include -#define DIE_HELPER(M, ...) die(M ": %s\n", __VA_ARGS__) -#define fatal(...) DIE_HELPER(__VA_ARGS__, strerror(errno)) - -extern void __attribute__((noreturn)) die(const char *err, ...); +extern void __attribute__((noreturn)) die_(const char *err, ...); extern void *xmalloc(size_t size); extern void *xrealloc(void *ptr, size_t size); extern char *xstrdup(const char *str); -- cgit v1.2.3-70-g09d2