2015-07-22 13:38:36 +02:00
|
|
|
#pragma once
|
2015-10-15 12:58:16 +02:00
|
|
|
#ifndef MESSMER_CPPUTILS_ASSERT_ASSERT_H
|
|
|
|
#define MESSMER_CPPUTILS_ASSERT_ASSERT_H
|
2015-07-22 13:38:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This implements an ASSERT(expr, msg) macro.
|
|
|
|
* In a debug build, it will crash and halt the program on an assert failure.
|
|
|
|
* In a release build, it will throw an AssertFailed exception instead, which can then be caught.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AssertFailed.h"
|
|
|
|
#include <iostream>
|
2015-10-05 15:46:50 +02:00
|
|
|
#include "backtrace.h"
|
2015-10-17 03:08:56 +02:00
|
|
|
#include "../logging/logging.h"
|
2015-07-22 13:38:36 +02:00
|
|
|
|
|
|
|
namespace cpputils {
|
|
|
|
namespace _assert {
|
|
|
|
inline std::string format(const char *expr, const char *message, const char *file, int line) {
|
2015-10-05 15:46:50 +02:00
|
|
|
std::string result = std::string()+"Assertion ["+expr+"] failed in "+file+":"+std::to_string(line)+": "+message+"\n\n" + backtrace();
|
2015-09-29 22:43:55 +02:00
|
|
|
return result;
|
2015-07-22 13:38:36 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 20:27:46 +02:00
|
|
|
inline void assert_fail_release [[noreturn]] (const char *expr, const char *message, const char *file, int line) {
|
2015-10-17 03:08:56 +02:00
|
|
|
auto msg = format(expr, message, file, line);
|
|
|
|
using namespace logging;
|
|
|
|
LOG(ERROR) << msg;
|
|
|
|
throw AssertFailed(msg);
|
2015-07-22 13:38:36 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 20:27:46 +02:00
|
|
|
inline void assert_fail_debug [[noreturn]] (const char *expr, const char *message, const char *file, int line) {
|
2015-10-17 03:08:56 +02:00
|
|
|
using namespace logging;
|
|
|
|
LOG(ERROR) << format(expr, message, file, line);
|
2015-07-22 13:38:36 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
//TODO Check whether disabling assertions in prod affects speed.
|
|
|
|
# define ASSERT(expr, msg) (void)((expr) || (cpputils::_assert::assert_fail_release(#expr, msg, __FILE__, __LINE__),0))
|
|
|
|
#else
|
|
|
|
# define ASSERT(expr, msg) (void)((expr) || (cpputils::_assert::assert_fail_debug(#expr, msg, __FILE__, __LINE__),0))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|