mijin2/source/mijin/debug/stacktrace.hpp

206 lines
5.6 KiB
C++

#pragma once
#if !defined(MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED)
#define MIJIN_DEBUG_STACKTRACE_HPP_INCLUDED 1
#include <cmath>
#include <format>
#include <iomanip>
#include <vector>
#if __has_include(<fmt/format.h>)
# include <fmt/format.h>
#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<Stackframe> frames_;
public:
Stacktrace() = default;
Stacktrace(const Stacktrace&) = default;
Stacktrace(Stacktrace&&) = default;
explicit Stacktrace(std::vector<Stackframe> frames) MIJIN_NOEXCEPT : frames_(std::move(frames)) {}
Stacktrace& operator=(const Stacktrace&) = default;
Stacktrace& operator=(Stacktrace&&) = default;
[[nodiscard]] const std::vector<Stackframe>& getFrames() const MIJIN_NOEXCEPT { return frames_; }
};
//
// public functions
//
[[nodiscard]] Result<Stacktrace> captureStacktrace(unsigned skipFrames = 0) MIJIN_NOEXCEPT;
[[nodiscard]] const Optional<Stacktrace>& getExceptionStacktrace() MIJIN_NOEXCEPT;
template<typename TStream>
TStream& operator<<(TStream& stream, const Stackframe& stackframe)
{
stream << "[" << stackframe.address << "] " << stackframe.filename << ":" << stackframe.lineNumber << " in " << demangleCPPIdentifier(stackframe.function.c_str());
return stream;
}
template<typename TStream>
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<int>(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<typename TChar>
struct std::formatter<mijin::Stackframe, TChar>
{
using char_t = TChar;
template<typename TContext>
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<typename TContext>
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<typename TChar>
struct std::formatter<mijin::Stacktrace, TChar>
{
using char_t = TChar;
template<class TContext>
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<typename TContext>
TContext::iterator format(const mijin::Stacktrace& stacktrace, TContext& ctx) const
{
const int numDigits = static_cast<int>(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(<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 != '}') FMT_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 != '}') FMT_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)