summaryrefslogtreecommitdiff
path: root/source/fud_assert.cpp
blob: 3df673426ffe76260f2587eeecde445ae3c6f34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "fud_assert.hpp"

// #include "fud_array.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;

[[noreturn]] void assertFail(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);
    }

    /*
    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);

    std::terminate();
}

} // namespace fud