aboutsummaryrefslogtreecommitdiff
path: root/src/debug.h
diff options
context:
space:
mode:
authorPeter Carbonetto2017-08-07 13:23:44 -0500
committerPeter Carbonetto2017-08-07 13:23:44 -0500
commitecc5c0b5b11cbc2501b60d6d4e9a9b3e8f270d02 (patch)
treeb27cd975b19b85e75255cacf1c3d771266c56a10 /src/debug.h
parent8be2ce433c015d8c6776d270537d645c7e761bdf (diff)
parent7360d14216400b8f12fbfda03ac2f4827b102711 (diff)
downloadpangemma-ecc5c0b5b11cbc2501b60d6d4e9a9b3e8f270d02.tar.gz
Merge branch 'master' of github.com:genetics-statistics/GEMMA
Diffstat (limited to 'src/debug.h')
-rw-r--r--src/debug.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/debug.h b/src/debug.h
new file mode 100644
index 0000000..84637e1
--- /dev/null
+++ b/src/debug.h
@@ -0,0 +1,46 @@
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+// enforce works like assert but also when NDEBUG is set (i.e., it
+// always works). enforce_msg prints message instead of expr
+
+#if defined NDEBUG
+#define debug_msg(msg)
+#else
+#define debug_msg(msg) cout << "**** DEBUG: " << msg << endl;
+#endif
+
+/* This prints an "Assertion failed" message and aborts. */
+extern "C" void __assert_fail(const char *__assertion, const char *__file,
+ unsigned int __line,
+ const char *__function) __THROW
+ __attribute__((__noreturn__));
+
+#define __ASSERT_FUNCTION __PRETTY_FUNCTION__
+
+#define enforce(expr) \
+ ((expr) \
+ ? __ASSERT_VOID_CAST(0) \
+ : __assert_fail(__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
+
+#define enforce_msg(expr, msg) \
+ ((expr) ? __ASSERT_VOID_CAST(0) \
+ : __assert_fail(msg, __FILE__, __LINE__, __ASSERT_FUNCTION))
+
+#define enforce_str(expr, msg) \
+ ((expr) \
+ ? __ASSERT_VOID_CAST(0) \
+ : __assert_fail((msg).c_str(), __FILE__, __LINE__, __ASSERT_FUNCTION))
+
+// Helpers to create a unique varname per MACRO
+#define COMBINE1(X, Y) X##Y
+#define COMBINE(X, Y) COMBINE1(X, Y)
+
+#define enforce_gsl(expr) \
+ auto COMBINE(res, __LINE__) = (expr); \
+ (COMBINE(res, __LINE__) == 0 \
+ ? __ASSERT_VOID_CAST(0) \
+ : __assert_fail(gsl_strerror(COMBINE(res, __LINE__)), __FILE__, \
+ __LINE__, __ASSERT_FUNCTION))
+
+#endif