ARTS 2.5.11 (git: 6827797f)
debug.h
Go to the documentation of this file.
1
9#ifndef debug_h
10#define debug_h
11
12#include <sstream>
13#include <string>
14#include <version>
15
17template <typename ... Args>
18std::string var_string(Args&& ... args) {
19 if constexpr (sizeof...(Args) not_eq 0) {
20 std::ostringstream os;
21 ((os << std::forward<Args>(args)), ...);
22 return os.str();
23 } else {
24 return "";
25 }
26}
27
28#if __cpp_lib_source_location >= 201907L
29#include <iomanip>
30#include <source_location>
31
32#define CURRENT_SOURCE_LOCATION \
33 var_string("Filename: ", \
34 std::quoted(std::source_location::current().file_name()), '\n', \
35 "Function Name: ", \
36 std::quoted(std::source_location::current().function_name()), \
37 '\n', "Line Number: ", std::source_location::current().line(), \
38 '\n', \
39 "Column Number: ", std::source_location::current().column())
40
41#else
42
43#define CURRENT_SOURCE_LOCATION var_string("\t", __FILE__, ":", __LINE__)
44
45#endif
46
47#ifndef NDEBUG
48#include <exception>
49#include <iostream>
50
51// Use this macro around function parameter names and variable definitions
52// which are only used in assertions
53#define DEBUG_ONLY(...) __VA_ARGS__
54
55// Use this macro to output a counter value everytime a
56// certain place is reached
57#define DEBUG_COUNTER(n) \
58 { \
59 static Index n = 0; \
60 std::cerr << "DBG: " << #n << ": " << ++n << std::endl; \
61 }
62
63// Print value of expression for debugging
64#define DEBUG_PRINT(e) \
65 { std::cerr << "DBG: " << (e) << std::endl; }
66
67// Print expression and value for debugging
68#define DEBUG_VAR(e) \
69 { std::cerr << "DBG: " << #e << ": " << (e) << std::endl; }
70
71// Print expression and value with the given fp precision for debugging
72#define DEBUG_VAR_FLT(p, e) \
73 { \
74 std::streamsize old_p = std::cerr.precision(); \
75 std::cerr << "DBG: " << #e << ": " << std::setprecision(p) << (e) \
76 << std::endl \
77 << std::setprecision(old_p); \
78 }
79
81#define ARTS_NOEXCEPT noexcept(false)
82
84#define ARTS_ASSERT(condition, ...) \
85 { \
86 if (not(condition)) { \
87 throw std::runtime_error( \
88 var_string("Failed Internal Assert: " #condition "\n" \
89 "This is a bug! Bug is found at:\n", \
90 CURRENT_SOURCE_LOCATION, \
91 "\nPlease contact" \
92 " ARTS developers so we can fix " \
93 "our error(s) via:\n\t" \
94 "github.com/atmtools/arts\n" __VA_OPT__(, __VA_ARGS__))); \
95 } \
96 }
97
98#else
99
100#define DEBUG_ONLY(...)
101
102#define DEBUG_COUNTER(n)
103
104#define DEBUG_PRINT(e)
105
106#define DEBUG_VAR(e)
107
108#define DEBUG_VAR_FLT(p, e)
109
111#define ARTS_NOEXCEPT noexcept(true)
112
114#define ARTS_ASSERT(condition, ...) {}
115
116#endif /* NDEBUG */
117
118#ifdef NO_ARTS_USER_ERRORS
119
121#define ARTS_USER_NOEXCEPT noexcept(true)
122
124#define ARTS_USER_ERROR_IF(condition, ...) {}
125
127#define ARTS_USER_ERROR(...) {}
128
129#else // NO_ARTS_USER_ERRORS == 0
130
132#define ARTS_USER_NOEXCEPT noexcept(false)
133
135#define ARTS_USER_ERROR_IF(condition, ...) \
136 { \
137 static_assert(false __VA_OPT__(or true), \
138 "Must have an error message in user-" \
139 "facing code in " __FILE__); \
140 if (condition) { \
141 throw std::runtime_error( \
142 var_string("User Error: " #condition "\nError is found at:\n", \
143 CURRENT_SOURCE_LOCATION, \
144 "\nPlease follow " \
145 "these instructions to correct your " \
146 "error:\n" __VA_OPT__(, __VA_ARGS__))); \
147 } \
148 }
149
151#define ARTS_USER_ERROR(...) \
152 { \
153 static_assert(false __VA_OPT__(or true), \
154 "Must have an error message in user-" \
155 "facing code in " __FILE__); \
156 throw std::runtime_error( \
157 var_string("User Error:\n" \
158 "Error is found at:\n", \
159 CURRENT_SOURCE_LOCATION, \
160 "\nPlease follow " \
161 "these instructions to correct your " \
162 "error:\n" __VA_OPT__(, __VA_ARGS__))); \
163 }
164
165#endif // NO_ARTS_USER_ERRORS
166
167#endif /* debug_h */
std::string var_string(Args &&... args)
Definition: debug.h:18