123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #ifndef BASE_CHECK_H_
- #define BASE_CHECK_H_
- #include <iosfwd>
- #include "base/base_export.h"
- #include "base/compiler_specific.h"
- #include "base/immediate_crash.h"
- namespace logging {
- class VoidifyStream {
- public:
- VoidifyStream() = default;
- explicit VoidifyStream(bool ignored) {}
-
- void operator&(std::ostream&) {}
- };
- #define LAZY_CHECK_STREAM(stream, condition) \
- !(condition) ? (void)0 : ::logging::VoidifyStream() & (stream)
- #define EAT_CHECK_STREAM_PARAMS(expr) \
- true ? (void)0 \
- : ::logging::VoidifyStream(expr) & (*::logging::g_swallow_stream)
- BASE_EXPORT extern std::ostream* g_swallow_stream;
- class CheckOpResult;
- class LogMessage;
- class BASE_EXPORT CheckError {
- public:
- static CheckError Check(const char* file, int line, const char* condition);
- static CheckError CheckOp(const char* file, int line, CheckOpResult* result);
- static CheckError DCheck(const char* file, int line, const char* condition);
- static CheckError DCheckOp(const char* file, int line, CheckOpResult* result);
- static CheckError PCheck(const char* file, int line, const char* condition);
- static CheckError PCheck(const char* file, int line);
- static CheckError DPCheck(const char* file, int line, const char* condition);
- static CheckError NotImplemented(const char* file,
- int line,
- const char* function);
-
- std::ostream& stream();
- ~CheckError();
- CheckError(const CheckError& other) = delete;
- CheckError& operator=(const CheckError& other) = delete;
- CheckError(CheckError&& other) = default;
- CheckError& operator=(CheckError&& other) = default;
- private:
- explicit CheckError(LogMessage* log_message);
- LogMessage* log_message_;
- };
- #if defined(OFFICIAL_BUILD) && defined(NDEBUG)
- #define CHECK(condition) \
- UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_CHECK_STREAM_PARAMS()
- #define PCHECK(condition) \
- LAZY_CHECK_STREAM( \
- ::logging::CheckError::PCheck(__FILE__, __LINE__).stream(), \
- UNLIKELY(!(condition)))
- #else
- #define CHECK(condition) \
- LAZY_CHECK_STREAM( \
- ::logging::CheckError::Check(__FILE__, __LINE__, #condition).stream(), \
- !ANALYZER_ASSUME_TRUE(condition))
- #define PCHECK(condition) \
- LAZY_CHECK_STREAM( \
- ::logging::CheckError::PCheck(__FILE__, __LINE__, #condition).stream(), \
- !ANALYZER_ASSUME_TRUE(condition))
- #endif
- #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
- #define DCHECK_IS_ON() false
- #else
- #define DCHECK_IS_ON() true
- #endif
- #if DCHECK_IS_ON()
- #define DCHECK(condition) \
- LAZY_CHECK_STREAM( \
- ::logging::CheckError::DCheck(__FILE__, __LINE__, #condition).stream(), \
- !ANALYZER_ASSUME_TRUE(condition))
- #define DPCHECK(condition) \
- LAZY_CHECK_STREAM( \
- ::logging::CheckError::DPCheck(__FILE__, __LINE__, #condition).stream(), \
- !ANALYZER_ASSUME_TRUE(condition))
- #else
- #define DCHECK(condition) EAT_CHECK_STREAM_PARAMS(!(condition))
- #define DPCHECK(condition) EAT_CHECK_STREAM_PARAMS(!(condition))
- #endif
- BASE_EXPORT void RawCheck(const char* message);
- #define RAW_CHECK(condition) \
- do { \
- if (!(condition)) \
- ::logging::RawCheck("Check failed: " #condition "\n"); \
- } while (0)
- }
- #endif
|