#pragma once #if !defined(MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED) #define MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED 1 #include #include #include #include #if __has_include() # include #endif #include "./symbol_info.hpp" #include "../internal/common.hpp" #include "../types/result.hpp" #include "../util/iterators.hpp" namespace mijin { // // public defines // // // public constants // // // public types // struct Stackframe { void* address; std::string filename; std::string function; int lineNumber; }; class Stacktrace { private: std::vector frames_; public: Stacktrace() = default; Stacktrace(const Stacktrace&) = default; Stacktrace(Stacktrace&&) = default; explicit Stacktrace(std::vector frames) MIJIN_NOEXCEPT : frames_(std::move(frames)) {} Stacktrace& operator=(const Stacktrace&) = default; Stacktrace& operator=(Stacktrace&&) = default; [[nodiscard]] const std::vector& getFrames() const MIJIN_NOEXCEPT { return frames_; } }; // // public functions // [[nodiscard]] Result captureStacktrace(unsigned skipFrames = 0) MIJIN_NOEXCEPT; [[nodiscard]] const Optional& getExceptionStacktrace() MIJIN_NOEXCEPT; template TStream& operator<<(TStream& stream, const Stackframe& stackframe) { stream << "[" << stackframe.address << "] " << stackframe.filename << ":" << stackframe.lineNumber << " in " << demangleCPPIdentifier(stackframe.function.c_str()); return stream; } template TStream& operator<<(TStream& stream, const Stacktrace& stacktrace) { const std::streamsize oldWidth = stream.width(); const std::ios::fmtflags oldFlags = stream.flags(); const int numDigits = static_cast(std::ceil(std::log10(stacktrace.getFrames().size()))); stream << std::left; for (const auto& [idx, frame] : mijin::enumerate(stacktrace.getFrames())) { stream << " #" << std::setw(numDigits) << idx << std::setw(0) << " at " << frame << "\n"; } stream << std::setw(oldWidth); stream.flags(oldFlags); return stream; } } // namespace mijin template struct std::formatter { using char_t = TChar; template constexpr TContext::iterator parse(TContext& ctx) { auto it = ctx.begin(); auto end = ctx.end(); if (it != end && *it != MIJIN_SMART_QUOTE(char_t, '}')) { throw std::format_error("invalid format"); } return it; } template TContext::iterator format(const mijin::Stackframe& stackframe, TContext& ctx) const { auto it = ctx.out(); it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "[{}] {}:{} in {}"), stackframe.address, stackframe.filename, stackframe.lineNumber, mijin::demangleCPPIdentifier(stackframe.function.c_str())); return it; } }; template struct std::formatter { using char_t = TChar; template constexpr TContext::iterator parse(TContext& ctx) { auto it = ctx.begin(); auto end = ctx.end(); if (it != end && *it != MIJIN_SMART_QUOTE(char_t, '}')) { throw std::format_error("invalid format"); } return it; } template TContext::iterator format(const mijin::Stacktrace& stacktrace, TContext& ctx) const { const int numDigits = static_cast(std::ceil(std::log10(stacktrace.getFrames().size()))); auto it = ctx.out(); it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "[{} frames]"), stacktrace.getFrames().size()); for (const auto& [idx, frame] : mijin::enumerate(stacktrace.getFrames())) { it = std::format_to(it, MIJIN_SMART_QUOTE(char_t, "\n #{:<{}} at {}"), idx, numDigits, frame); } return it; } }; #if __has_include() template<> struct fmt::formatter { constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); if (it != end && *it != '}') FMT_THROW(format_error("invalid format")); return it; } template auto format(const mijin::Stackframe& stackframe, TContext& ctx) const -> decltype(ctx.out()) { auto it = ctx.out(); it = fmt::format_to(it, "[{}] {}:{} in {}", stackframe.address, stackframe.filename, stackframe.lineNumber, mijin::demangleCPPIdentifier(stackframe.function.c_str())); return it; } }; template<> struct fmt::formatter { constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); if (it != end && *it != '}') FMT_THROW(format_error("invalid format")); return it; } template auto format(const mijin::Stacktrace& stacktrace, TContext& ctx) const -> decltype(ctx.out()) { const int numDigits = static_cast(std::ceil(std::log10(stacktrace.getFrames().size()))); auto it = ctx.out(); it = fmt::format_to(it, "[{} frames]", stacktrace.getFrames().size()); for (const auto& [idx, frame] : mijin::enumerate(stacktrace.getFrames())) { it = fmt::format_to(it, "\n #{:<{}} at {}", idx, numDigits, frame); } return it; } }; #endif // __has_include() #endif // !defined(MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED)