From 5834b9c98d77afea054af3912d7c8d2ed744bbde Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 13 Sep 2024 23:32:42 -0500 Subject: [PATCH] [ox/std] Cleanup logging output --- deps/ox/src/ox/std/assert.hpp | 15 ++++++++------- deps/ox/src/ox/std/stacktrace.cpp | 22 ++++++++++++++++++++++ deps/ox/src/ox/std/stacktrace.hpp | 5 +++++ deps/ox/src/ox/std/tracehook.cpp | 6 +++++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/deps/ox/src/ox/std/assert.hpp b/deps/ox/src/ox/std/assert.hpp index 879f3b20..b8d12a14 100644 --- a/deps/ox/src/ox/std/assert.hpp +++ b/deps/ox/src/ox/std/assert.hpp @@ -36,8 +36,8 @@ constexpr void assertFunc(CRStringView file, int line, bool pass, [[maybe_unused if (!pass) { if (!std::is_constant_evaluated()) { #ifdef OX_USE_STDLIB - oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg); - printStackTrace(2); + auto output = sfmt("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, msg); + output += genStackTrace(2); oxTracef("assert", "Failed assert: {} ({}) [{}:{}]", msg, assertTxt, file, line); std::abort(); #else @@ -55,15 +55,16 @@ constexpr void assertFunc(CRStringView file, int line, const Error &err, CRStrin if (err) { if (!std::is_constant_evaluated()) { #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) { - oxErrf("\tError Message:\t{}\n", err.msg); + msg += sfmt("\tError Message:\t{}\n", err.msg); } - oxErrf("\tError Code:\t{}\n", static_cast(err)); + msg += sfmt("\tError Code:\t{}\n", static_cast(err)); 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); std::abort(); #else diff --git a/deps/ox/src/ox/std/stacktrace.cpp b/deps/ox/src/ox/std/stacktrace.cpp index acbfdb46..b4da2712 100644 --- a/deps/ox/src/ox/std/stacktrace.cpp +++ b/deps/ox/src/ox/std/stacktrace.cpp @@ -57,6 +57,28 @@ static auto symbolicate([[maybe_unused]]void **frames, } #endif // defined(OX_USE_STDLIB) && __has_include() +ox::String genStackTrace([[maybe_unused]]unsigned shave) noexcept { + ox::String out; +#if defined(OX_USE_STDLIB) && __has_include() && __has_include() + constexpr auto FrameCnt = 100; + Vector frames(FrameCnt); +#ifdef OX_OS_FreeBSD + using FrameCnt_t = unsigned; +#else + using FrameCnt_t = signed; +#endif + frames.resize(static_cast(backtrace(frames.data(), static_cast(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 { #if defined(OX_USE_STDLIB) && __has_include() && __has_include() constexpr auto FrameCnt = 100; diff --git a/deps/ox/src/ox/std/stacktrace.hpp b/deps/ox/src/ox/std/stacktrace.hpp index 26555e52..0c462d06 100644 --- a/deps/ox/src/ox/std/stacktrace.hpp +++ b/deps/ox/src/ox/std/stacktrace.hpp @@ -8,8 +8,13 @@ #pragma once +#include "string.hpp" + namespace ox { +[[nodiscard]] +ox::String genStackTrace([[maybe_unused]]unsigned shave) noexcept; + /** * Prints a stack trace to stderr. * diff --git a/deps/ox/src/ox/std/tracehook.cpp b/deps/ox/src/ox/std/tracehook.cpp index 90743ec0..8ff24d34 100644 --- a/deps/ox/src/ox/std/tracehook.cpp +++ b/deps/ox/src/ox/std/tracehook.cpp @@ -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) { #if defined(OX_USE_STDLIB) 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(65) << std::left << msg << '|'; + std::cout << std::setw(65) << std::left << m << '|'; std::cout << " " << file << ':' << line << "\n"; } else if (ox::strcmp(ch, "debug") == 0 || ox::strcmp(ch, "info") == 0) { printf("%s\n", msg);