Improve CHECK_<op> logging.
This patch lets us show the lhs and rhs when a relational check fails. (I show
both, even though that looks silly in cases like CHECK_EQ(rc, 0) where we'll
say "rc=-1, 0=0", because it's helpful in cases like CHECK_LT(i, size()) where
we want "i=4, size=2".)
I had to add a few operator<<s for enums, and fix a bunch of signed/unsigned
mismatches, and put the StringPiece operator<< in the right namespace.
Change-Id: I390f38bd97b3f50e12182f36ff027ca067c48d69
diff --git a/src/logging.h b/src/logging.h
index 98eb953..0a96504 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -25,14 +25,23 @@
if (!(x)) \
LogMessage(__FILE__, __LINE__, FATAL, -1).stream() << "Check failed: " #x
-#define CHECK_EQ(x, y) CHECK((x) == (y))
-#define CHECK_NE(x, y) CHECK((x) != (y))
-#define CHECK_LE(x, y) CHECK((x) <= (y))
-#define CHECK_LT(x, y) CHECK((x) < (y))
-#define CHECK_GE(x, y) CHECK((x) >= (y))
-#define CHECK_GT(x, y) CHECK((x) > (y))
-#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
-#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
+#define CHECK_OP(LHS, RHS, OP) \
+ do { \
+ typeof (LHS) _lhs = (LHS); \
+ typeof (RHS) _rhs = (RHS); \
+ if (!(_lhs OP _rhs)) { \
+ LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
+ << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
+ << " (" #LHS "=" << _lhs << ", " #RHS "=" << _rhs << ")"; \
+ } \
+ } while (false)
+
+#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
+#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
+#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
+#define CHECK_LT(x, y) CHECK_OP(x, y, <)
+#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
+#define CHECK_GT(x, y) CHECK_OP(x, y, >)
#define CHECK_STROP(s1, s2, sense) \
do { \
@@ -44,6 +53,9 @@
} \
} while (false)
+#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
+#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
+
#ifndef NDEBUG
#define DCHECK(x) CHECK(x)