forked from foonathan/debug_assert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
68 lines (58 loc) · 2 KB
/
Copy pathexample.cpp
File metadata and controls
68 lines (58 loc) · 2 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "debug_assert.hpp"
#include <iostream>
#include <csignal>
//=== module A ===//
#define MODULE_A_LEVEL 1 // macro to control assertion level
// usually set by the build system
// tag type that defines a module
struct module_a
: debug_assert::default_handler, // it uses the default handler
debug_assert::set_level<MODULE_A_LEVEL> // and this level
{};
void module_a_func(void* ptr)
{
DEBUG_ASSERT(ptr, module_a{}); // minimal assertion
DEBUG_ASSERT(2 + 2 == 4, module_a{}, debug_assert::level<2>{}); // assertion with level
DEBUG_ASSERT(1 == 0, module_a{}, "this should be true"); // assertion with additional parameters, i.e. a message
DEBUG_UNREACHABLE(module_a{}); // mark unreachable statements
}
//=== module B ===//
#define MODULE_B_LEVEL 2
struct module_b
: debug_assert::set_level<MODULE_B_LEVEL>// b uses all assertions with level <= 2
{
// module b uses a different handler
// it does not support a message
// instead you can specify a pointer value
static void handle(const debug_assert::source_location& loc, const char* expression, void* ptr = nullptr)
{
std::cerr << "Assertion failure '" << loc.file_name << ':' << loc.line_number << ": " << expression;
if (ptr)
std::cerr << " - pointer is " << ptr;
std::cerr << '\n';
}
};
void module_b_func(int &value, void* ptr)
{
DEBUG_ASSERT(ptr == &value, module_b{}, ptr); // uses the additional custom parameter
DEBUG_ASSERT(ptr == &value, module_b{}, debug_assert::level<2>{}, ptr); // also works with a custom level
}
int main()
{
auto old_handler = std::signal(SIGABRT, +[](int signal)
{
if(signal == SIGABRT)
{
std::cerr << "Please never call std::abort() in production :)";
std::exit(EXIT_SUCCESS);
}
});
if(old_handler == SIG_ERR)
{
std::cerr << "Error settings custom SIGABRT handler";
return EXIT_FAILURE;
}
module_a_func(nullptr);
int val = 5;
module_b_func(val, &val);
}