From e94db4695e236b42ae1be44b2605075161d5144f Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Fri, 18 Oct 2024 21:43:44 -0500 Subject: Add temporary work for formatting. --- source/fud_assert.cpp | 55 +++++++++++++++++---------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) (limited to 'source') diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index 14bf5eb..98f17d0 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -1,8 +1,6 @@ #include "fud_assert.hpp" #include "fud_array.hpp" -#include "fud_span.hpp" -#include "fud_string_view.hpp" #include #include @@ -10,46 +8,29 @@ namespace fud { -void assertFormat( - const char* assertion, - const std::source_location sourceLocation, - Span buffer) -{ - auto lengthResult = cStringLength(sourceLocation.file_name()); - size_t filenameLength = 0; - auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX; - if (!badLength) { - filenameLength = static_cast(lengthResult); - } - const char* filename = nullptr; - if (badLength) { - constexpr const char invalidFile[] = "INVALID FILE"; - static_assert(sizeof(invalidFile) < MAX_FILE_CHARS); - filenameLength = sizeof(invalidFile); - filename = invalidFile; - } else if (filenameLength > MAX_LINE_CHARS) { - filename = sourceLocation.file_name() + filenameLength - MAX_LINE_CHARS; - filenameLength = MAX_FILE_CHARS; - } - - // clang-format off - static_cast(std::format_to_n( - buffer.data(), buffer.size() - 1U, - "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed\n", - filename, filenameLength, - sourceLocation.line(), - sourceLocation.function_name(), MAX_FUNCTION_CHARS, - assertion, MAX_ASSERT_CHARS)); - // clang-format on -} +constexpr std::size_t BITS_PER_OCTAL = 3; +constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(std::source_location{}.line())) + 3; [[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation) { - Array buffer{}; - assertFormat(assertion, sourceLocation, Span::make(buffer)); + const char* file_name = sourceLocation.file_name(); + if (file_name == nullptr) { + fputs("Unknown file", stderr); + } else { + fputs(file_name, stderr); + } + + constexpr std::size_t assertMsgSize = MAX_LINE_CHARS + 3; + Array buffer{}; + static_cast(std::format_to_n(buffer.data(), buffer.size() - 1U, ":{}:", sourceLocation.line())); buffer[buffer.size() - 1] = '\0'; fputs(buffer.data(), stderr); - std::abort(); + + fputs(sourceLocation.function_name(), stderr); + fputs(": ", stderr); + fputs(assertion, stderr); + + std::terminate(); } } // namespace fud -- cgit v1.2.3