Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
assert.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2
3#include <helios/assert.hpp>
6
7#include <cstdio>
8#include <cstdlib>
9#include <format>
10#include <source_location>
11#include <string>
12#include <string_view>
13
14#if defined(__cpp_lib_print) && (__cpp_lib_print >= 202302L)
15#include <print>
16#endif
17
18namespace helios {
19
20namespace {
21
23
25 const std::source_location& loc) noexcept {
26 auto config = StacktraceConfig::FromSourceLocation(loc);
27 config.start_frame = 1;
28 config.max_frames = kDefaultAssertionStacktraceFrames;
29 config.stop_before = "__libc_start_main";
30 return config;
31}
32
33} // namespace
34
35namespace details {
36
37std::string FormatAssertionMessage(std::string_view condition,
38 const std::source_location& loc,
39 std::string_view message) {
40 std::string result;
41 result.reserve(256);
42
43 if (!message.empty()) {
44 std::format_to(std::back_inserter(result), "Assertion failed: {} | {}",
45 condition, message);
46 } else {
47 std::format_to(std::back_inserter(result), "Assertion failed: {}",
48 condition);
49 }
50
51 const std::string_view filename = utils::GetFileName(loc.file_name());
52 std::format_to(std::back_inserter(result), " [{}:{}]", filename, loc.line());
53
54#ifdef HELIOS_ENABLE_STACKTRACE
55 try {
56 const auto stacktrace =
57 Stacktrace::Capture(BuildAssertionStacktraceConfig(loc));
58 std::format_to(std::back_inserter(result), "\n{}", stacktrace.ToString());
59 } catch (...) {
60 result.append("\nStack trace: <error>");
61 }
62#endif
63
64 return result;
65}
66
67} // namespace details
68
69void AbortWithStacktrace(std::string_view message) noexcept {
70#if defined(__cpp_lib_print) && (__cpp_lib_print >= 202302L)
71 std::println(stderr, "\n=== FATAL ERROR ===");
72 std::println(stderr, "Message: {}", message);
73
74#ifdef HELIOS_ENABLE_STACKTRACE
75 const auto stacktrace = Stacktrace::Capture(
76 BuildAssertionStacktraceConfig(std::source_location::current()));
77 std::println(stderr, "\n{}", stacktrace.ToString());
78#else
79 std::println(
80 stderr,
81 "\nStack trace: <not available - build with HELIOS_ENABLE_STACKTRACE>");
82#endif
83
84 std::println(stderr, "===================\n");
85#else
86 std::fprintf(stderr, "\n=== FATAL ERROR ===\n");
87 std::fprintf(stderr, "Message: %.*s\n", static_cast<int>(message.size()),
88 message.data());
89
90#ifdef HELIOS_ENABLE_STACKTRACE
91 const auto stacktrace = Stacktrace::Capture(
92 BuildAssertionStacktraceConfig(std::source_location::current()));
93 const std::string text = stacktrace.ToString();
94 std::fprintf(stderr, "\n%s\n", text.c_str());
95#else
96 std::fprintf(
97 stderr,
98 "\nStack trace: <not available - build with HELIOS_ENABLE_STACKTRACE>\n");
99#endif
100
101 std::fprintf(stderr, "===================\n\n");
102#endif
103 std::fflush(stderr);
104
106 std::abort();
107}
108
109namespace details {
110
111#ifndef _MSC_VER
112
113#if defined(__GNUC__) || defined(__clang__)
114[[gnu::weak]]
115#endif
116bool HasLogPluginHandler() noexcept {
117 return false;
118}
119
120#if defined(__GNUC__) || defined(__clang__)
121[[gnu::weak]]
122#endif
124 [[maybe_unused]] std::string_view condition,
125 [[maybe_unused]] const std::source_location& loc,
126 [[maybe_unused]] std::string_view message) noexcept {
127}
128
129#endif // !_MSC_VER
130
131} // namespace details
132
133} // namespace helios
static Stacktrace Capture(const StacktraceConfig &config={})
Captures stacktrace using provided config.
constexpr size_t kDefaultAssertionStacktraceFrames
Definition assert.cpp:22
constexpr StacktraceConfig BuildAssertionStacktraceConfig(const std::source_location &loc) noexcept
Definition assert.cpp:24
bool HasLogPluginHandler() noexcept
Log plugin assertion handler (weak symbol).
Definition assert.cpp:116
std::string FormatAssertionMessage(std::string_view condition, const std::source_location &loc, std::string_view message)
Formats a complete assertion failure message with source location and stack trace.
Definition assert.cpp:37
void LogPluginAssertionHandler(std::string_view condition, const std::source_location &loc, std::string_view message) noexcept
Weak symbol for log plugin assertion handler.
Definition assert.cpp:123
constexpr std::string_view GetFileName(std::string_view path)
Extracts the file name from a given path.
void AbortWithStacktrace(std::string_view message) noexcept
Prints a message with stack trace and aborts the program execution.
Definition assert.cpp:69
#define HELIOS_DEBUG_BREAK()
Definition platform.hpp:89
Configuration for stacktrace capture and formatting.
static constexpr StacktraceConfig FromSourceLocation(const std::source_location &loc) noexcept