1 #ifndef CHECK_H_ 2 #define CHECK_H_ 3 4 #include <cstdlib> 5 #include <ostream> 6 7 #include "internal_macros.h" 8 #include "log.h" 9 10 namespace benchmark { 11 namespace internal { 12 13 // CheckHandler is the class constructed by failing CHECK macros. CheckHandler 14 // will log information about the failures and abort when it is destructed. 15 class CheckHandler { 16 public: CheckHandler(const char * check,const char * file,const char * func,int line)17 CheckHandler(const char* check, const char* file, const char* func, int line) 18 : log_(GetErrorLogInstance()) 19 { 20 log_ << file << ":" << line << ": " << func << ": Check `" 21 << check << "' failed. "; 22 } 23 GetLog()24 std::ostream& GetLog() { 25 return log_; 26 } 27 ~CheckHandler()28 BENCHMARK_NORETURN ~CheckHandler() { 29 log_ << std::endl; 30 std::abort(); 31 } 32 33 CheckHandler & operator=(const CheckHandler&) = delete; 34 CheckHandler(const CheckHandler&) = delete; 35 CheckHandler() = delete; 36 private: 37 std::ostream& log_; 38 }; 39 40 } // end namespace internal 41 } // end namespace benchmark 42 43 // The CHECK macro returns a std::ostream object that can have extra information 44 // written to it. 45 #ifndef NDEBUG 46 # define CHECK(b) (b ? ::benchmark::internal::GetNullLogInstance() \ 47 : ::benchmark::internal::CheckHandler( \ 48 #b, __FILE__, __func__, __LINE__).GetLog()) 49 #else 50 # define CHECK(b) ::benchmark::internal::GetNullLogInstance() 51 #endif 52 53 #define CHECK_EQ(a, b) CHECK((a) == (b)) 54 #define CHECK_NE(a, b) CHECK((a) != (b)) 55 #define CHECK_GE(a, b) CHECK((a) >= (b)) 56 #define CHECK_LE(a, b) CHECK((a) <= (b)) 57 #define CHECK_GT(a, b) CHECK((a) > (b)) 58 #define CHECK_LT(a, b) CHECK((a) < (b)) 59 60 #endif // CHECK_H_ 61