[ox/std] Cleanup logging output

This commit is contained in:
Gary Talent 2024-09-13 23:32:42 -05:00
parent 2a58490521
commit 5834b9c98d
4 changed files with 40 additions and 8 deletions

View File

@ -36,8 +36,8 @@ constexpr void assertFunc(CRStringView file, int line, bool pass, [[maybe_unused
if (!pass) { if (!pass) {
if (!std::is_constant_evaluated()) { if (!std::is_constant_evaluated()) {
#ifdef OX_USE_STDLIB #ifdef OX_USE_STDLIB
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg); auto output = sfmt("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg);
printStackTrace(2); output += genStackTrace(2);
oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line); oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line);
std::abort(); std::abort();
#else #else
@ -55,15 +55,16 @@ constexpr void assertFunc(CRStringView file, int line, const Error &err, CRStrin
if (err) { if (err) {
if (!std::is_constant_evaluated()) { if (!std::is_constant_evaluated()) {
#if defined(OX_USE_STDLIB) #if defined(OX_USE_STDLIB)
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, assertMsg); auto msg = sfmt("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, assertMsg);
if (err.msg) { if (err.msg) {
oxErrf("\tError Message:\t{}\n", err.msg); msg += sfmt("\tError Message:\t{}\n", err.msg);
} }
oxErrf("\tError Code:\t{}\n", static_cast<ErrorCode>(err)); msg += sfmt("\tError Code:\t{}\n", static_cast<ErrorCode>(err));
if (err.file != nullptr) { if (err.file != nullptr) {
oxErrf("\tError Location:\t{}:{}\n", err.file, err.line); msg += sfmt("\tError Location:\t{}:{}\n", err.file, err.line);
} }
printStackTrace(2); msg += genStackTrace(2);
oxErr(msg);
oxTracef("assert", "Failed assert: {} [{}:{}]", assertMsg, file, line); oxTracef("assert", "Failed assert: {} [{}:{}]", assertMsg, file, line);
std::abort(); std::abort();
#else #else

View File

@ -57,6 +57,28 @@ static auto symbolicate([[maybe_unused]]void **frames,
} }
#endif // defined(OX_USE_STDLIB) && __has_include(<unistd.h>) #endif // defined(OX_USE_STDLIB) && __has_include(<unistd.h>)
ox::String genStackTrace([[maybe_unused]]unsigned shave) noexcept {
ox::String out;
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) && __has_include(<execinfo.h>)
constexpr auto FrameCnt = 100;
Vector<void*, FrameCnt> frames(FrameCnt);
#ifdef OX_OS_FreeBSD
using FrameCnt_t = unsigned;
#else
using FrameCnt_t = signed;
#endif
frames.resize(static_cast<std::size_t>(backtrace(frames.data(), static_cast<FrameCnt_t>(frames.size()))));
if (frames.size() - shave > 2) {
const auto symbolicatedStacktrace = symbolicate(frames.data() + shave, frames.size() - shave, "\t");
out+= "Stacktrace:\n";
for (const auto &s : symbolicatedStacktrace) {
out += sfmt("\t{}\n", s);
}
}
#endif
return out;
}
void printStackTrace([[maybe_unused]]unsigned shave) noexcept { void printStackTrace([[maybe_unused]]unsigned shave) noexcept {
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) && __has_include(<execinfo.h>) #if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) && __has_include(<execinfo.h>)
constexpr auto FrameCnt = 100; constexpr auto FrameCnt = 100;

View File

@ -8,8 +8,13 @@
#pragma once #pragma once
#include "string.hpp"
namespace ox { namespace ox {
[[nodiscard]]
ox::String genStackTrace([[maybe_unused]]unsigned shave) noexcept;
/** /**
* Prints a stack trace to stderr. * Prints a stack trace to stderr.
* *

View File

@ -66,8 +66,12 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
[[maybe_unused]] const char *ch, [[maybe_unused]] const char *msg) { [[maybe_unused]] const char *ch, [[maybe_unused]] const char *msg) {
#if defined(OX_USE_STDLIB) #if defined(OX_USE_STDLIB)
if (OxPrintTrace) { if (OxPrintTrace) {
auto m = std::string_view{msg};
if (m.ends_with('\n')) {
m = std::string_view{msg, m.size() - 1};
}
std::cout << std::setw(53) << std::left << ch << "| "; std::cout << std::setw(53) << std::left << ch << "| ";
std::cout << std::setw(65) << std::left << msg << '|'; std::cout << std::setw(65) << std::left << m << '|';
std::cout << " " << file << ':' << line << "\n"; std::cout << " " << file << ':' << line << "\n";
} else if (ox::strcmp(ch, "debug") == 0 || ox::strcmp(ch, "info") == 0) { } else if (ox::strcmp(ch, "debug") == 0 || ox::strcmp(ch, "info") == 0) {
printf("%s\n", msg); printf("%s\n", msg);