1 parent
f0825fbd1a
commit
42268c6c35
45 files changed
+2732
-262
No files matched your search
@@ -1,64 +1,112 @@
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#include <Void/Stacktrace/Color.hpp>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <format>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#define BOOST_STACKTRACE_USE_BACKTRACE
|
||||
#include <boost/stacktrace.hpp>
|
||||
#include <boost/core/demangle.hpp>
|
||||
#include <boost/stacktrace.hpp>
|
||||
|
||||
namespace Void::Stacktrace {
|
||||
namespace Void::Stacktrace
|
||||
{
|
||||
|
||||
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames, unsigned max_frames) {
|
||||
std::vector<StackFrame> result;
|
||||
try {
|
||||
boost::stacktrace::stacktrace bt(skip_frames + 1, max_frames);
|
||||
for (auto it = bt.begin(); it != bt.end(); ++it) {
|
||||
StackFrame frame;
|
||||
std::string name = it->name();
|
||||
if (!name.empty()) frame.function_name = boost::core::demangle(name.c_str());
|
||||
frame.source_file = it->source_file(); frame.source_line = it->source_line();
|
||||
if (!frame.source_file.empty() && frame.source_line > 0) frame.source_snippet = read_source_snippet(frame.source_file, frame.source_line, 2);
|
||||
result.push_back(std::move(frame));
|
||||
}
|
||||
} catch (...) {}
|
||||
return result;
|
||||
}
|
||||
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames,
|
||||
unsigned max_frames)
|
||||
{
|
||||
std::vector<StackFrame> result;
|
||||
try {
|
||||
boost::stacktrace::stacktrace bt(skip_frames + 1, max_frames);
|
||||
for (auto it = bt.begin(); it != bt.end(); ++it) {
|
||||
StackFrame frame;
|
||||
std::string name = it->name();
|
||||
if (!name.empty()) {
|
||||
frame.function_name = boost::core::demangle(name.c_str());
|
||||
}
|
||||
frame.source_file = it->source_file();
|
||||
frame.source_line = it->source_line();
|
||||
if (!frame.source_file.empty() && frame.source_line > 0) {
|
||||
frame.source_snippet =
|
||||
read_source_snippet(frame.source_file,
|
||||
frame.source_line, 2)
|
||||
.value_or(std::string{});
|
||||
}
|
||||
result.push_back(std::move(frame));
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string read_source_snippet(const std::string& file, int line, int context_lines) {
|
||||
std::ifstream ifs{file}; if (!ifs.is_open()) return {};
|
||||
std::ostringstream result; std::string current_line; int line_num = 1;
|
||||
int start = std::max(1, line - context_lines); int end = line + context_lines;
|
||||
while (std::getline(ifs, current_line)) {
|
||||
if (line_num >= start && line_num <= end) {
|
||||
std::string prefix = (line_num == line) ? " >>> " : " ";
|
||||
result << std::format("{:>4}{}{}\n", line_num, prefix, current_line);
|
||||
}
|
||||
if (line_num > end) break; ++line_num;
|
||||
}
|
||||
return result.str();
|
||||
}
|
||||
Void::Result::Result<std::string, Void::Result::Error>
|
||||
read_source_snippet(const std::string& file, int line,
|
||||
int context_lines)
|
||||
{
|
||||
std::ifstream ifs{file};
|
||||
if (!ifs.is_open()) {
|
||||
return Void::Result::fail(
|
||||
std::format("read_source_snippet: cannot open '{}'", file));
|
||||
}
|
||||
std::ostringstream result;
|
||||
std::string current_line;
|
||||
int line_num = 1;
|
||||
int start = std::max(1, line - context_lines);
|
||||
int end = line + context_lines;
|
||||
while (std::getline(ifs, current_line)) {
|
||||
if (line_num >= start && line_num <= end) {
|
||||
std::string prefix = (line_num == line) ? " >>> " : " ";
|
||||
result << std::format("{:>4}{}{}\n", line_num, prefix,
|
||||
current_line);
|
||||
}
|
||||
if (line_num > end) {
|
||||
break;
|
||||
}
|
||||
++line_num;
|
||||
}
|
||||
return Void::Result::ok(result.str());
|
||||
}
|
||||
|
||||
std::string format_stacktrace(const std::vector<StackFrame>& frames) {
|
||||
if (frames.empty()) return " <no stacktrace available>\n";
|
||||
std::ostringstream oss;
|
||||
for (size_t i = 0; i < frames.size(); ++i) {
|
||||
const auto& frame = frames[i];
|
||||
oss << std::format(" #{:02} ", i);
|
||||
if (!frame.function_name.empty()) oss << (Color::g_color_enabled ? Color::apply(Color::bold_cyan, frame.function_name) : frame.function_name);
|
||||
else oss << "<unknown>";
|
||||
if (!frame.source_file.empty() && frame.source_line > 0) {
|
||||
std::filesystem::path p{frame.source_file};
|
||||
oss << "\n at " << (Color::g_color_enabled ? Color::apply(Color::dim, p.filename().string()) : p.filename().string()) << ":" << frame.source_line;
|
||||
}
|
||||
oss << "\n";
|
||||
if (!frame.source_snippet.empty() && i < 5) oss << " " << (Color::g_color_enabled ? Color::apply(Color::dim, frame.source_snippet) : frame.source_snippet);
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
std::string format_stacktrace(const std::vector<StackFrame>& frames)
|
||||
{
|
||||
if (frames.empty()) {
|
||||
return " <no stacktrace available>\n";
|
||||
}
|
||||
std::ostringstream oss;
|
||||
for (size_t i = 0; i < frames.size(); ++i) {
|
||||
const auto& frame = frames[i];
|
||||
oss << std::format(" #{:02} ", i);
|
||||
if (!frame.function_name.empty()) {
|
||||
oss << (Color::g_color_enabled
|
||||
? Color::apply(Color::bold_cyan,
|
||||
frame.function_name)
|
||||
: frame.function_name);
|
||||
} else {
|
||||
oss << "<unknown>";
|
||||
}
|
||||
if (!frame.source_file.empty() && frame.source_line > 0) {
|
||||
std::filesystem::path p{frame.source_file};
|
||||
oss << "\n at "
|
||||
<< (Color::g_color_enabled
|
||||
? Color::apply(Color::dim, p.filename().string())
|
||||
: p.filename().string())
|
||||
<< ":" << frame.source_line;
|
||||
}
|
||||
oss << "\n";
|
||||
if (!frame.source_snippet.empty() && i < 5) {
|
||||
oss << " "
|
||||
<< (Color::g_color_enabled
|
||||
? Color::apply(Color::dim, frame.source_snippet)
|
||||
: frame.source_snippet);
|
||||
}
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames) { os << format_stacktrace(frames); }
|
||||
void print_stacktrace(std::ostream& os,
|
||||
const std::vector<StackFrame>& frames)
|
||||
{
|
||||
os << format_stacktrace(frames);
|
||||
}
|
||||
} // namespace Void::Stacktrace
|
||||
@@ -1,4 +1,5 @@
|
||||
void_add_module(Stacktrace
|
||||
INTERNAL_DEPS Result
|
||||
EXTERNAL_DEPS Boost::stacktrace_backtrace
|
||||
DEPS_WIN32 dbghelp
|
||||
DEPS_UNIX dl
|
||||
|
||||
Reference in new issue
Block a user