summaryrefslogtreecommitdiff
path: root/source/fud_assert.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-05 08:33:39 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-05 08:33:39 -0500
commit79620980ea3880f6512a35b9d688a60a02ff8b98 (patch)
treeb4ab3dfc656b88989f3e4234e9c476c295c176ed /source/fud_assert.cpp
parentb50980ad70684530d55b7adf20de6047ebf53ba2 (diff)
Formatting changes. Refactoring out detail::CopyMove from Result.
Diffstat (limited to 'source/fud_assert.cpp')
-rw-r--r--source/fud_assert.cpp45
1 files changed, 36 insertions, 9 deletions
diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp
index f3358df..9c2ec77 100644
--- a/source/fud_assert.cpp
+++ b/source/fud_assert.cpp
@@ -1,26 +1,53 @@
#include "fud_assert.hpp"
#include "fud_array.hpp"
+#include "fud_string_view.hpp"
+#include <climits>
#include <cstdio>
-#include <stdexcept>
#include <format>
+#include <stdexcept>
namespace fud {
void assertFail(const char* assertion, const char* file, unsigned int line, const char* function) noexcept(false)
{
- constexpr size_t ASSERT_MSG_SIZE = 1024;
+ constexpr size_t MAX_FILE_CHARS = 256;
+ constexpr size_t MAX_FUNCTION_CHARS = 256;
+ constexpr size_t BITS_PER_OCTAL = 3;
+ constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(line)) + 3;
+ constexpr size_t MAX_ASSERT_CHARS = 512 - MAX_LINE_CHARS;
+ constexpr size_t ASSERT_MSG_SIZE = MAX_FILE_CHARS + MAX_LINE_CHARS + MAX_FUNCTION_CHARS + MAX_ASSERT_CHARS;
+
+ auto lengthResult = cStringLength(file);
+ size_t filenameLength = 0;
+ auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX;
+ if (!badLength) {
+ filenameLength = static_cast<size_t>(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 = file + filenameLength - MAX_LINE_CHARS;
+ filenameLength = MAX_FILE_CHARS;
+ }
+
Array<char, ASSERT_MSG_SIZE> buffer{};
+ // clang-format off
static_cast<void>(std::format_to_n(
- buffer.data(),
- buffer.size() - 1U,
- "{}:{}: {}:Assertion `{}` failed",
- file,
+ buffer.data(), buffer.size() - 1U,
+ "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed",
+ filename, filenameLength,
line,
- function,
- assertion));
+ function, MAX_FUNCTION_CHARS,
+ assertion, MAX_ASSERT_CHARS));
+ // clang-format on
+
throw std::runtime_error(buffer.data());
}
-}
+} // namespace fud