36#if TRINITY_COMPILER_IS_MICROSOFT
37#define Unreachable() __assume(false)
38#define GetReturnAddress() _ReturnAddress()
40#define Unreachable() __builtin_unreachable()
41#define GetReturnAddress() __builtin_return_address(0)
44#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
47#define Crash(message) \
48 ULONG_PTR execeptionArgs[] = { reinterpret_cast<ULONG_PTR>(strdup(message)), reinterpret_cast<ULONG_PTR>(GetReturnAddress()) }; \
49 RaiseException(EXCEPTION_ASSERTION_FAILURE, 0, 2, execeptionArgs); \
53extern "C" {
TC_COMMON_API char const* TrinityAssertionFailedMessage =
nullptr; }
54#define Crash(message) \
55 TrinityAssertionFailedMessage = strdup(message); \
56 *((volatile int*)nullptr) = 0; \
62 void FormatAssertionMessageTo(std::string& formatted,
char const* format, va_list args)
noexcept
67 int32 length = vsnprintf(
nullptr, 0, format, len);
70 std::size_t offset = formatted.length();
71 formatted.resize(offset + length);
72 vsnprintf(&formatted[offset], length + 1, format, args);
78void Assert(
char const* file,
int line,
char const* function,
char const* message, std::string debugInfo)
noexcept
80 std::string formattedMessage =
StringFormat(
"\n{}:{} in {} ASSERTION FAILED:\n {}\n{}\n", file, line, function, message, debugInfo);
81 fprintf(stderr,
"%s", formattedMessage.c_str());
83 Crash(formattedMessage.c_str());
86void Assert(
char const* file,
int line,
char const* function,
char const* message, std::string debugInfo,
char const* format, ...) noexcept
89 va_start(args, format);
91 std::string formattedMessage =
StringFormat(
"\n{}:{} in {} ASSERTION FAILED:\n {}\n", file, line, function, message);
92 FormatAssertionMessageTo(formattedMessage, format, args);
95 formattedMessage.append(1,
'\n');
96 formattedMessage.append(debugInfo);
97 formattedMessage.append(1,
'\n');
99 fprintf(stderr,
"%s", formattedMessage.c_str());
102 Crash(formattedMessage.c_str());
105void Fatal(
char const* file,
int line,
char const* function,
char const* message, ...) noexcept
108 va_start(args, message);
110 std::string formattedMessage =
StringFormat(
"\n{}:{} in {} FATAL ERROR:\n", file, line, function);
111 FormatAssertionMessageTo(formattedMessage, message, args);
114 formattedMessage.append(1,
'\n');
116 fprintf(stderr,
"%s", formattedMessage.c_str());
119 std::this_thread::sleep_for(std::chrono::seconds(10));
120 Crash(formattedMessage.c_str());
123void Error(
char const* file,
int line,
char const* function,
char const* message)
noexcept
125 std::string formattedMessage =
StringFormat(
"\n{}:{} in {} ERROR:\n {}\n", file, line, function, message);
126 fprintf(stderr,
"%s", formattedMessage.c_str());
128 Crash(formattedMessage.c_str());
131void Warning(
char const* file,
int line,
char const* function,
char const* message)
noexcept
133 fprintf(stderr,
"\n%s:%i in %s WARNING:\n %s\n",
134 file, line, function, message);
137void Abort(
char const* file,
int line,
char const* function)
noexcept
139 std::string formattedMessage =
StringFormat(
"\n{}:{} in {} ABORTED.\n", file, line, function);
140 fprintf(stderr,
"%s", formattedMessage.c_str());
142 Crash(formattedMessage.c_str());
145void Abort(
char const* file,
int line,
char const* function,
char const* message, ...) noexcept
148 va_start(args, message);
150 std::string formattedMessage =
StringFormat(
"\n{}:{} in {} ABORTED:\n", file, line, function);
151 FormatAssertionMessageTo(formattedMessage, message, args);
154 formattedMessage.append(1,
'\n');
156 fprintf(stderr,
"%s", formattedMessage.c_str());
159 Crash(formattedMessage.c_str());
165 std::string formattedMessage =
StringFormat(
"Caught signal {}\n", sigval);
166 fprintf(stderr,
"%s", formattedMessage.c_str());
168 Crash(formattedMessage.c_str());
std::string GetDebugInfo()
void Assert(char const *file, int line, char const *function, char const *message, std::string debugInfo) noexcept
void Error(char const *file, int line, char const *function, char const *message) noexcept
std::string StringFormat(FormatString< Args... > fmt, Args &&... args) noexcept
Default TC string format function.
void Abort(char const *file, int line, char const *function) noexcept
void Fatal(char const *file, int line, char const *function, char const *message,...) noexcept
void AbortHandler(int sigval) noexcept