81 lines
2.3 KiB
C++
81 lines
2.3 KiB
C++
/*
|
|
* Copyright 2015 - 2025 gary@drinkingtea.net
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "fmt.hpp"
|
|
#include "realstd.hpp"
|
|
#include "stacktrace.hpp"
|
|
#include "trace.hpp"
|
|
|
|
#include "assert.hpp"
|
|
|
|
namespace ox {
|
|
|
|
void panic(StringViewCR file, int const line, StringViewCR panicMsg, Error const&err) noexcept {
|
|
oxErrf("\033[31;1;1mPANIC:\033[0m [{}:{}]: {}\n", file, line, panicMsg);
|
|
if (err.msg) {
|
|
oxErrf("\tError Message:\t{}\n", err.msg);
|
|
}
|
|
oxErrf("\tError Code:\t{}\n", static_cast<ErrorCode>(err));
|
|
if (err.src.file_name() != nullptr) {
|
|
oxErrf("\tError Location:\t{}:{}\n", err.src.file_name(), err.src.line());
|
|
}
|
|
#ifdef OX_USE_STDLIB
|
|
printStackTrace(2);
|
|
oxTrace("panic").del("") << "Panic: " << panicMsg << " (" << file << ":" << line << ")";
|
|
std::abort();
|
|
#else
|
|
while (1);
|
|
#endif
|
|
}
|
|
|
|
void panic(const char *file, int const line, char const*panicMsg, Error const&err) noexcept {
|
|
panic(StringView{file}, line, StringView{panicMsg}, err);
|
|
}
|
|
|
|
void assertFailFuncRuntime(
|
|
StringViewCR file,
|
|
int const line,
|
|
StringViewCR assertTxt,
|
|
StringViewCR msg) noexcept {
|
|
#ifdef OX_USE_STDLIB
|
|
auto const st = genStackTrace(2);
|
|
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]:\n{}", msg, assertTxt, file, line, st);
|
|
abort();
|
|
#else
|
|
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg);
|
|
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line);
|
|
constexprPanic(file, line, msg);
|
|
#endif
|
|
}
|
|
|
|
void assertFailFuncRuntime(
|
|
StringViewCR file,
|
|
int const line,
|
|
[[maybe_unused]] Error const&err,
|
|
StringViewCR,
|
|
StringViewCR assertMsg) noexcept {
|
|
#if defined(OX_USE_STDLIB)
|
|
auto msg = sfmt("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, assertMsg);
|
|
if (err.msg) {
|
|
msg += sfmt("\tError Message:\t{}\n", err.msg);
|
|
}
|
|
msg += sfmt("\tError Code:\t{}\n", static_cast<ErrorCode>(err));
|
|
if (err.src.file_name() != nullptr) {
|
|
msg += sfmt("\tError Location:\t{}:{}\n", err.src.file_name(), err.src.line());
|
|
}
|
|
msg += genStackTrace(2);
|
|
oxErr(msg);
|
|
oxTracef("assert", "Failed assert: {} [{}:{}]", assertMsg, file, line);
|
|
abort();
|
|
#else
|
|
constexprPanic(file, line, assertMsg);
|
|
#endif
|
|
}
|
|
|
|
}
|