From e423f7fdd794dc2816f8b4e648d82ce89818605f Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Fri, 27 Aug 2021 16:47:38 +0200
Subject: [PATCH] Remove automatic backtrace display on failure

To display backtrace on failure one must now invoke pugs with the
--backtrace (or -b)  option
---
 src/utils/BacktraceManager.cpp | 53 +++++++++++++++++++++-------------
 src/utils/BacktraceManager.hpp |  3 ++
 src/utils/PugsUtils.cpp        |  5 ++++
 3 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/src/utils/BacktraceManager.cpp b/src/utils/BacktraceManager.cpp
index 26219cbd7..0ba95d6a6 100644
--- a/src/utils/BacktraceManager.cpp
+++ b/src/utils/BacktraceManager.cpp
@@ -8,6 +8,14 @@
 #include <rang.hpp>
 #include <regex>
 
+bool BacktraceManager::s_show = false;
+
+void
+BacktraceManager::setShow(bool show_backtrace)
+{
+  s_show = show_backtrace;
+}
+
 BacktraceManager::BacktraceManager()
 {
   const int size = 100;
@@ -26,28 +34,33 @@ BacktraceManager::BacktraceManager()
 std::ostream&
 operator<<(std::ostream& os, const BacktraceManager& btm)
 {
-  const std::vector<std::string>& lines = btm.m_lines;
-
-  const std::regex mangled_function(R"%(\(.*\+)%");
-  const int width = std::log10(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;
-    std::smatch matchex;
-    if (std::regex_search(line, matchex, mangled_function)) {
-      std::string prefix   = matchex.prefix().str();
-      std::string function = line.substr(matchex.position() + 1, matchex.length() - 2);
-      std::string suffix   = matchex.suffix().str();
-
-      os << prefix << '(';
-      if (function.size() > 0) {
-        os << rang::style::bold << demangle(function) << rang::style::reset;
+  if (BacktraceManager::s_show) {
+    const std::vector<std::string>& lines = btm.m_lines;
+
+    const std::regex mangled_function(R"%(\(.*\+)%");
+    const int width = std::log10(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;
+      std::smatch matchex;
+      if (std::regex_search(line, matchex, mangled_function)) {
+        std::string prefix   = matchex.prefix().str();
+        std::string function = line.substr(matchex.position() + 1, matchex.length() - 2);
+        std::string suffix   = matchex.suffix().str();
+
+        os << prefix << '(';
+        if (function.size() > 0) {
+          os << rang::style::bold << demangle(function) << rang::style::reset;
+        }
+        os << '+' << suffix << '\n';
+      } else {
+        os << line << '\n';
       }
-      os << '+' << suffix << '\n';
-    } else {
-      os << line << '\n';
     }
+  } else {
+    os << rang::fg::yellow << "\n[To display backtrace launch pugs with the --backtrace option]" << rang::style::reset
+       << '\n';
   }
 
   return os;
diff --git a/src/utils/BacktraceManager.hpp b/src/utils/BacktraceManager.hpp
index 0ff2d2d42..e990ffd3b 100644
--- a/src/utils/BacktraceManager.hpp
+++ b/src/utils/BacktraceManager.hpp
@@ -7,9 +7,12 @@
 class BacktraceManager
 {
  private:
+  static bool s_show;
   std::vector<std::string> m_lines;
 
  public:
+  static void setShow(bool show_backtrace);
+
   BacktraceManager();
 
   friend std::ostream& operator<<(std::ostream& os, const BacktraceManager& btm);
diff --git a/src/utils/PugsUtils.cpp b/src/utils/PugsUtils.cpp
index 696dd8f86..c4372c47d 100644
--- a/src/utils/PugsUtils.cpp
+++ b/src/utils/PugsUtils.cpp
@@ -1,5 +1,6 @@
 #include <utils/PugsUtils.hpp>
 
+#include <utils/BacktraceManager.hpp>
 #include <utils/BuildInfo.hpp>
 #include <utils/ConsoleManager.hpp>
 #include <utils/FPEManager.hpp>
@@ -104,6 +105,9 @@ initialize(int& argc, char* argv[])
 
     app.add_flag("--fpe,!--no-fpe", enable_fpe, "Trap floating point exceptions [default: true]");
 
+    bool show_backtrace = false;
+    app.add_flag("-b,--backtrace,!--no-backtrace", show_backtrace, "Show backtrace on failure [default: false]");
+
     app.add_flag("--signal,!--no-signal", enable_signals, "Catches signals [default: true]");
 
     bool pause_on_error = false;
@@ -118,6 +122,7 @@ initialize(int& argc, char* argv[])
       std::exit(app.exit(e, std::cout, std::cerr));
     }
 
+    BacktraceManager::setShow(show_backtrace);
     ConsoleManager::init(enable_color);
     SignalManager::setPauseForDebug(pause_on_error);
   }
-- 
GitLab