]> mj.ucw.cz Git - libucw.git/commitdiff
asserts: Added ASSERT_MSG to print a debug msg together with assertion fail.
authorTomas Valla <tom@ucw.cz>
Mon, 28 Apr 2014 09:13:48 +0000 (11:13 +0200)
committerTomas Valla <tom@ucw.cz>
Mon, 28 Apr 2014 09:13:48 +0000 (11:13 +0200)
ucw/lib.h
ucw/log.c

index 9892d9723e7c5946a1b37cdae81bd1255b01ab2d..45ad09e545b35794f378bbf5c1df86dfce1cb9e8 100644 (file)
--- a/ucw/lib.h
+++ b/ucw/lib.h
@@ -2,7 +2,7 @@
  *     The UCW Library -- Miscellaneous Functions
  *
  *     (c) 1997--2014 Martin Mares <mj@ucw.cz>
  *     The UCW Library -- Miscellaneous Functions
  *
  *     (c) 1997--2014 Martin Mares <mj@ucw.cz>
- *     (c) 2005 Tomas Valla <tom@ucw.cz>
+ *     (c) 2005--2014 Tomas Valla <tom@ucw.cz>
  *     (c) 2006 Robert Spalek <robert@ucw.cz>
  *     (c) 2007 Pavel Charvat <pchar@ucw.cz>
  *
  *     (c) 2006 Robert Spalek <robert@ucw.cz>
  *     (c) 2007 Pavel Charvat <pchar@ucw.cz>
  *
@@ -19,6 +19,7 @@
 
 #ifdef CONFIG_UCW_CLEAN_ABI
 #define assert_failed ucw_assert_failed
 
 #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
 #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 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
 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; })
  * 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; })
 #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]
 #endif
 
 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
index aea97e3bd61457d2f0d371021cfe7cfcb0a306f0..dc21038573e29eaf00162e72eab03277190019d9 100644 (file)
--- a/ucw/log.c
+++ b/ucw/log.c
@@ -3,6 +3,7 @@
  *
  *     (c) 1997--2009 Martin Mares <mj@ucw.cz>
  *     (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
  *
  *     (c) 1997--2009 Martin Mares <mj@ucw.cz>
  *     (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
+ *     (c) 2014 Tomas Valla <tom@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  *
  *     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();
 }
 
   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)
 {
 void
 assert_failed_noinfo(void)
 {