ARTS 2.5.0 (git: 9ee3ac6c)
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 <tuple>
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#ifndef NDEBUG
47#include <exception>
48#include <iostream>
49
50// Use this macro around function parameter names and variable definitions
51// which are only used in assertions
52#define DEBUG_ONLY(...) __VA_ARGS__
53
54// Use this macro to output a counter value everytime a
55// certain place is reached
56#define DEBUG_COUNTER(n) \
57 { \
58 static Index n = 0; \
59 std::cerr << "DBG: " << #n << ": " << ++n << std::endl; \
60 }
61
62// Print value of expression for debugging
63#define DEBUG_PRINT(e) \
64 { std::cerr << "DBG: " << (e) << std::endl; }
65
66// Print expression and value for debugging
67#define DEBUG_VAR(e) \
68 { std::cerr << "DBG: " << #e << ": " << (e) << std::endl; }
69
70// Print expression and value with the given fp precision for debugging
71#define DEBUG_VAR_FLT(p, e) \
72 { \
73 std::streamsize old_p = std::cerr.precision(); \
74 std::cerr << "DBG: " << #e << ": " << std::setprecision(p) << (e) \
75 << std::endl \
76 << std::setprecision(old_p); \
77 }
78
80#define ARTS_NOEXCEPT noexcept(false)
81
83#define ARTS_ASSERT(condition, ...) { \
84 if (not (condition)) { \
85 throw std::runtime_error( \
86 var_string("Failed Internal Assert: " \
87 #condition "\n" "This is a bug! " \
88 "Bug is found at:\n\t" __FILE__ \
89 ":", __LINE__, "\nPlease contact" \
90 " ARTS developers so we can fix " \
91 "our error(s) via:\n\t" \
92 "github.com/atmtools/arts\n") + \
93 var_string(__VA_ARGS__) \
94 ); \
95 } }
96
97#else
98
99#define DEBUG_ONLY(...)
100
101#define DEBUG_COUNTER(n)
102
103#define DEBUG_PRINT(e)
104
105#define DEBUG_VAR(e)
106
107#define DEBUG_VAR_FLT(p, e)
108
110#define ARTS_NOEXCEPT noexcept(true)
111
113#define ARTS_ASSERT(condition, ...) {}
114
115#endif /* NDEBUG */
116
117#ifdef NO_ARTS_USER_ERRORS
118
120#define ARTS_USER_NOEXCEPT noexcept(true)
121
123#define ARTS_USER_ERROR_IF(condition, ...) {}
124
126#define ARTS_USER_ERROR(...) {}
127
128#else // NO_ARTS_USER_ERRORS == 0
129
131#define ARTS_USER_NOEXCEPT noexcept(false)
132
134#define ARTS_USER_ERROR_IF(condition, ...) { \
135 static_assert(std::tuple_size<decltype( \
136 std::make_tuple(__VA_ARGS__))>::value, \
137 "Must have an error message in user-" \
138 "facing code in " __FILE__); \
139 if (condition) { \
140 throw std::runtime_error( \
141 var_string("User Error: " #condition \
142 "\nError is found at:\n\t" __FILE__ \
143 ":", __LINE__, "\nPlease follow " \
144 "these instructions to correct your " \
145 "error:\n") + var_string(__VA_ARGS__) \
146 ); \
147 } }
148
150#define ARTS_USER_ERROR(...) { \
151 static_assert(std::tuple_size<decltype( \
152 std::make_tuple(__VA_ARGS__))>::value, \
153 "Must have an error message in user-" \
154 "facing code in " __FILE__); \
155 throw std::runtime_error( \
156 var_string("User Error:\n" \
157 "Error is found at:\n\t" __FILE__ \
158 ":", __LINE__, "\nPlease follow " \
159 "these instructions to correct your " \
160 "error:\n") + var_string(__VA_ARGS__) \
161 ); \
162 }
163
164#endif // NO_ARTS_USER_ERRORS
165
166#endif /* debug_h */
std::string var_string(Args &&... args)
Definition: debug.h:36