#include "fud_assert.hpp" #include "fud_array.hpp" #include "fud_span.hpp" #include "fud_string_view.hpp" #include #include #include 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 } [[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation) { Array buffer{}; assertFormat(assertion, sourceLocation, Span::make(buffer)); buffer[buffer.size() - 1] = '\0'; fputs(buffer.data(), stderr); std::abort(); } } // namespace fud