summaryrefslogtreecommitdiff
path: root/source/fud_assert.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/fud_assert.cpp')
-rw-r--r--source/fud_assert.cpp63
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();
}