Assert.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #ifndef BASELIB_ENABLE_ASSERTIONS
  3. #ifdef NDEBUG
  4. #define BASELIB_ENABLE_ASSERTIONS 0
  5. #else
  6. #define BASELIB_ENABLE_ASSERTIONS 1
  7. #endif
  8. #endif
  9. #include "../C/Baselib_Debug.h"
  10. #ifdef __cplusplus
  11. BASELIB_C_INTERFACE
  12. {
  13. #endif
  14. #if COMPILER_CLANG || COMPILER_GCC
  15. __attribute__((format(printf, 1, 2)))
  16. #endif
  17. BASELIB_API void detail_AssertLog(const char* format, ...);
  18. #define DETAIL__ASSERT_LOG(ASSERT_EXPRESSION_, message, ...) \
  19. PP_EVAL(PP_IF_ELSE(PP_VARG_IS_NONEMPTY(__VA_ARGS__)) \
  20. (detail_AssertLog("%s(%d): Assertion failed (%s) - " message "\n", __FILE__, __LINE__, #ASSERT_EXPRESSION_, __VA_ARGS__)) \
  21. (detail_AssertLog("%s(%d): Assertion failed (%s) - %s\n", __FILE__, __LINE__, #ASSERT_EXPRESSION_, message)) \
  22. )
  23. #define BaselibAssert(ASSERT_EXPRESSION_, ...) \
  24. do { \
  25. if (BASELIB_ENABLE_ASSERTIONS) \
  26. { \
  27. if(!(ASSERT_EXPRESSION_)) \
  28. { \
  29. PP_EVAL(PP_IF_ELSE(PP_VARG_IS_NONEMPTY(__VA_ARGS__)) \
  30. (DETAIL__ASSERT_LOG(ASSERT_EXPRESSION_, __VA_ARGS__)) \
  31. (detail_AssertLog("%s(%d): Assertion failed (%s)\n", __FILE__, __LINE__, #ASSERT_EXPRESSION_)) \
  32. ); \
  33. Baselib_Debug_Break(); \
  34. } \
  35. } \
  36. } while(0)
  37. #ifdef __cplusplus
  38. } // BASELIB_C_INTERFACE
  39. #endif