2 * Copyright 2006 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef WEBRTC_BASE_CHECKS_H_
12 #define WEBRTC_BASE_CHECKS_H_
17 #ifdef WEBRTC_CHROMIUM_BUILD
18 // Include logging.h in a Chromium build to enable the overrides mechanism for
19 // using Chromium's macros. Otherwise, don't depend on logging.h.
20 // TODO(ajm): Ideally, checks.h would be combined with logging.h, but
21 // consolidation with system_wrappers/logging.h should happen first.
22 #include "webrtc/base/logging.h"
24 #include "webrtc/typedefs.h"
26 // The macros here print a message to stderr and abort under various
27 // conditions. All will accept additional stream messages. For example:
28 // DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
30 // - CHECK(x) is an assertion that x is always true, and that if it isn't, it's
31 // better to terminate the process than to continue. During development, the
32 // reason that it's better to terminate might simply be that the error
33 // handling code isn't in place yet; in production, the reason might be that
34 // the author of the code truly believes that x will always be true, but that
35 // she recognizes that if she is wrong, abrupt and unpleasant process
36 // termination is still better than carrying on with the assumption violated.
38 // CHECK always evaluates its argument, so it's OK for x to have side
41 // - DCHECK(x) is the same as CHECK(x)---an assertion that x is always
42 // true---except that x will only be evaluated in debug builds; in production
43 // builds, x is simply assumed to be true. This is useful if evaluating x is
44 // expensive and the expected cost of failing to detect the violated
45 // assumption is acceptable. You should not handle cases where a production
46 // build fails to spot a violated condition, even those that would result in
47 // crashes. If the code needs to cope with the error, make it cope, but don't
48 // call DCHECK; if the condition really can't occur, but you'd sleep better
49 // at night knowing that the process will suicide instead of carrying on in
50 // case you were wrong, use CHECK instead of DCHECK.
52 // DCHECK only evaluates its argument in debug builds, so if x has visible
53 // side effects, you need to write e.g.
54 // bool w = x; DCHECK(w);
56 // - CHECK_EQ, _NE, _GT, ..., and DCHECK_EQ, _NE, _GT, ... are specialized
57 // variants of CHECK and DCHECK that print prettier messages if the condition
58 // doesn't hold. Prefer them to raw CHECK and DCHECK.
60 // - FATAL() aborts unconditionally.
64 // The use of overrides/webrtc/base/logging.h in a Chromium build results in
65 // redefined macro errors. Fortunately, Chromium's macros can be used as drop-in
66 // replacements for the standalone versions.
67 #ifndef WEBRTC_CHROMIUM_BUILD
69 // Helper macro which avoids evaluating the arguments to a stream if
70 // the condition doesn't hold.
71 #define LAZY_STREAM(stream, condition) \
72 !(condition) ? static_cast<void>(0) : rtc::FatalMessageVoidify() & (stream)
74 // The actual stream used isn't important.
75 #define EAT_STREAM_PARAMETERS \
76 true ? static_cast<void>(0) \
77 : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream()
79 // CHECK dies with a fatal error if condition is not true. It is *not*
80 // controlled by NDEBUG, so the check will be executed regardless of
83 // We make sure CHECK et al. always evaluates their arguments, as
84 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
85 #define CHECK(condition) \
86 LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), !(condition)) \
87 << "Check failed: " #condition << std::endl << "# "
89 // Helper macro for binary operators.
90 // Don't use this macro directly in your code, use CHECK_EQ et al below.
92 // TODO(akalin): Rewrite this so that constructs like if (...)
93 // CHECK_EQ(...) else { ... } work properly.
94 #define CHECK_OP(name, op, val1, val2) \
95 if (std::string* _result = \
96 rtc::Check##name##Impl((val1), (val2), \
97 #val1 " " #op " " #val2)) \
98 rtc::FatalMessage(__FILE__, __LINE__, _result).stream()
100 // Build the error message string. This is separate from the "Impl"
101 // function template because it is not performance critical and so can
102 // be out of line, while the "Impl" code should be inline. Caller
103 // takes ownership of the returned string.
104 template<class t1, class t2>
105 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
106 std::ostringstream ss;
107 ss << names << " (" << v1 << " vs. " << v2 << ")";
108 std::string* msg = new std::string(ss.str());
112 // MSVC doesn't like complex extern templates and DLLs.
113 #if !defined(COMPILER_MSVC)
114 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
116 extern template std::string* MakeCheckOpString<int, int>(
117 const int&, const int&, const char* names);
119 std::string* MakeCheckOpString<unsigned long, unsigned long>(
120 const unsigned long&, const unsigned long&, const char* names);
122 std::string* MakeCheckOpString<unsigned long, unsigned int>(
123 const unsigned long&, const unsigned int&, const char* names);
125 std::string* MakeCheckOpString<unsigned int, unsigned long>(
126 const unsigned int&, const unsigned long&, const char* names);
128 std::string* MakeCheckOpString<std::string, std::string>(
129 const std::string&, const std::string&, const char* name);
132 // Helper functions for CHECK_OP macro.
133 // The (int, int) specialization works around the issue that the compiler
134 // will not instantiate the template version of the function on values of
135 // unnamed enum type - see comment below.
136 #define DEFINE_CHECK_OP_IMPL(name, op) \
137 template <class t1, class t2> \
138 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
139 const char* names) { \
140 if (v1 op v2) return NULL; \
141 else return rtc::MakeCheckOpString(v1, v2, names); \
143 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
144 if (v1 op v2) return NULL; \
145 else return rtc::MakeCheckOpString(v1, v2, names); \
147 DEFINE_CHECK_OP_IMPL(EQ, ==)
148 DEFINE_CHECK_OP_IMPL(NE, !=)
149 DEFINE_CHECK_OP_IMPL(LE, <=)
150 DEFINE_CHECK_OP_IMPL(LT, < )
151 DEFINE_CHECK_OP_IMPL(GE, >=)
152 DEFINE_CHECK_OP_IMPL(GT, > )
153 #undef DEFINE_CHECK_OP_IMPL
155 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
156 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
157 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
158 #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2)
159 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
160 #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2)
162 // The DCHECK macro is equivalent to CHECK except that it only generates code in
164 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
165 #define DCHECK(condition) CHECK(condition)
166 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2)
167 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2)
168 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2)
169 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2)
170 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2)
171 #define DCHECK_GT(v1, v2) CHECK_GT(v1, v2)
173 #define DCHECK(condition) EAT_STREAM_PARAMETERS
174 #define DCHECK_EQ(v1, v2) EAT_STREAM_PARAMETERS
175 #define DCHECK_NE(v1, v2) EAT_STREAM_PARAMETERS
176 #define DCHECK_LE(v1, v2) EAT_STREAM_PARAMETERS
177 #define DCHECK_LT(v1, v2) EAT_STREAM_PARAMETERS
178 #define DCHECK_GE(v1, v2) EAT_STREAM_PARAMETERS
179 #define DCHECK_GT(v1, v2) EAT_STREAM_PARAMETERS
182 // This is identical to LogMessageVoidify but in name.
183 class FatalMessageVoidify {
185 FatalMessageVoidify() { }
186 // This has to be an operator with a precedence lower than << but
188 void operator&(std::ostream&) { }
191 #endif // WEBRTC_CHROMIUM_BUILD
193 #define FATAL() rtc::FatalMessage(__FILE__, __LINE__).stream()
194 // TODO(ajm): Consider adding NOTIMPLEMENTED and NOTREACHED macros when
195 // base/logging.h and system_wrappers/logging.h are consolidated such that we
196 // can match the Chromium behavior.
198 // Like a stripped-down LogMessage from logging.h, except that it aborts.
201 FatalMessage(const char* file, int line);
202 // Used for CHECK_EQ(), etc. Takes ownership of the given string.
203 FatalMessage(const char* file, int line, std::string* result);
204 NO_RETURN ~FatalMessage();
206 std::ostream& stream() { return stream_; }
209 void Init(const char* file, int line);
211 std::ostringstream stream_;
216 #endif // WEBRTC_BASE_CHECKS_H_