Introduce panic_trace and small accuracy fix

This commit is contained in:
SimoneN64
2024-05-15 22:55:32 +02:00
parent cf8b812389
commit 249bb97ca9
5 changed files with 57 additions and 18 deletions

View File

@@ -2,6 +2,10 @@
#include <common.hpp>
#include <fmt/format.h>
#include <fmt/color.h>
#include <string>
#ifndef NDEBUG
#include <dlfcn.h>
#endif
namespace Util {
enum LogLevel : u8 {
@@ -22,7 +26,7 @@ constexpr void print(const std::string& fmt, Args... args) {
fmt::print(fmt::emphasis::bold | fg(fmt::color::red), fmt, args...);
} else if constexpr(messageType == Warn) {
fmt::print(fg(fmt::color::yellow), fmt, args...);
} else if constexpr(messageType == Info) {
} else if constexpr(messageType == Info || messageType == Trace) {
fmt::print(fmt, args...);
} else if constexpr(messageType == Debug) {
#ifndef NDEBUG
@@ -43,32 +47,60 @@ constexpr void print(const std::string& fmt, Args... args) {
template <typename ...Args>
constexpr void panic(const std::string& fmt, Args... args) {
print<Error>(fmt + "\n", args...);
print<Error>("[FATAL] " + fmt + "\n", args...);
exit(-1);
}
template <typename ...Args>
constexpr void error(const std::string& fmt, Args... args) {
print<Error>(fmt + "\n", args...);
print<Error>("[ERROR] " + fmt + "\n", args...);
}
template <typename ...Args>
constexpr void warn(const std::string& fmt, Args... args) {
print<Warn>(fmt + "\n", args...);
print<Warn>("[WARN] " + fmt + "\n", args...);
}
template <typename ...Args>
constexpr void info(const std::string& fmt, Args... args) {
print(fmt + "\n", args...);
print("[INFO] " + fmt + "\n", args...);
}
template <typename ...Args>
constexpr void debug(const std::string& fmt, Args... args) {
print<Debug>(fmt + "\n", args...);
print<Debug>("[DEBUG] " + fmt + "\n", args...);
}
template <typename ...Args>
constexpr void trace(const std::string& fmt, Args... args) {
print<Trace>(fmt + "\n", args...);
print<Trace>("[TRACE] " + fmt + "\n", args...);
}
template <typename ...Args>
constexpr void panic_trace(const std::string& fmt, Args... args) {
#ifndef NDEBUG
Dl_info info;
auto tmp = fmt::format(fmt + '\n', args...);
tmp += "Called by:\n";
if(dladdr(__builtin_return_address(0), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
if(dladdr(__builtin_return_address(1), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
if(dladdr(__builtin_return_address(2), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
if(dladdr(__builtin_return_address(3), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
if(dladdr(__builtin_return_address(4), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
if(dladdr(__builtin_return_address(5), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
if(dladdr(__builtin_return_address(6), &info))
tmp += fmt::format("\t-> {}\n", info.dli_sname);
print<Error>("[FATAL TRACE] " + tmp);
exit(-1);
#else
panic(fmt, args...);
#endif
}
}