From 3c994f4575c2d19e1e0e4f1867d551bf4781c5aa Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Thu, 25 May 2023 23:56:03 +0200 Subject: [PATCH] Simplify backtrace output This makes backtrace readable --- src/utils/BacktraceManager.cpp | 30 +++++++++++++++++++----------- src/utils/BacktraceManager.hpp | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/utils/BacktraceManager.cpp b/src/utils/BacktraceManager.cpp index 0ba95d6a6..a7142a45c 100644 --- a/src/utils/BacktraceManager.cpp +++ b/src/utils/BacktraceManager.cpp @@ -25,7 +25,7 @@ BacktraceManager::BacktraceManager() char** ptr = backtrace_symbols(buffer, ret); for (int i = 2; i < ret; ++i) { - m_lines.push_back(ptr[i]); + m_stack_lines.push_back(ptr[i]); } free(ptr); @@ -35,27 +35,35 @@ std::ostream& operator<<(std::ostream& os, const BacktraceManager& btm) { 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 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) { - const auto& line = lines[i_line]; - os << rang::fg::green << "[" << std::setw(width) << i_line + 1 << '/' << lines.size() << "] " << rang::fg::reset; + for (size_t i_stack_line = 0; i_stack_line < stack_lines.size(); ++i_stack_line) { + const size_t i_line = stack_lines.size() - i_stack_line - 1; + 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; - 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 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(); - os << prefix << '('; + os << prefix << '\n'; + os << std::setw(5 + 2 * width) << "from " << '('; 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'; } else { - os << line << '\n'; + os << stack_line << '\n'; } } } else { diff --git a/src/utils/BacktraceManager.hpp b/src/utils/BacktraceManager.hpp index e990ffd3b..3f5ac23f8 100644 --- a/src/utils/BacktraceManager.hpp +++ b/src/utils/BacktraceManager.hpp @@ -8,7 +8,7 @@ class BacktraceManager { private: static bool s_show; - std::vector<std::string> m_lines; + std::vector<std::string> m_stack_lines; public: static void setShow(bool show_backtrace); -- GitLab