crn_assert.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // File: crn_assert.cpp
  2. // See Copyright Notice and license at the end of inc/crnlib.h
  3. #include "crn_core.h"
  4. #if CRNLIB_USE_WIN32_API
  5. #include "crn_winhdr.h"
  6. #endif
  7. static bool g_fail_exceptions;
  8. static bool g_exit_on_failure = true;
  9. void crnlib_enable_fail_exceptions(bool enabled)
  10. {
  11. g_fail_exceptions = enabled;
  12. }
  13. void crnlib_assert(const char* pExp, const char* pFile, unsigned line)
  14. {
  15. char buf[512];
  16. sprintf_s(buf, sizeof(buf), "%s(%u): Assertion failed: \"%s\"\n", pFile, line, pExp);
  17. crnlib_output_debug_string(buf);
  18. fputs(buf, stderr);
  19. if (crnlib_is_debugger_present())
  20. crnlib_debug_break();
  21. }
  22. void crnlib_fail(const char* pExp, const char* pFile, unsigned line)
  23. {
  24. char buf[512];
  25. sprintf_s(buf, sizeof(buf), "%s(%u): Failure: \"%s\"\n", pFile, line, pExp);
  26. crnlib_output_debug_string(buf);
  27. fputs(buf, stderr);
  28. if (crnlib_is_debugger_present())
  29. crnlib_debug_break();
  30. #if CRNLIB_USE_WIN32_API
  31. if (g_fail_exceptions)
  32. RaiseException(CRNLIB_FAIL_EXCEPTION_CODE, 0, 0, NULL);
  33. else
  34. #endif
  35. if (g_exit_on_failure)
  36. exit(EXIT_FAILURE);
  37. }
  38. void trace(const char* pFmt, va_list args)
  39. {
  40. if (crnlib_is_debugger_present())
  41. {
  42. char buf[512];
  43. vsprintf_s(buf, sizeof(buf), pFmt, args);
  44. crnlib_output_debug_string(buf);
  45. }
  46. };
  47. void trace(const char* pFmt, ...)
  48. {
  49. va_list args;
  50. va_start(args, pFmt);
  51. trace(pFmt, args);
  52. va_end(args);
  53. };