//********************************************************
// The following code example is taken from the book
//  C++23 - The Complete Guide
//  by Nicolai M. Josuttis (www.josuttis.com)
//  http://www.cppstd23.com
//
// The code is licensed under a
//  Creative Commons Attribution 4.0 International License
//  http://creativecommons.org/licenses/by/4.0/
//********************************************************


#include <print>
#include <stacktrace>
#include <regex>

void printStacktrace(const std::stacktrace& st)
{
  for (const std::stacktrace_entry& entry : st) {
    auto file = entry.source_file();    // std::string
    if (!file.empty()) {
      auto line = entry.source_line();  // std::string
      auto desc = entry.description();  // std::string
      // strip Windows entries like "output_s!main+0x15"
      if (desc.starts_with("output_s!")) {      
        static std::regex reg{"output_s!(.*)[+]0x([[:xdigit:]]*)"};
        desc = std::regex_replace(desc, reg, "$1");
      }
      std::println("{:25}\n  line {} in {}", desc, line, file);
    }
  }
  std::println("");
}

