aboutsummaryrefslogtreecommitdiff
path: root/cassert/include/cassert.h
blob: bb98f502c23d4b83b19ef97a83ee70584fb1317f (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
#pragma once

#include <assert.h> // For convenience, bring in soft assertions with assert().
#include <signal.h>

// Allow the client to define their own LOGE() macro.
#ifndef LOGE
#include <stdio.h>
// __VA_OPT__ is not available until C2X.
/*#define LOGE(format, ...)                                \
  {                                                      \
    fprintf(                                             \
        stderr, "[ASSERT] %s:%d " format "\n", __FILE__, \
        __LINE__ __VA_OPT__(, ) __VA_ARGS__);            \
  }*/
#define LOGE(...)                                           \
  {                                                         \
    fprintf(stderr, "[ASSERT] %s:%d ", __FILE__, __LINE__); \
    fprintf(stderr, __VA_ARGS__);                           \
    fprintf(stderr, "\n");                                  \
  }
#endif // LOGE

#define TRAP() raise(SIGTRAP)

/// Unconditional hard assert.
#define FAIL(...)    \
  LOGE(__VA_ARGS__); \
  TRAP();

//// Conditional hard assert.
#define ASSERT(condition)                 \
  if (!(condition)) {                     \
    LOGE("Assertion failed: " #condition) \
    TRAP();                               \
  }