summaryrefslogtreecommitdiffstats
path: root/base/logging.h
diff options
context:
space:
mode:
authorwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-01 19:52:56 +0000
committerwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-01 19:52:56 +0000
commita1e16e05fb365ad8d29ad6ed95e46bacdaaf83fb (patch)
treed0c960c21c0f7d0bb8f8333a23d9babc91623a03 /base/logging.h
parentecd9aa34054396f135981f369105af51dec02793 (diff)
downloadchromium_src-a1e16e05fb365ad8d29ad6ed95e46bacdaaf83fb.zip
chromium_src-a1e16e05fb365ad8d29ad6ed95e46bacdaaf83fb.tar.gz
chromium_src-a1e16e05fb365ad8d29ad6ed95e46bacdaaf83fb.tar.bz2
Add support for CHECK_* macros.
Convert a few places to make sure it works. Review URL: http://codereview.chromium.org/660221 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40289 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/logging.h')
-rw-r--r--base/logging.h86
1 files changed, 60 insertions, 26 deletions
diff --git a/base/logging.h b/base/logging.h
index 1aa1162..ebb2518 100644
--- a/base/logging.h
+++ b/base/logging.h
@@ -332,6 +332,55 @@ std::string* MakeCheckOpString(const int& v1,
return MakeCheckOpStringIntInt(v1, v2, names);
}
+// Helper macro for binary operators.
+// Don't use this macro directly in your code, use CHECK_EQ et al below.
+#define CHECK_OP(name, op, val1, val2) \
+ if (logging::CheckOpString _result = \
+ logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
+ logging::LogMessage(__FILE__, __LINE__, _result).stream()
+
+// Helper functions for string comparisons.
+// To avoid bloat, the definitions are in logging.cc.
+#define DECLARE_CHECK_STROP_IMPL(func, expected) \
+ std::string* Check##func##expected##Impl(const char* s1, \
+ const char* s2, \
+ const char* names);
+DECLARE_CHECK_STROP_IMPL(strcmp, true)
+DECLARE_CHECK_STROP_IMPL(strcmp, false)
+DECLARE_CHECK_STROP_IMPL(_stricmp, true)
+DECLARE_CHECK_STROP_IMPL(_stricmp, false)
+#undef DECLARE_CHECK_STROP_IMPL
+
+// Helper macro for string comparisons.
+// Don't use this macro directly in your code, use CHECK_STREQ et al below.
+#define CHECK_STROP(func, op, expected, s1, s2) \
+ while (CheckOpString _result = \
+ logging::Check##func##expected##Impl((s1), (s2), \
+ #s1 " " #op " " #s2)) \
+ LOG(FATAL) << *_result.str_
+
+// String (char*) equality/inequality checks.
+// CASE versions are case-insensitive.
+//
+// Note that "s1" and "s2" may be temporary strings which are destroyed
+// by the compiler at the end of the current "full expression"
+// (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
+
+#define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
+#define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
+#define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(_stricmp, ==, true, s1, s2)
+#define CHECK_STRCASENE(s1, s2) CHECK_STROP(_stricmp, !=, false, s1, s2)
+
+#define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0])))
+#define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0])))
+
+#define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
+#define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
+#define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
+#define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
+#define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
+#define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
+
// Plus some debug-logging macros that get compiled to nothing for production
//
// DEBUG_MODE is for uses like
@@ -444,11 +493,8 @@ enum { DEBUG_MODE = 0 };
// debug-only checking. not executed in NDEBUG mode.
enum { DEBUG_MODE = 1 };
-#define DCHECK(condition) \
- LOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". "
-
-#define DPCHECK(condition) \
- PLOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". "
+#define DCHECK(condition) CHECK(condition)
+#define DPCHECK(condition) PCHECK(condition)
// Helper macro for binary operators.
// Don't use this macro directly in your code, use DCHECK_EQ et al below.
@@ -457,18 +503,6 @@ enum { DEBUG_MODE = 1 };
logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
logging::LogMessage(__FILE__, __LINE__, _result).stream()
-// Helper functions for string comparisons.
-// To avoid bloat, the definitions are in logging.cc.
-#define DECLARE_DCHECK_STROP_IMPL(func, expected) \
- std::string* Check##func##expected##Impl(const char* s1, \
- const char* s2, \
- const char* names);
-DECLARE_DCHECK_STROP_IMPL(strcmp, true)
-DECLARE_DCHECK_STROP_IMPL(strcmp, false)
-DECLARE_DCHECK_STROP_IMPL(_stricmp, true)
-DECLARE_DCHECK_STROP_IMPL(_stricmp, false)
-#undef DECLARE_DCHECK_STROP_IMPL
-
// Helper macro for string comparisons.
// Don't use this macro directly in your code, use CHECK_STREQ et al below.
#define DCHECK_STROP(func, op, expected, s1, s2) \
@@ -560,11 +594,11 @@ extern bool g_enable_dcheck;
#endif // NDEBUG
-// Helper functions for DCHECK_OP macro.
+// Helper functions for CHECK_OP macro.
// The (int, int) specialization works around the issue that the compiler
// will not instantiate the template version of the function on values of
// unnamed enum type - see comment below.
-#define DEFINE_DCHECK_OP_IMPL(name, op) \
+#define DEFINE_CHECK_OP_IMPL(name, op) \
template <class t1, class t2> \
inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
const char* names) { \
@@ -575,13 +609,13 @@ extern bool g_enable_dcheck;
if (v1 op v2) return NULL; \
else return MakeCheckOpString(v1, v2, names); \
}
-DEFINE_DCHECK_OP_IMPL(EQ, ==)
-DEFINE_DCHECK_OP_IMPL(NE, !=)
-DEFINE_DCHECK_OP_IMPL(LE, <=)
-DEFINE_DCHECK_OP_IMPL(LT, < )
-DEFINE_DCHECK_OP_IMPL(GE, >=)
-DEFINE_DCHECK_OP_IMPL(GT, > )
-#undef DEFINE_DCHECK_OP_IMPL
+DEFINE_CHECK_OP_IMPL(EQ, ==)
+DEFINE_CHECK_OP_IMPL(NE, !=)
+DEFINE_CHECK_OP_IMPL(LE, <=)
+DEFINE_CHECK_OP_IMPL(LT, < )
+DEFINE_CHECK_OP_IMPL(GE, >=)
+DEFINE_CHECK_OP_IMPL(GT, > )
+#undef DEFINE_CHECK_OP_IMPL
// Equality/Inequality checks - compare two values, and log a LOG_FATAL message
// including the two values when the result is not as expected. The values