Improve assert implementation and output formatting

This commit is contained in:
Riyyi
2021-02-01 03:47:20 +01:00
parent 4cbf0a6811
commit 54171260b6
+19 -18
View File
@@ -1,5 +1,5 @@
#ifndef ASSERTIONS_H #ifndef ASSERT_H
#define ASSERTIONS_H #define ASSERT_H
#include <csignal> // raise #include <csignal> // raise
@@ -24,14 +24,16 @@
#define FUNCTION_MACRO __PRETTY_FUNCTION__ #define FUNCTION_MACRO __PRETTY_FUNCTION__
#elif MSVC #elif MSVC
#define FUNCTION_MACRO __FUNCSIG__ #define FUNCTION_MACRO __FUNCSIG__
#else
#define FUNCTION_MACRO __func__
#endif #endif
// ##__VA_ARGS__ is a non-standard GCC extension, C++20 introduces __VA_OPT__ // ##__VA_ARGS__ is a non-standard GCC extension, C++20 introduces __VA_OPT__
// https://stackoverflow.com/questions/52891546/what-does-va-args-mean // https://stackoverflow.com/questions/52891546/what-does-va-args-mean
#define ASSERT(cond, ...) static_cast<bool>(cond) ? (void)0 : __assertion_failed(#cond, __FILE__, __LINE__, FUNCTION_MACRO, ##__VA_ARGS__) #define ASSERT(expr, ...) static_cast<bool>(expr) ? (void)0 : Inferno::__assert_fail(#expr, __FILE__, __LINE__, FUNCTION_MACRO, ##__VA_ARGS__)
#define ASSERT_NOT_REACHED() ASSERT(false) #define ASSERT_NOT_REACHED() ASSERT(false)
#else #else
#define ASSERT(cond, ...) #define ASSERT(expr, ...)
#define ASSERT_NOT_REACHED() CRASH() #define ASSERT_NOT_REACHED() CRASH()
#endif #endif
@@ -40,23 +42,22 @@
namespace Inferno { namespace Inferno {
#ifdef NF_ENABLE_ASSERTS #ifdef NF_ENABLE_ASSERTS
template<typename... P> template<typename... P>
void __assertion_failed(const char* message, const char* file, unsigned line, const char* function, P&&... parameters) [[noreturn]] inline void __assert_fail(const char* assertion, const char* file, unsigned int line, const char* function, P&&... parameters)
{ {
danger(false) << "ASSERTION FAILED: " << message; dangerln(false, "ASSERTION `{}' FAILED.", assertion);
if (sizeof...(P) > 0) { if (sizeof...(P) > 0) {
danger(false) << ": "; dbg(false) << " ";
dbgln(Log::Danger, false, std::forward<P>(parameters)...); dbgln(Log::Danger, false, std::forward<P>(parameters)...);
danger(false); }
danger() << "\n\t" << file << ":" << line << ": " << function;
raise(ABORT_SIGNAL);
} }
danger() << "\n\t" << file << ":" << line << ": " << function;
raise(ABORT_SIGNAL);
}
#endif #endif
} }
#endif // ASSERTIONS_H #endif // ASSERT_H