Added libfmt formatters for stacktrace.
This commit is contained in:
parent
55486b49dc
commit
a92148aac1
@ -7,6 +7,9 @@
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#if __has_include(<fmt/format.h>)
|
||||
# include <fmt/format.h>
|
||||
#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(<fmt/format.h>)
|
||||
template<>
|
||||
struct fmt::formatter<mijin::Stackframe>
|
||||
{
|
||||
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<typename TContext>
|
||||
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<mijin::Stacktrace>
|
||||
{
|
||||
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<typename TContext>
|
||||
auto format(const mijin::Stacktrace& stacktrace, TContext& ctx) const -> decltype(ctx.out())
|
||||
{
|
||||
const int numDigits = static_cast<int>(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(<fmt/format.h>)
|
||||
|
||||
#endif // !defined(MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user