Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
stacktrace.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <source_location>
6#include <span>
7#include <string>
8#include <string_view>
9#include <vector>
10
11namespace helios {
12
13/// @brief Configuration for stacktrace capture and formatting.
15 /// Maximum number of frames to emit after filtering.
16 size_t max_frames = 10;
17
18 /// Default frame index to start from when source matching is disabled or
19 /// fails.
20 size_t start_frame = 1;
21
22 /// Additional number of frames to skip from the computed start.
23 size_t skip_frames = 0;
24
25 /**
26 * @brief Maximum number of frames to scan from start frame.
27 * @details `0` means no explicit depth limit.
28 */
29 size_t max_depth = 0;
30
31 /// @brief Source file used to infer a relevant starting frame.
32 std::string_view source_file;
33
34 /// @brief Source line used to infer a relevant starting frame.
35 uint_least32_t source_line = 0;
36
37 /// @brief Source function used to infer a relevant starting frame.
38 std::string_view source_function;
39
40 /**
41 * @brief Marker to stop capture before frames that contain this token.
42 * @details Typical values are `"__libc_start_main"`, `"_start"` or `"main"`.
43 */
44 std::string_view stop_before = "__libc_start_main";
45
46 /**
47 * @brief Keep only frames that contain this token.
48 * @details Empty token keeps all frames.
49 */
50 std::string_view include_frames_containing;
51
52 /**
53 * @brief Drop frames that contain this token.
54 * @details Empty token drops no frames.
55 */
56 std::string_view exclude_frames_containing;
57
58 [[nodiscard]] static constexpr StacktraceConfig FromSourceLocation(
59 const std::source_location& loc) noexcept {
60 StacktraceConfig config;
61 config.source_file = loc.file_name();
62 config.source_line = loc.line();
63 config.source_function = loc.function_name();
64 return config;
65 }
66
67 [[nodiscard]] static constexpr StacktraceConfig FromSource(
68 std::string_view file, uint_least32_t line) noexcept {
69 StacktraceConfig config;
70 config.source_file = file;
71 config.source_line = line;
72 return config;
73 }
74};
75
76/// @brief Captured and filtered stacktrace with string conversion support.
78public:
79 constexpr Stacktrace() = default;
80 constexpr Stacktrace(const Stacktrace&) = default;
81 constexpr Stacktrace(Stacktrace&&) noexcept = default;
82 constexpr ~Stacktrace() noexcept = default;
83
84 constexpr Stacktrace& operator=(const Stacktrace&) = default;
85 constexpr Stacktrace& operator=(Stacktrace&&) noexcept = default;
86
87 /**
88 * @brief Captures stacktrace using provided config.
89 * @param config Capture and formatting configuration
90 * @return Captured stacktrace object
91 */
92 [[nodiscard]] static Stacktrace Capture(const StacktraceConfig& config = {});
93
94 /**
95 * @brief Converts stacktrace to a formatted string.
96 * @param include_header Whether to include `Stack trace:` header
97 * @return Formatted stacktrace string
98 */
99 [[nodiscard]] std::string ToString(bool include_header = true) const;
100
101 /**
102 * @brief Checks if no frames were captured.
103 * @return True if no frames were captured, false otherwise
104 */
105 [[nodiscard]] constexpr bool Empty() const noexcept {
106 return frames_.empty();
107 }
108
109 /**
110 * @brief Returns captured frames.
111 * @return Read-only span of captured frames
112 */
113 [[nodiscard]] constexpr auto Frames() const noexcept
114 -> std::span<const std::string> {
115 return frames_;
116 }
117
118 /**
119 * @brief Returns number of captured frames.
120 * @return Number of captured frames
121 */
122 [[nodiscard]] constexpr size_t Size() const noexcept {
123 return frames_.size();
124 }
125
126private:
127 constexpr Stacktrace(std::vector<std::string> frames,
128 bool capture_error) noexcept
129 : frames_(std::move(frames)), capture_error_(capture_error) {}
130
131 std::vector<std::string> frames_;
132 bool capture_error_ = false;
133};
134
135} // namespace helios
constexpr size_t Size() const noexcept
Returns number of captured frames.
std::string ToString(bool include_header=true) const
Converts stacktrace to a formatted string.
constexpr auto Frames() const noexcept -> std::span< const std::string >
Returns captured frames.
static Stacktrace Capture(const StacktraceConfig &config={})
Captures stacktrace using provided config.
constexpr bool Empty() const noexcept
Checks if no frames were captured.
constexpr Stacktrace(const Stacktrace &)=default
constexpr Stacktrace()=default
constexpr Stacktrace(Stacktrace &&) noexcept=default
STL namespace.
Configuration for stacktrace capture and formatting.
size_t max_frames
Maximum number of frames to emit after filtering.
static constexpr StacktraceConfig FromSource(std::string_view file, uint_least32_t line) noexcept
std::string_view source_file
Source file used to infer a relevant starting frame.
std::string_view stop_before
Marker to stop capture before frames that contain this token.
static constexpr StacktraceConfig FromSourceLocation(const std::source_location &loc) noexcept
std::string_view exclude_frames_containing
Drop frames that contain this token.
std::string_view include_frames_containing
Keep only frames that contain this token.
size_t skip_frames
Additional number of frames to skip from the computed start.
size_t max_depth
Maximum number of frames to scan from start frame.
uint_least32_t source_line
Source line used to infer a relevant starting frame.
std::string_view source_function
Source function used to infer a relevant starting frame.