2014-11-01 04:54:55 +00:00
|
|
|
#ifndef CHECK_H_
|
|
|
|
#define CHECK_H_
|
|
|
|
|
2015-03-06 19:07:24 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
#include "internal_macros.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
namespace benchmark {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-05-23 21:05:55 +00:00
|
|
|
typedef void(AbortHandlerT)();
|
|
|
|
|
2016-05-23 21:27:43 +00:00
|
|
|
inline AbortHandlerT*& GetAbortHandler() {
|
2016-05-23 21:05:55 +00:00
|
|
|
static AbortHandlerT* handler = &std::abort;
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:27:43 +00:00
|
|
|
BENCHMARK_NORETURN inline void CallAbortHandler() {
|
|
|
|
GetAbortHandler()();
|
2016-05-23 21:05:55 +00:00
|
|
|
std::abort(); // fallback to enforce noreturn
|
|
|
|
}
|
|
|
|
|
2015-03-06 19:07:24 +00:00
|
|
|
// CheckHandler is the class constructed by failing CHECK macros. CheckHandler
|
|
|
|
// will log information about the failures and abort when it is destructed.
|
|
|
|
class CheckHandler {
|
|
|
|
public:
|
|
|
|
CheckHandler(const char* check, const char* file, const char* func, int line)
|
|
|
|
: log_(GetErrorLogInstance())
|
|
|
|
{
|
|
|
|
log_ << file << ":" << line << ": " << func << ": Check `"
|
|
|
|
<< check << "' failed. ";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& GetLog() {
|
|
|
|
return log_;
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:05:55 +00:00
|
|
|
BENCHMARK_NORETURN ~CheckHandler() noexcept(false) {
|
2015-03-06 19:07:24 +00:00
|
|
|
log_ << std::endl;
|
2016-05-23 21:27:43 +00:00
|
|
|
CallAbortHandler();
|
2015-03-06 19:07:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:05:55 +00:00
|
|
|
CheckHandler & operator=(const CheckHandler&) = delete;
|
|
|
|
CheckHandler(const CheckHandler&) = delete;
|
2015-10-05 12:05:06 +00:00
|
|
|
CheckHandler() = delete;
|
2015-03-06 19:07:24 +00:00
|
|
|
private:
|
|
|
|
std::ostream& log_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
} // end namespace benchmark
|
|
|
|
|
|
|
|
// The CHECK macro returns a std::ostream object that can have extra information
|
|
|
|
// written to it.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
# define CHECK(b) (b ? ::benchmark::internal::GetNullLogInstance() \
|
|
|
|
: ::benchmark::internal::CheckHandler( \
|
|
|
|
#b, __FILE__, __func__, __LINE__).GetLog())
|
|
|
|
#else
|
|
|
|
# define CHECK(b) ::benchmark::internal::GetNullLogInstance()
|
|
|
|
#endif
|
2014-11-01 04:54:55 +00:00
|
|
|
|
|
|
|
#define CHECK_EQ(a, b) CHECK((a) == (b))
|
|
|
|
#define CHECK_NE(a, b) CHECK((a) != (b))
|
|
|
|
#define CHECK_GE(a, b) CHECK((a) >= (b))
|
|
|
|
#define CHECK_LE(a, b) CHECK((a) <= (b))
|
|
|
|
#define CHECK_GT(a, b) CHECK((a) > (b))
|
|
|
|
#define CHECK_LT(a, b) CHECK((a) < (b))
|
|
|
|
|
|
|
|
#endif // CHECK_H_
|