Stacktrace module

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-04-29 23:23:20 +03:00
1 parent e7186f17f0
commit f0825fbd1a
13 files changed
+105 -36

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <string_view>
#include <string>
namespace Void::Stacktrace::Color {
inline bool g_color_enabled{true};
constexpr std::string_view reset = "\033[0m";
constexpr std::string_view bold = "\033[1m";
constexpr std::string_view dim = "\033[2m";
constexpr std::string_view bold_red = "\033[1;31m";
constexpr std::string_view bold_green = "\033[1;32m";
constexpr std::string_view bold_yellow = "\033[1;33m";
constexpr std::string_view bold_blue = "\033[1;34m";
constexpr std::string_view bold_magenta = "\033[1;35m";
constexpr std::string_view bold_cyan = "\033[1;36m";
constexpr std::string_view bold_white = "\033[1;37m";
constexpr std::string_view red = "\033[31m";
constexpr std::string_view green = "\033[32m";
constexpr std::string_view yellow = "\033[33m";
constexpr std::string_view cyan = "\033[36m";
inline std::string apply(std::string_view code, std::string_view text) {
if (!g_color_enabled) return std::string{text};
return std::string{code} + std::string{text} + std::string{reset};
}
inline std::string success(std::string_view text) { return apply(bold_green, text); }
inline std::string fail(std::string_view text) { return apply(bold_red, text); }
inline std::string skip(std::string_view text) { return apply(bold_yellow, text); }
inline std::string info(std::string_view text) { return apply(bold_cyan, text); }
inline std::string hint(std::string_view text) { return apply(dim, text); }
inline std::string heading(std::string_view text) { return apply(bold_blue, text); }
inline std::string emphasis(std::string_view text) { return apply(bold, text); }
} // namespace Void::Stacktrace::Color
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <string>
#include <vector>
#include <ostream>
namespace Void::Stacktrace {
struct StackFrame {
std::string function_name;
std::string source_file;
int source_line{0};
std::string source_snippet;
};
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames = 0, unsigned max_frames = 32);
std::string read_source_snippet(const std::string& file, int line, int context_lines = 3);
std::string format_stacktrace(const std::vector<StackFrame>& frames);
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames);
} // namespace Void::Stacktrace