From 368b803216312b197ba5d0e8fedaf9b2089f0ad0 Mon Sep 17 00:00:00 2001 From: Geoff Gustafson Date: Thu, 7 Sep 2017 15:05:40 -0700 Subject: [PATCH] [utils] Add ZJS_ASSERT macro to test assertions in debug mode Does nothing in release mode, but in debug mode if an assertion fails it prints a message and hangs to make sure it's noticed. Signed-off-by: Geoff Gustafson --- src/zjs_common.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/zjs_common.h b/src/zjs_common.h index 838673e19..dcb43ac63 100644 --- a/src/zjs_common.h +++ b/src/zjs_common.h @@ -48,15 +48,24 @@ int zjs_get_ms(void); zjs_shorten_filepath(__FILE__), __LINE__, __func__); \ ZJS_PRINT -#else -#define DBG_PRINT(fmat...) \ - do { \ - } while (0); +#define ZJS_ASSERT(condition, str) \ + if (!(condition)) { \ + ERR_PRINT("ASSERTION FAILURE: %s\n", str); \ + do {} while (1); \ + } + +#else // !DEBUG_BUILD + +#define DBG_PRINT(fmt...) do {} while (0); + #define ERR_PRINT \ ZJS_PRINT("\n%s:%d %s():\n(ERROR) ", zjs_shorten_filepath(__FILE__), \ __LINE__, __func__); \ ZJS_PRINT -#endif + +#define ZJS_ASSERT(condition, str) do {} while (0); + +#endif // DEBUG_BUILD // TODO: We should instead have a macro that changes in debug vs. release build, // to save string space and instead print error codes or something for release. @@ -104,4 +113,5 @@ int zjs_get_ms(void); #ifndef TEMP_DEVICE_NAME #define TEMP_DEVICE_NAME "" #endif + #endif // __zjs_common_h__