STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
verbose.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/verbose.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2005-2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007-2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2013 Timo Bingmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_VERBOSE_HEADER
16 #define STXXL_VERBOSE_HEADER
17 
18 #include <cstdlib>
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 #include <stxxl/bits/unused.h>
23 
24 #define _STXXL_PRNT_COUT (1 << 0)
25 #define _STXXL_PRNT_CERR (1 << 1)
26 #define _STXXL_PRNT_LOG (1 << 2)
27 #define _STXXL_PRNT_ERRLOG (1 << 3)
28 #define _STXXL_PRNT_ADDNEWLINE (1 << 16)
29 #define _STXXL_PRNT_TIMESTAMP (1 << 17)
30 #define _STXXL_PRNT_THREAD_ID (1 << 18)
31 
32 #define _STXXL_PRINT_FLAGS_DEFAULT (_STXXL_PRNT_COUT | _STXXL_PRNT_LOG)
33 #define _STXXL_PRINT_FLAGS_ERROR (_STXXL_PRNT_CERR | _STXXL_PRNT_ERRLOG)
34 #define _STXXL_PRINT_FLAGS_VERBOSE (_STXXL_PRINT_FLAGS_DEFAULT | _STXXL_PRNT_TIMESTAMP | _STXXL_PRNT_THREAD_ID)
35 
37 
38 void print_msg(const char* label, const std::string& msg, unsigned flags);
39 
41 
42 #define _STXXL_PRINT(label, message, flags) \
43  do { \
44  std::ostringstream str_; \
45  str_ << message; \
46  stxxl::print_msg(label, str_.str(), flags | _STXXL_PRNT_ADDNEWLINE); \
47  } while (false)
48 
49 #define _STXXL_NOT_VERBOSE(message) \
50  do { \
51  if (0) { \
52  std::ostringstream str_; \
53  str_ << message; \
54  } \
55  } while (false)
56 
57 #ifdef STXXL_FORCE_VERBOSE_LEVEL
58 #undef STXXL_VERBOSE_LEVEL
59 #define STXXL_VERBOSE_LEVEL STXXL_FORCE_VERBOSE_LEVEL
60 #endif
61 
62 #ifdef STXXL_DEFAULT_VERBOSE_LEVEL
63 #ifndef STXXL_VERBOSE_LEVEL
64 #define STXXL_VERBOSE_LEVEL STXXL_DEFAULT_VERBOSE_LEVEL
65 #endif
66 #endif
67 
68 #ifndef STXXL_VERBOSE_LEVEL
69 #define STXXL_VERBOSE_LEVEL -1
70 #endif
71 
72 #if STXXL_VERBOSE_LEVEL > -10
73  #define STXXL_MSG(x) _STXXL_PRINT("STXXL-MSG", x, _STXXL_PRINT_FLAGS_DEFAULT)
74 #else
75 // Please do not report STXXL problems with STXXL_MSG disabled!
76  #define STXXL_MSG(x) _STXXL_NOT_VERBOSE(x)
77 #endif
78 
79 // STXXL_VARDUMP(x) prints the name of x together with its value.
80 #if STXXL_VERBOSE_LEVEL > -10
81  #define STXXL_VARDUMP(x) _STXXL_PRINT("STXXL-MSG", #x " = " << x, _STXXL_PRINT_FLAGS_DEFAULT)
82 #else
83  #define STXXL_VARDUMP(x) _STXXL_NOT_VERBOSE
84 #endif
85 
86 // STXXL_MEMDUMP(x) prints the name of x together with its value as an amount of memory in IEC units.
87 #if STXXL_VERBOSE_LEVEL > -10
88  #define STXXL_MEMDUMP(x) _STXXL_PRINT("STXXL-MSG", #x " = " << stxxl::format_IEC_size(x) << "B", _STXXL_PRINT_FLAGS_DEFAULT)
89 #else
90  #define STXXL_MEMDUMP(x) _STXXL_NOT_VERBOSE
91 #endif
92 
93 #if STXXL_VERBOSE_LEVEL > -100
94  #define STXXL_ERRMSG(x) _STXXL_PRINT("STXXL-ERRMSG", x, _STXXL_PRINT_FLAGS_ERROR)
95 #else
96 // Please do not report STXXL problems with STXXL_ERRMSG disabled!
97  #define STXXL_ERRMSG(x) _STXXL_NOT_VERBOSE(x)
98 #endif
99 
100 // STXXL_VERBOSE0 should be used for current debugging activity only,
101 // and afterwards be replaced by STXXL_VERBOSE1 or higher.
102 // Code that actively uses STXXL_VERBOSE0 should never get into a release.
103 
104 #if STXXL_VERBOSE_LEVEL > -1
105  #define STXXL_VERBOSE0(x) _STXXL_PRINT("STXXL-VERBOSE0", x, _STXXL_PRINT_FLAGS_VERBOSE)
106 #else
107  #define STXXL_VERBOSE0(x) _STXXL_NOT_VERBOSE(x)
108 #endif
109 
110 #if STXXL_VERBOSE_LEVEL > 0
111  #define STXXL_VERBOSE1(x) _STXXL_PRINT("STXXL-VERBOSE1", x, _STXXL_PRINT_FLAGS_VERBOSE)
112 #else
113  #define STXXL_VERBOSE1(x) _STXXL_NOT_VERBOSE(x)
114 #endif
115 
116 #define STXXL_VERBOSE(x) STXXL_VERBOSE1(x)
117 
118 #if STXXL_VERBOSE_LEVEL > 1
119  #define STXXL_VERBOSE2(x) _STXXL_PRINT("STXXL-VERBOSE2", x, _STXXL_PRINT_FLAGS_VERBOSE)
120 #else
121  #define STXXL_VERBOSE2(x) _STXXL_NOT_VERBOSE(x)
122 #endif
123 
124 #if STXXL_VERBOSE_LEVEL > 2
125  #define STXXL_VERBOSE3(x) _STXXL_PRINT("STXXL-VERBOSE3", x, _STXXL_PRINT_FLAGS_VERBOSE)
126 #else
127  #define STXXL_VERBOSE3(x) _STXXL_NOT_VERBOSE(x)
128 #endif
129 
130 // STXXL_VERBOSE[0123]_THIS prefixes "[0xaddress]" and then calls the version
131 // without _THIS.
132 
133 #define STXXL_VERBOSE0_THIS(x) \
134  STXXL_VERBOSE0("[" << static_cast<void*>(this) << "] " << x)
135 
136 #define STXXL_VERBOSE1_THIS(x) \
137  STXXL_VERBOSE1("[" << static_cast<void*>(this) << "] " << x)
138 
139 #define STXXL_VERBOSE2_THIS(x) \
140  STXXL_VERBOSE2("[" << static_cast<void*>(this) << "] " << x)
141 
142 #define STXXL_VERBOSE3_THIS(x) \
143  STXXL_VERBOSE3("[" << static_cast<void*>(this) << "] " << x)
144 
145 //! STXXL_DEBUG_COND is a macro which prints iff the passed variable "debug" is
146 //! true. Use this for conditional debug variables.
147 #define STXXL_DEBUG_COND(dbg, x) \
148  do { \
149  if (dbg) { \
150  _STXXL_PRINT("STXXL-DEBUG", x, _STXXL_PRINT_FLAGS_DEFAULT); \
151  } \
152  } while (0)
153 
154 //! STXXL_DEBUG is a macro which prints iff the locally defined variable
155 //! "debug" is true. Use this for scoped-based debug variables.
156 #define STXXL_DEBUG(x) STXXL_DEBUG_COND(debug, x)
157 
158 //! STXXL_DEBUG0 is a macro which never prints the debug message, used to
159 //! temporarily disable STXXL_DEBUG occurrences.
160 #define STXXL_DEBUG0(x) STXXL_DEBUG_COND(false, x)
161 
162 //! STXXL_DEBUG1 is a macro which always prints the debug message, used to
163 //! temporarily enable STXXL_DEBUG occurrences.
164 #define STXXL_DEBUG1(x) STXXL_DEBUG_COND(true, x)
165 
166 //! STXXL_CHECK is an assertion macro for unit tests, which contrarily to
167 //! assert() also works in release builds. These macros should ONLY be used in
168 //! UNIT TESTS, not in the library source. Use usual assert() there.
169 
170 #define STXXL_CHECK(condition) \
171  do { \
172  if (!(condition)) { \
173  _STXXL_PRINT("STXXL-CHECK", \
174  #condition " - FAILED @ " __FILE__ ":" << __LINE__, \
175  _STXXL_PRINT_FLAGS_ERROR); abort(); \
176  } \
177  } while (0)
178 
179 #define STXXL_CHECK2(condition, text) \
180  do { \
181  if (!(condition)) { \
182  _STXXL_PRINT("STXXL-CHECK", \
183  text << " - " #condition " - FAILED @ " __FILE__ ":" << __LINE__, \
184  _STXXL_PRINT_FLAGS_ERROR); abort(); \
185  } \
186  } while (0)
187 
188 //! STXXL_CHECK_EQUAL(a,b) is an assertion macro for unit tests, similar to
189 //! STXXL_CHECK(a==b). The difference is that STXXL_CHECK_EQUAL(a,b) also
190 //! prints the values of a and b. Attention: a and b must be printable with
191 //! std::cout!
192 #define STXXL_CHECK_EQUAL(a, b) \
193  do { \
194  if (!(a == b)) { \
195  _STXXL_PRINT("STXXL-CHECK", \
196  "\"" << a << "\" = " #a " == " #b " = \"" << b << "\"" \
197  " - FAILED @ " __FILE__ ":" << __LINE__, \
198  _STXXL_PRINT_FLAGS_ERROR); abort(); \
199  } \
200  } while (0)
201 
202 // STXXL_ASSERT is an assertion macro almost identical to assert(). The only
203 // difference is that with NDEBUG defined, the _code_ itself still exists. This
204 // silences warnings about unused variables and typedefs in release builds.
205 
206 #ifdef NDEBUG
207 
208 #define STXXL_ASSERT(condition) \
209  do { if (0) { \
210  if (!(condition)) { \
211  _STXXL_PRINT("STXXL-ASSERT", \
212  #condition " - FAILED @ " __FILE__ ":" << __LINE__, \
213  _STXXL_PRINT_FLAGS_ERROR); abort(); \
214  } \
215  } \
216  } while (0)
217 
218 #else
219 
220 #define STXXL_ASSERT(condition) \
221  do { if (1) { \
222  if (!(condition)) { \
223  _STXXL_PRINT("STXXL-ASSERT", \
224  #condition " - FAILED @ " __FILE__ ":" << __LINE__, \
225  _STXXL_PRINT_FLAGS_ERROR); abort(); \
226  } \
227  } \
228  } while (0)
229 
230 #endif
231 
232 // STXXL_DEBUG_ASSERT is an assertions, that is only active if the compile flag
233 // STXXL_DEBUG_ASSERTIONS=1 is true. This macro should be used for more costly
234 // assertions inside the library, that can be deactivated after development.
235 
236 #if STXXL_DEBUG_ASSERTIONS
237 
238 #define STXXL_DEBUG_ASSERT(condition) \
239  do { if (true) { \
240  if (!(condition)) { \
241  _STXXL_PRINT("STXXL-DEBUG-ASSERT", \
242  #condition " - FAILED @ " __FILE__ ":" << __LINE__, \
243  _STXXL_PRINT_FLAGS_ERROR); abort(); \
244  } \
245  } \
246  } while (0)
247 
248 #else
249 
250 #define STXXL_DEBUG_ASSERT(condition) \
251  do { if (false) { \
252  if (!(condition)) { \
253  _STXXL_PRINT("STXXL-DEBUG-ASSERT", \
254  #condition " - FAILED @ " __FILE__ ":" << __LINE__, \
255  _STXXL_PRINT_FLAGS_ERROR); abort(); \
256  } \
257  } \
258  } while (0)
259 
260 #endif
261 
262 // STXXL_CHECK_THROW is an assertion macro for unit tests, which checks that
263 // the enclosed code throws an exception.
264 
265 #define STXXL_CHECK_THROW(code, exception_type) \
266  do { \
267  bool t_ = false; try { code; } \
268  catch (const exception_type&) { t_ = true; } \
269  if (t_) break; \
270  _STXXL_PRINT("STXXL-CHECK-THROW", \
271  #code " - NO EXCEPTION " #exception_type \
272  " @ " __FILE__ ":" << __LINE__, \
273  _STXXL_PRINT_FLAGS_ERROR); \
274  abort(); \
275  } while (0)
276 
277 ////////////////////////////////////////////////////////////////////////////
278 
279 #endif // !STXXL_VERBOSE_HEADER
280 // vim: et:ts=4:sw=4
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void print_msg(const char *label, const std::string &msg, unsigned flags)
Definition: verbose.cpp:33
#define STXXL_END_NAMESPACE
Definition: namespace.h:17