summaryrefslogtreecommitdiffhomepage
path: root/compat/assert.hpp
blob: e4dd827a5c22acf0c0f58bbd0ff14cd6a5da8709 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include "defs.hpp"
#include <cstdlib>
#include <cstdio>
#include <type_traits>

namespace Magnum::Examples::detail {

template<std::size_t N, typename...Xs>
constexpr void abort(const char (&fmt)[N], Xs... xs)
{
    if (std::is_constant_evaluated())
        throw "aborting";
    else {
        std::fprintf(stderr, fmt, xs...);
        std::putc('\n', stderr);
        std::fflush(stderr);
        std::abort();
    }
}

} // namespace Magnum::Examples::detail

namespace Magnum::Examples {

#define ABORT(...)                                                   \
    do {                                                            \
        if (std::is_constant_evaluated())                           \
            throw "aborting";                                       \
        else                                                        \
            ::Magnum::Examples::detail:: abort(__VA_ARGS__);        \
    } while (false)

#define ASSERT(expr)                                                \
    do {                                                            \
        if (!(expr)) {                                              \
            ::Magnum::Examples::detail::                            \
                abort("assertion failed: '%s' in %s:%d",            \
                      #expr, __FILE__, __LINE__);                   \
        }                                                           \
    } while(false)

#define GAME_DEBUG_OUT(pfx, ...) ([&]() {                           \
    if constexpr (sizeof((pfx)) > 1)                                \
        std::fputs((pfx), stderr);                                  \
    std::fprintf(stderr, __VA_ARGS__);                              \
    std::fputs("\n", stderr);                                       \
    std::fflush(stderr);                                            \
}())

#define WARN(...)   GAME_DEBUG_OUT("warning: ", __VA_ARGS__)
#define ERR(...)    GAME_DEBUG_OUT("error: ", __VA_ARGS__)
#define DEBUG(...)  GAME_DEBUG_OUT("", __VA_ARGS__)

} // namespace Magnum::Examples