diff --git a/samples/main-debug.cc b/samples/main-debug.cc index 332e693f2..d10f5169d 100644 --- a/samples/main-debug.cc +++ b/samples/main-debug.cc @@ -15,4 +15,11 @@ int main (int argc, int argv) foo::OneFunction (); NS3_DEBUG ("other debug output"); + + int a = 0; + + NS3_ASSERT (a == 0); + NS3_ASSERT_MSG (a == 0, "my msg"); + NS3_ASSERT (a != 0) + NS3_ASSERT_MSG (a != 0, "my 2 msg"); } diff --git a/src/core/debug.cc b/src/core/debug.cc index addfa6b1c..a0da17a5e 100644 --- a/src/core/debug.cc +++ b/src/core/debug.cc @@ -168,6 +168,12 @@ DebugComponentPrintList (void) } } +void +AssertBreakpoint (void) +{ + int *a = 0; + a = 0; +} }; // namespace ns3 diff --git a/src/core/debug.h b/src/core/debug.h index c64f88e45..458d1f243 100644 --- a/src/core/debug.h +++ b/src/core/debug.h @@ -40,6 +40,8 @@ private: bool m_isEnabled; }; +void AssertBreakpoint (void); + }; // namespace ns3 @@ -53,10 +55,29 @@ private: { \ std::cout << x << std::endl; \ } + +#define NS3_ASSERT(condition) \ + if (!(condition)) \ + { \ + std::cout << "assert failed. file=" << __FILE__ << \ + ", line=" << __LINE__ << ", cond=\""#condition << \ + "\"" << std::endl; \ + ns3::AssertBreakpoint (); \ + } + +#define NS3_ASSERT_MSG(condition, message) \ + if (!(condition)) \ + { \ + std::cout << message << std::endl; \ + ns3::AssertBreakpoint (); \ + } + #else /* NS3_DEBUG_ENABLE */ #define NS3_DEBUG_COMPONENT_DEFINE(name) #define NS3_DEBUG(x) +#define NS3_ASSERT(cond) +#define NS3_ASSERT_MSG(cond) #endif /* NS3_DEBUG_ENABLE */