packages.biu: fix stacktrace on exception throw

This commit is contained in:
2025-07-29 20:12:09 +08:00
parent b4df678546
commit 9858c48d90
5 changed files with 21 additions and 12 deletions

View File

@@ -13,7 +13,7 @@ endif()
find_package(magic_enum REQUIRED)
find_package(fmt REQUIRED)
find_package(Boost REQUIRED COMPONENTS headers iostreams filesystem system process)
find_package(Boost REQUIRED COMPONENTS headers iostreams filesystem system process stacktrace_from_exception)
# stacktrace_backtrace
find_package(range-v3 REQUIRED)
find_path(NAMEOF_INCLUDE_DIR nameof.hpp REQUIRED)
@@ -36,7 +36,7 @@ target_include_directories(biu PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_D
${LIBBACKTRACE_INCLUDE_DIR} ${POCKETFFT_INCLUDE_DIR})
target_link_libraries(biu PUBLIC magic_enum::magic_enum fmt::fmt Boost::headers Boost::iostreams Boost::filesystem
range-v3::range-v3 Eigen3::Eigen HighFive TgBot::TgBot ${LIBBACKTRACE_LIBRARY} hdf5::hdf5 concurrencpp::concurrencpp
yaml-cpp::yaml-cpp glaze::glaze Boost::process)
yaml-cpp::yaml-cpp glaze::glaze Boost::process Boost::stacktrace_from_exception)
target_compile_features(biu PUBLIC cxx_std_23)
target_compile_options(biu PUBLIC -Wno-gnu-string-literal-operator-template)
install(TARGETS biu EXPORT biuTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

View File

@@ -1,7 +1,7 @@
include("${CMAKE_CURRENT_LIST_DIR}/biuTargets.cmake")
find_package(magic_enum REQUIRED)
find_package(fmt REQUIRED)
find_package(Boost REQUIRED COMPONENTS headers iostreams filesystem system process)
find_package(Boost REQUIRED COMPONENTS headers iostreams filesystem system process stacktrace_from_exception)
find_package(range-v3 REQUIRED)
find_path(NAMEOF_INCLUDE_DIR nameof.hpp REQUIRED)
find_package(Eigen3 REQUIRED)

View File

@@ -7,6 +7,7 @@
# include <fmt/format.h>
# include <fmt/ostream.h>
# include <yaml-cpp/yaml.h>
# include <boost/stacktrace.hpp>
namespace biu
{
@@ -77,4 +78,6 @@ namespace fmt
: basic_ostream_formatter<Char> {};
template <typename Char> struct formatter<YAML::Node, Char> : basic_ostream_formatter<Char> {};
template <typename Char> struct formatter<boost::stacktrace::basic_stacktrace<>, Char>
: basic_ostream_formatter<Char> {};
}

View File

@@ -95,10 +95,10 @@ namespace biu
public: [[gnu::always_inline]] inline void info(const std::string& message) const;
public: [[gnu::always_inline]] inline void debug(const std::string& message) const;
public: template <typename FinalException> [[gnu::always_inline]] void print_exception
public: [[gnu::always_inline]] inline void print_exception
(
const std::string& type, const std::string& message, const boost::stacktrace::stacktrace& stacktrace,
CalledBy<Exception<FinalException>>
std::optional<std::pair<std::string, std::string>> type_and_message,
const boost::stacktrace::stacktrace& stacktrace
) const;
};
friend class Guard;

View File

@@ -66,14 +66,20 @@ namespace biu
template <typename FinalException> Logger::Exception<FinalException>::Exception(const std::string& message)
{
Logger::Guard log(message);
log.print_exception<FinalException>(nameof::nameof_full_type<FinalException>(), message, Stacktrace_, {});
log.print_exception
(std::pair<std::string, std::string>(nameof::nameof_full_type<FinalException>(), message), Stacktrace_);
}
template <typename Function> inline void Logger::try_exec(Function&& function)
{
Logger::Guard log;
try { function(); }
catch (...) { log.error(boost::current_exception_diagnostic_information()); }
catch (...)
{
log.error(boost::current_exception_diagnostic_information());
log.print_exception
(std::nullopt, boost::stacktrace::stacktrace::from_current_exception());
}
}
inline thread_local unsigned Logger::Guard::Indent_ = 0;
@@ -148,13 +154,13 @@ namespace biu
return std::forward<T>(value);
}
template <typename FinalException> inline void Logger::Guard::print_exception
inline void Logger::Guard::print_exception
(
const std::string& type, const std::string& message, const boost::stacktrace::stacktrace& stacktrace,
CalledBy<Exception<FinalException>>
std::optional<std::pair<std::string, std::string>> type_and_message,
const boost::stacktrace::stacktrace& stacktrace
) const
{
log<Level::Error>("{}: {}"_f(type, message));
if (type_and_message) log<Level::Error>("{}: {}"_f(type_and_message->first, type_and_message->second));
if (auto&& lock = LoggerConfig_.lock(); lock->Level >= Logger::Level::Error)
{
static_assert(std::same_as<std::size_t, std::uint64_t>);