From 8061fd556a657e8658df7efe3828adc318311524 Mon Sep 17 00:00:00 2001 From: Tomas Valla Date: Mon, 28 Apr 2014 11:13:48 +0200 Subject: [PATCH] asserts: Added ASSERT_MSG to print a debug msg together with assertion fail. --- ucw/lib.h | 13 ++++++++++++- ucw/log.c | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ucw/lib.h b/ucw/lib.h index 9892d972..45ad09e5 100644 --- a/ucw/lib.h +++ b/ucw/lib.h @@ -2,7 +2,7 @@ * The UCW Library -- Miscellaneous Functions * * (c) 1997--2014 Martin Mares - * (c) 2005 Tomas Valla + * (c) 2005--2014 Tomas Valla * (c) 2006 Robert Spalek * (c) 2007 Pavel Charvat * @@ -19,6 +19,7 @@ #ifdef CONFIG_UCW_CLEAN_ABI #define assert_failed ucw_assert_failed +#define assert_failed_msg ucw_assert_failed_msg #define assert_failed_noinfo ucw_assert_failed_noinfo #define big_alloc ucw_big_alloc #define big_alloc_zero ucw_big_alloc_zero @@ -162,6 +163,7 @@ void log_fork(void); /** Call after fork() to update @log_pid. **/ void log_file(const char *name); /** Establish logging to the named file. Also redirect stderr there. **/ void assert_failed(const char *assertion, const char *file, int line) NONRET; +void assert_failed_msg(const char *assertion, const char *file, int line, const char *fmt, ...) NONRET FORMAT_CHECK(printf,4,5); void assert_failed_noinfo(void) NONRET; #ifdef DEBUG_ASSERTS @@ -170,8 +172,17 @@ void assert_failed_noinfo(void) NONRET; * Assertion checks are compiled only when `DEBUG_ASSERTS` is defined. **/ #define ASSERT(x) ({ if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); 1; }) + +/** + * Check an assertion with a debug message. If the condition @cond is false, + * print the message and stop the program with fatal error. + * Assertion checks are compiled only when `DEBUG_ASSERTS` is defined. + **/ +#define ASSERT_MSG(cond,str,x...) ({ if (unlikely(!(cond))) assert_failed_msg(#cond, __FILE__, __LINE__, str,##x); 1; }) + #else #define ASSERT(x) ({ if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); 1; }) +#define ASSERT_MSG(cond,str,x...) ASSERT(cond) #endif #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1] diff --git a/ucw/log.c b/ucw/log.c index aea97e3b..dc210385 100644 --- a/ucw/log.c +++ b/ucw/log.c @@ -3,6 +3,7 @@ * * (c) 1997--2009 Martin Mares * (c) 2008 Tomas Gavenciak + * (c) 2014 Tomas Valla * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -386,6 +387,16 @@ assert_failed(const char *assertion, const char *file, int line) abort(); } +void +assert_failed_msg(const char *assertion, const char *file, int line, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + vmsg(L_DEBUG, fmt, args); + msg(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line); + abort(); +} + void assert_failed_noinfo(void) { -- 2.39.2