C++11 way for logging & macroses.

This commit is contained in:
Alex Zolotarev 2015-02-20 22:20:29 +03:00
parent 9114556ba7
commit efac4023df
3 changed files with 21 additions and 96 deletions

View file

@ -18,34 +18,34 @@ namespace my
// TODO: Evaluate X only once in CHECK().
#define CHECK(X, msg) do { if (X) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X")", ::my::impl::Message msg));} } while(false)
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X")", ::my::impl::Message msg));} } while(false)
#define CHECK_EQUAL(X, Y, msg) do { if ((X) == (Y)) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X" == "#Y")", \
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X" == "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));} } while (false)
#define CHECK_NOT_EQUAL(X, Y, msg) do { if ((X) != (Y)) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X" != "#Y")", \
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X" != "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));} } while (false)
#define CHECK_LESS(X, Y, msg) do { if ((X) < (Y)) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X" < "#Y")", \
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X" < "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));} } while (false)
#define CHECK_LESS_OR_EQUAL(X, Y, msg) do { if ((X) <= (Y)) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X" <= "#Y")", \
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X" <= "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));} } while (false)
#define CHECK_GREATER(X, Y, msg) do { if ((X) > (Y)) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X" > "#Y")", \
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X" > "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));} } while (false)
#define CHECK_GREATER_OR_EQUAL(X, Y, msg) do { if ((X) >= (Y)) {} else { \
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg("CHECK("#X" >= "#Y")", \
::my::OnAssertFailed(SRC(), ::my::impl::Message("CHECK("#X" >= "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));} } while (false)
#define CHECK_OR_CALL(fail, call, X, msg) do { if (X) {} else { \
if (fail) {\
::my::OnAssertFailed(SRC(), ::my::impl::MergeMsg(::my::impl::Message("CHECK("#X")"), \
::my::OnAssertFailed(SRC(), ::my::impl::Message(::my::impl::Message("CHECK("#X")"), \
::my::impl::Message msg)); \
} else { \
call(); \

View file

@ -118,88 +118,13 @@ namespace my
{
return string();
}
string inline MergeMsg(string const & msg1, string const & msg2)
template <typename T> string Message(T const & t)
{
return msg1 + " " + msg2;
return DebugPrint(t);
}
string inline MergeMsg(string const & msg1, string const & msg2, string const & msg3)
template <typename T, typename... ARGS> string Message(T const & t, ARGS const & ... others)
{
return msg1 + " " + msg2 + " " + msg3;
}
template <typename T1>
string Message(T1 const & t1)
{
return DebugPrint(t1);
}
template <typename T1, typename T2>
string Message(T1 const & t1, T2 const & t2)
{
return MergeMsg(Message(t1), Message(t2));
}
template <typename T1, typename T2, typename T3>
string Message(T1 const & t1, T2 const & t2, T3 const & t3)
{
return MergeMsg(Message(t1, t2), Message(t3));
}
template <typename T1, typename T2, typename T3, typename T4>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4)
{
return MergeMsg(Message(t1, t2), Message(t3, t4));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5)
{
return MergeMsg(Message(t1, t2, t3), Message(t4, t5));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6)
{
return MergeMsg(Message(t1, t2, t3), Message(t4, t5, t6));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
typename T7>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6, T7 const & t7)
{
return MergeMsg(Message(t1, t2, t3, t4), Message(t5, t6, t7));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
typename T7, typename T8>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6, T7 const & t7, T8 const & t8)
{
return MergeMsg(Message(t1, t2, t3, t4), Message(t5, t6, t7, t8));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
typename T7, typename T8, typename T9>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6, T7 const & t7, T8 const & t8, T9 const & t9)
{
return MergeMsg(Message(t1, t2, t3, t4, t5), Message(t6, t7, t8, t9));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
typename T7, typename T8, typename T9, typename TA>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6, T7 const & t7, T8 const & t8, T9 const & t9, TA const & tA)
{
return MergeMsg(Message(t1, t2, t3, t4, t5), Message(t6, t7, t8, t9, tA));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
typename T7, typename T8, typename T9, typename TA, typename TB>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6, T7 const & t7, T8 const & t8, T9 const & t9, TA const & tA,
TB const & tB)
{
return MergeMsg(Message(t1, t2, t3, t4, t5, t6), Message(t7, t8, t9, tA, tB));
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6,
typename T7, typename T8, typename T9, typename TA, typename TB, typename TC>
string Message(T1 const & t1, T2 const & t2, T3 const & t3, T4 const & t4, T5 const & t5,
T6 const & t6, T7 const & t7, T8 const & t8, T9 const & t9, TA const & tA,
TB const & tB, TC const & tC)
{
return MergeMsg(Message(t1, t2, t3, t4, t5, t6), Message(t7, t8, t9, tA, tB, tC));
return DebugPrint(t) + " " + Message(others...);
}
}
}

View file

@ -26,36 +26,36 @@ namespace my
}
#define TEST(X, msg) { if (X) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X")", ::my::impl::Message msg));}}
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X")", ::my::impl::Message msg));}}
#define TEST_EQUAL(X, Y, msg) { if ((X) == (Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X" == "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" == "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_NOT_EQUAL(X, Y, msg) { if ((X) != (Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X" != "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" != "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_LESS(X, Y, msg) { if ((X) < (Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X" < "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" < "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_LESS_OR_EQUAL(X, Y, msg) { if ((X) <= (Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X" <= "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" <= "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_GREATER(X, Y, msg) { if ((X) > (Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X" > "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" > "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_GREATER_OR_EQUAL(X, Y, msg) { if ((X) >= (Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST("#X" >= "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST("#X" >= "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_ALMOST_EQUAL(X, Y, msg) { if (::my::AlmostEqual(X, Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST(my::AlmostEqual("#X", "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST(my::AlmostEqual("#X", "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}
#define TEST_NOT_ALMOST_EQUAL(X, Y, msg) { if (!::my::AlmostEqual(X, Y)) {} else { \
::my::OnTestFailed(SRC(), ::my::impl::MergeMsg("TEST(!my::AlmostEqual("#X", "#Y")", \
::my::OnTestFailed(SRC(), ::my::impl::Message("TEST(!my::AlmostEqual("#X", "#Y")", \
::my::impl::Message(X, Y), \
::my::impl::Message msg));}}