diff --git a/source/mijin/debug/stacktrace.hpp b/source/mijin/debug/stacktrace.hpp index 3d750dd..d039337 100644 --- a/source/mijin/debug/stacktrace.hpp +++ b/source/mijin/debug/stacktrace.hpp @@ -7,6 +7,9 @@ #include #include #include +#if __has_include() +# include +#endif #include "./symbol_info.hpp" #include "../types/result.hpp" #include "../util/iterators.hpp" @@ -82,4 +85,56 @@ TStream& operator<<(TStream& stream, const Stacktrace& stacktrace) } // namespace mijin +#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 != '}') 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 != '}') 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)