PageRenderTime 22ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/assert.h

http://github.com/mozy/mordor
C Header | 104 lines | 76 code | 18 blank | 10 comment | 7 complexity | 541ba273d2d9c00bcef5b0ed24f6800d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_ASSERT_H__
  2. #define __MORDOR_ASSERT_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include "exception.h"
  5. #include "log.h"
  6. #include "version.h"
  7. namespace Mordor {
  8. bool isDebuggerAttached();
  9. void debugBreak();
  10. struct Assertion : virtual Exception
  11. {
  12. Assertion(const std::string &expr) : m_expr(expr) {}
  13. ~Assertion() throw() {}
  14. const char *what() const throw() { return m_expr.c_str(); }
  15. static bool throwOnAssertion;
  16. private:
  17. std::string m_expr;
  18. };
  19. }
  20. #endif
  21. // No include guard - you can include multiple times
  22. #ifdef MORDOR_ASSERT
  23. #undef MORDOR_ASSERT
  24. #endif
  25. #ifdef MORDOR_VERIFY
  26. #undef MORDOR_VERIFY
  27. #endif
  28. #ifdef MORDOR_NOTREACHED
  29. #undef MORDOR_NOTREACHED
  30. #endif
  31. #ifdef NDEBUG
  32. #ifndef NDEBUG_PERF
  33. #define NDEBUG_PERF
  34. #endif
  35. #define MORDOR_ASSERT(x) ((void)0)
  36. #define MORDOR_NOTHROW_ASSERT(x) ((void)0)
  37. #define MORDOR_VERIFY(x) ((void)(x))
  38. #define MORDOR_NOTHROW_VERIFY(x) ((void)(x))
  39. #define MORDOR_NOTREACHED() ::std::terminate();
  40. #else
  41. #define MORDOR_ASSERT(x) \
  42. while (!(x)) { \
  43. MORDOR_LOG_FATAL(::Mordor::Log::root()) \
  44. << "ASSERTION: " # x \
  45. << "\nbacktrace:\n" << ::Mordor::to_string(::Mordor::backtrace()); \
  46. if (::Mordor::Assertion::throwOnAssertion) \
  47. MORDOR_THROW_EXCEPTION(::Mordor::Assertion(# x)); \
  48. if (::Mordor::isDebuggerAttached()) \
  49. ::Mordor::debugBreak(); \
  50. ::std::terminate(); \
  51. }
  52. #define MORDOR_NOTHROW_ASSERT(x) \
  53. while (!(x)) { \
  54. MORDOR_LOG_FATAL(::Mordor::Log::root()) \
  55. << "ASSERTION: " # x \
  56. << "\nbacktrace:\n" << ::Mordor::to_string(::Mordor::backtrace()); \
  57. if (::Mordor::isDebuggerAttached()) \
  58. ::Mordor::debugBreak(); \
  59. ::std::terminate(); \
  60. }
  61. #define MORDOR_VERIFY(x) MORDOR_ASSERT(x)
  62. #define MORDOR_NOTHROW_VERIFY(x) MORDOR_NOTHROW_ASSERT(x)
  63. #define MORDOR_NOTREACHED() \
  64. { \
  65. MORDOR_LOG_FATAL(::Mordor::Log::root()) << "NOT REACHED" \
  66. << "\nbacktrace:\n" << ::Mordor::to_string(::Mordor::backtrace()); \
  67. if (::Mordor::Assertion::throwOnAssertion) \
  68. MORDOR_THROW_EXCEPTION(::Mordor::Assertion("Not Reached")); \
  69. if (::Mordor::isDebuggerAttached()) \
  70. ::Mordor::debugBreak(); \
  71. ::std::terminate(); \
  72. }
  73. #endif
  74. /// MORDOR_ASSERT_PERF Macro is mostly the same as MORDOR_ASSERT except that
  75. /// - MORDOR_ASSERT can be only turned off by defining NDEBUG
  76. /// - MORDOR_ASSERT_PERF can be turned off by either NDEBUG or NDEBUG_PERF
  77. /// * MORDOR_ASSERT_PERF applies to those assertion that has significant
  78. /// performance impact.
  79. /// * NDEBUG_PERF can be defined When application don't want to disable all
  80. /// assertions but only to those which have big performance impact.
  81. /// * NOTE: if NDEBUG is defined, NDEBUG_PERF will be automatically defined.
  82. #ifndef NDEBUG_PERF
  83. #define MORDOR_ASSERT_PERF(x) MORDOR_ASSERT(x)
  84. #else
  85. #define MORDOR_ASSERT_PERF(x) ((void)0)
  86. #endif