ARTS 2.5.10 (git: 2f1c442c)
debug.h
Go to the documentation of this file.
1/* Copyright (C) 2013
2 Oliver Lemke <olemke@core-dump.info>
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA. */
18
27#ifndef debug_h
28#define debug_h
29
30#include <sstream>
31#include <string>
32#include <version>
33
35template <typename ... Args>
36std::string var_string(Args&& ... args) {
37 if constexpr (sizeof...(Args) not_eq 0) {
38 std::ostringstream os;
39 ((os << std::forward<Args>(args)), ...);
40 return os.str();
41 } else {
42 return "";
43 }
44}
45
46#if __cpp_lib_source_location >= 201907L
47#include <iomanip>
48#include <source_location>
49
50#define CURRENT_SOURCE_LOCATION \
51 var_string("Filename: ", \
52 std::quoted(std::source_location::current().file_name()), '\n', \
53 "Function Name: ", \
54 std::quoted(std::source_location::current().function_name()), \
55 '\n', "Line Number: ", std::source_location::current().line(), \
56 '\n', \
57 "Column Number: ", std::source_location::current().column())
58
59#else
60
61#define CURRENT_SOURCE_LOCATION var_string("\t", __FILE__, ":", __LINE__)
62
63#endif
64
65#ifndef NDEBUG
66#include <exception>
67#include <iostream>
68
69// Use this macro around function parameter names and variable definitions
70// which are only used in assertions
71#define DEBUG_ONLY(...) __VA_ARGS__
72
73// Use this macro to output a counter value everytime a
74// certain place is reached
75#define DEBUG_COUNTER(n) \
76 { \
77 static Index n = 0; \
78 std::cerr << "DBG: " << #n << ": " << ++n << std::endl; \
79 }
80
81// Print value of expression for debugging
82#define DEBUG_PRINT(e) \
83 { std::cerr << "DBG: " << (e) << std::endl; }
84
85// Print expression and value for debugging
86#define DEBUG_VAR(e) \
87 { std::cerr << "DBG: " << #e << ": " << (e) << std::endl; }
88
89// Print expression and value with the given fp precision for debugging
90#define DEBUG_VAR_FLT(p, e) \
91 { \
92 std::streamsize old_p = std::cerr.precision(); \
93 std::cerr << "DBG: " << #e << ": " << std::setprecision(p) << (e) \
94 << std::endl \
95 << std::setprecision(old_p); \
96 }
97
99#define ARTS_NOEXCEPT noexcept(false)
100
102#define ARTS_ASSERT(condition, ...) \
103 { \
104 if (not(condition)) { \
105 throw std::runtime_error( \
106 var_string("Failed Internal Assert: " #condition "\n" \
107 "This is a bug! Bug is found at:\n", \
108 CURRENT_SOURCE_LOCATION, \
109 "\nPlease contact" \
110 " ARTS developers so we can fix " \
111 "our error(s) via:\n\t" \
112 "github.com/atmtools/arts\n" __VA_OPT__(, __VA_ARGS__))); \
113 } \
114 }
115
116#else
117
118#define DEBUG_ONLY(...)
119
120#define DEBUG_COUNTER(n)
121
122#define DEBUG_PRINT(e)
123
124#define DEBUG_VAR(e)
125
126#define DEBUG_VAR_FLT(p, e)
127
129#define ARTS_NOEXCEPT noexcept(true)
130
132#define ARTS_ASSERT(condition, ...) {}
133
134#endif /* NDEBUG */
135
136#ifdef NO_ARTS_USER_ERRORS
137
139#define ARTS_USER_NOEXCEPT noexcept(true)
140
142#define ARTS_USER_ERROR_IF(condition, ...) {}
143
145#define ARTS_USER_ERROR(...) {}
146
147#else // NO_ARTS_USER_ERRORS == 0
148
150#define ARTS_USER_NOEXCEPT noexcept(false)
151
153#define ARTS_USER_ERROR_IF(condition, ...) \
154 { \
155 static_assert(false __VA_OPT__(or true), \
156 "Must have an error message in user-" \
157 "facing code in " __FILE__); \
158 if (condition) { \
159 throw std::runtime_error( \
160 var_string("User Error: " #condition "\nError is found at:\n", \
161 CURRENT_SOURCE_LOCATION, \
162 "\nPlease follow " \
163 "these instructions to correct your " \
164 "error:\n" __VA_OPT__(, __VA_ARGS__))); \
165 } \
166 }
167
169#define ARTS_USER_ERROR(...) \
170 { \
171 static_assert(false __VA_OPT__(or true), \
172 "Must have an error message in user-" \
173 "facing code in " __FILE__); \
174 throw std::runtime_error( \
175 var_string("User Error:\n" \
176 "Error is found at:\n", \
177 CURRENT_SOURCE_LOCATION, \
178 "\nPlease follow " \
179 "these instructions to correct your " \
180 "error:\n" __VA_OPT__(, __VA_ARGS__))); \
181 }
182
183#endif // NO_ARTS_USER_ERRORS
184
185#endif /* debug_h */
std::string var_string(Args &&... args)
Definition: debug.h:36