diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-25 17:13:49 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-25 17:13:49 -0500 |
commit | 7b30a5425eaf7aae1d72d5ba564092e342901fe8 (patch) | |
tree | 8a74d0810ea4db23d4d0c6c4ba8d91518d569db2 /source/fud_assert.cpp | |
parent | 11968f674a7de34fb7de744598a8086330cd88a3 (diff) |
A lot of work on formatting.
Diffstat (limited to 'source/fud_assert.cpp')
-rw-r--r-- | source/fud_assert.cpp | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index 8811464..0c4d7cf 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -1,39 +1,56 @@ #include "fud_assert.hpp" -// #include "fud_array.hpp" -// #include "fud_format.hpp" +#include "fud_format.hpp" +#include "fud_string.hpp" #include <climits> #include <cstdio> #include <exception> -// #include <format> namespace fud { -// constexpr std::size_t BITS_PER_OCTAL = 3; -// constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(std::source_location{}.line())) + 3; +struct BufferSink { + DrainResult drain(StringView source); +}; -[[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation) +DrainResult BufferSink::drain(StringView source) +{ + DrainResult result{0, FudStatus::Success}; + if (source.m_data == nullptr) { + result.status = FudStatus::NullPointer; + return result; + } + if (source.m_length == 0) { + result.status = FudStatus::Success; + return result; + } + result.bytesWritten = fwrite(reinterpret_cast<const char*>(source.m_data), 1, source.m_length, stderr); + if (result.bytesWritten != source.m_length) { + result.status = FudStatus::Full; + } + return result; +} + +namespace impl { +void assertFailMessage(const char* assertion, const std::source_location sourceLocation) { - const char* file_name = sourceLocation.file_name(); - if (file_name == nullptr) { - fputs("Unknown file", stderr); - } else { - fputs(file_name, stderr); + BufferSink sink; + const char* fileName = sourceLocation.file_name(); + if (fileName == nullptr) { + fileName = "Unknown file"; + } + const char* functionName = sourceLocation.function_name(); + if (functionName == nullptr) { + functionName = "Unknown Function"; } + format(sink, FormatCharMode::Unchecked, "{}:{}:{}: {}\n", fileName, functionName, sourceLocation.line(), assertion); +} - /* - constexpr std::size_t assertMsgSize = MAX_LINE_CHARS + 3; - Array<char, assertMsgSize> buffer{}; - static_cast<void>(std::format_to_n(buffer.data(), buffer.size() - 1U, ":{}:", sourceLocation.line())); - buffer[buffer.size() - 1] = '\0'; - fputs(buffer.data(), stderr); - */ - - fputs(sourceLocation.function_name(), stderr); - fputs(": ", stderr); - fputs(assertion, stderr); - fputc('\n', stderr); +} // namespace impl + +[[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation) +{ + impl::assertFailMessage(assertion, sourceLocation); std::terminate(); } |