Files

57 lines
1.0 KiB
C++
Raw Permalink Normal View History

2022-08-14 03:36:53 +03:00
#pragma once
#include <util/types.hpp>
#include <util/logs.hpp>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
2022-08-14 03:36:53 +03:00
namespace utils
{
2022-12-17 14:19:19 +03:00
namespace stack_trace
{
// Printing utilities
template <typename T>
2026-06-14 06:28:05 +02:00
concept Logger = requires (T& t, std::string_view msg)
2022-12-17 14:19:19 +03:00
{
{ t.print(msg) };
};
struct print_to_log
{
logs::channel& log;
public:
print_to_log(logs::channel& chan)
: log(chan)
{}
2026-06-14 06:28:05 +02:00
void print(std::string_view s)
2022-12-17 14:19:19 +03:00
{
log.error("%s", s);
}
};
}
#ifdef _WIN32
std::vector<void*> get_backtrace(int max_depth = 255, PCONTEXT ctx = nullptr);
#else
2022-08-14 20:25:52 +03:00
std::vector<void*> get_backtrace(int max_depth = 255);
#endif
2022-08-14 20:25:52 +03:00
std::vector<std::string> get_backtrace_symbols(const std::vector<void*>& stack);
2022-08-14 03:36:53 +03:00
2022-12-17 14:19:19 +03:00
FORCE_INLINE void print_trace(stack_trace::Logger auto& logger, int max_depth = 255)
2022-08-14 03:36:53 +03:00
{
2022-08-14 20:25:52 +03:00
const auto trace = get_backtrace(max_depth);
const auto lines = get_backtrace_symbols(trace);
2022-08-14 03:36:53 +03:00
for (const auto& line : lines)
{
2022-12-17 14:19:19 +03:00
logger.print(line);
2022-08-14 03:36:53 +03:00
}
}
}