Skip to content
Snippets Groups Projects
Commit 3c994f45 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Simplify backtrace output

This makes backtrace readable
parent 99394c69
No related branches found
No related tags found
1 merge request!168Simplify backtrace output
...@@ -25,7 +25,7 @@ BacktraceManager::BacktraceManager() ...@@ -25,7 +25,7 @@ BacktraceManager::BacktraceManager()
char** ptr = backtrace_symbols(buffer, ret); char** ptr = backtrace_symbols(buffer, ret);
for (int i = 2; i < ret; ++i) { for (int i = 2; i < ret; ++i) {
m_lines.push_back(ptr[i]); m_stack_lines.push_back(ptr[i]);
} }
free(ptr); free(ptr);
...@@ -35,27 +35,35 @@ std::ostream& ...@@ -35,27 +35,35 @@ std::ostream&
operator<<(std::ostream& os, const BacktraceManager& btm) operator<<(std::ostream& os, const BacktraceManager& btm)
{ {
if (BacktraceManager::s_show) { if (BacktraceManager::s_show) {
const std::vector<std::string>& lines = btm.m_lines; const std::vector<std::string>& stack_lines = btm.m_stack_lines;
const std::regex mangled_function(R"%(\(.*\+)%"); const std::regex mangled_function(R"%(\(.*\+)%");
const int width = std::log10(lines.size()) + 1; const int width = std::log10(stack_lines.size()) + 1;
for (size_t i_line = 0; i_line < lines.size(); ++i_line) { for (size_t i_stack_line = 0; i_stack_line < stack_lines.size(); ++i_stack_line) {
const auto& line = lines[i_line]; const size_t i_line = stack_lines.size() - i_stack_line - 1;
os << rang::fg::green << "[" << std::setw(width) << i_line + 1 << '/' << lines.size() << "] " << rang::fg::reset; const auto& stack_line = stack_lines[i_line];
os << rang::fg::green << "[" << std::setw(width) << i_line + 1 << '/' << stack_lines.size() << "] "
<< rang::fg::reset;
std::smatch matchex; std::smatch matchex;
if (std::regex_search(line, matchex, mangled_function)) { if (std::regex_search(stack_line, matchex, mangled_function)) {
std::string prefix = matchex.prefix().str(); std::string prefix = matchex.prefix().str();
std::string function = line.substr(matchex.position() + 1, matchex.length() - 2); std::string function = stack_line.substr(matchex.position() + 1, matchex.length() - 2);
std::string suffix = matchex.suffix().str(); std::string suffix = matchex.suffix().str();
os << prefix << '('; os << prefix << '\n';
os << std::setw(5 + 2 * width) << "from " << '(';
if (function.size() > 0) { if (function.size() > 0) {
os << rang::style::bold << demangle(function) << rang::style::reset; std::string function_full_name = demangle(function);
if (function_full_name.size() > 80) {
function_full_name.resize(75);
function_full_name += "[...]";
}
os << rang::style::bold << function_full_name << rang::style::reset;
} }
os << '+' << suffix << '\n'; os << '+' << suffix << '\n';
} else { } else {
os << line << '\n'; os << stack_line << '\n';
} }
} }
} else { } else {
......
...@@ -8,7 +8,7 @@ class BacktraceManager ...@@ -8,7 +8,7 @@ class BacktraceManager
{ {
private: private:
static bool s_show; static bool s_show;
std::vector<std::string> m_lines; std::vector<std::string> m_stack_lines;
public: public:
static void setShow(bool show_backtrace); static void setShow(bool show_backtrace);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment