Add util::exception class that holds stacktrace information

This commit is contained in:
Nikita Lisitsa 2023-08-06 12:52:17 +03:00
parent 56c14e6111
commit 1c22892eec
2 changed files with 49 additions and 1 deletions

View file

@ -5,6 +5,8 @@ file(GLOB_RECURSE PSEMEK_UTIL_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "so
psemek_add_library(psemek-util ${PSEMEK_UTIL_HEADERS} ${PSEMEK_UTIL_SOURCES})
target_include_directories(psemek-util PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(psemek-util PUBLIC ${CMAKE_THREAD_LIBS_INIT} Boost::boost)
target_link_libraries(psemek-util PUBLIC ${CMAKE_THREAD_LIBS_INIT} Boost::boost libbacktrace)
target_compile_definitions(psemek-util PUBLIC "-DBOOST_STACKTRACE_USE_BACKTRACE")
psemek_glob_tests(psemek-util tests)

View file

@ -0,0 +1,46 @@
#pragma once
#include <boost/stacktrace/stacktrace.hpp>
#include <exception>
#include <string>
#include <iostream>
namespace psemek::util
{
struct exception
: std::exception
{
exception(std::string message, boost::stacktrace::stacktrace stacktrace = {})
: message_(std::move(message))
, stacktrace_(std::move(stacktrace))
{}
char const * what() const noexcept override
{
return message_.data();
}
std::string const & message() const noexcept
{
return message_;
}
boost::stacktrace::stacktrace const & stacktrace() const noexcept
{
return stacktrace_;
}
private:
std::string message_;
boost::stacktrace::stacktrace stacktrace_;
};
inline std::ostream & operator << (std::ostream & os, exception const & e)
{
os << e.what() << '\n' << e.stacktrace();
return os;
}
}