瀏覽代碼

Add a tor_assert macro that logs failed assertions.

svn:r1695
Nick Mathewson 21 年之前
父節點
當前提交
e062ca046b
共有 1 個文件被更改,包括 12 次插入0 次删除
  1. 12 0
      src/common/util.h

+ 12 - 0
src/common/util.h

@@ -40,6 +40,18 @@
 #define INLINE inline
 #endif
 
+#ifdef NDEBUG
+#define tor_assert(expr) do {} while(0)
+#else
+#define tor_assert(expr) do {                                 \
+ if (!(expr)) {                                               \
+   log(LOG_ERR, "%s:%d: %s: Assertion %s failed; aborting.",  \
+       __FILE__, __LINE__, __FUNCTION__, #expr);              \
+   assert(expr); /* write to console too. */                  \
+   abort();  /* unreached */                                  \
+ } } while (0)
+#endif
+
 /* legal characters in a filename */
 #define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"