Flow123d
global_defs.h
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file global_defs.h
26  * @brief Global macros to enhance readability and debugging, general constants.
27  *
28  */
29 
30 #ifndef GLOBAL_DEFS_H
31 #define GLOBAL_DEFS_H
32 
33 //Make sure that headers necessary for following macros are included.
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdlib.h>
37 
38 #include "system/exc_common.hh"
39 
40 
41 /*! @brief Debugging macros.
42  *
43  * The macro ASSERT has to be used for assertion tests. An error occures if
44  * given condition is violated. Macro accepts additional variables to print.
45  *
46  * Example:
47  * @verbatim
48  * ASSERT( i<size , "Array X overflow: index %d >= alocated size %d.\n",i,size);
49  * @endverbatim
50  *
51  * The macro INPUT_CHECK should be used for assertions about user input. So
52  * they produce User Error instead of Program error.
53  *
54  * The macro DBGMSG should be used for debugging messages,
55  * so they can be removed in production version.
56  *
57  * WARN_ASSERT - can be used for consistency tests in debugging version.
58  *
59  * @{
60  */
61 #define INPUT_CHECK(i,...) do { if (!(i)) xprintf(UsrErr,__VA_ARGS__); } while (0)
62 
63 /**
64  * Actually there are following debugging switches
65  * DEBUG_MESSAGES - use various debugging messages introduced by DBGMSG
66  * DEBUG_ASSERTS - use assertion checks introduced by ASSERT
67  * DEBUG_PROFILER - use profiling introduced by START_TIMER, END_TIMER
68  * DEBUG_FUNCTION_STACK - use function stack introduced by F_ENTRY
69  *
70  * You can turn all off defining: Flow123d_NODEBUG
71  * or turn all on defining: Flow123d_DEBUG
72  *
73  * Flow123d_DEBUG overrides Flow123d_NODEBUG
74  */
75 
76 #ifdef Flow123d_NODEBUG
77 
78 #undef DEBUG_MESSAGES
79 #undef DEBUG_ASSERTS
80 #undef DEBUG_PROFILER
81 #undef DEBUG_FUNCTION_STACK
82 
83 #endif
84 
85 
86 #ifdef Flow123d_DEBUG
87 
88 #define DEBUG_MESSAGES
89 #define DEBUG_ASSERTS
90 #define DEBUG_PROFILER
91 #define DEBUG_FUNCTION_STACK
92 
93 #endif
94 
95 
96 #ifdef DEBUG_ASSERTS
97 
98 /**
99  * Just quick hack to fix some unit tests.
100  * TODO:
101  * We should make better implementation, rather minimizing
102  * usage of macros. And make robust "system" part, that
103  * is MPI aware, but not MPI dependent.
104  */
105 #ifdef DEBUG_ASSERTS_WITHOUT_MPI
106 #define MPI_Comm_rank(A, B)
107 #endif // DEBUG_ASSERTS_WITHOUT_MPI
108 
109 #define ASSERT(i,...) do {\
110  if (!(i)) {\
111  char msg[1024];\
112  sprintf( msg, __VA_ARGS__);\
113  int rank=-1;\
114  MPI_Comm_rank(MPI_COMM_WORLD, &rank);\
115  THROW( ExcAssertMsg() << EI_Message(std::string(msg)) << EI_MPI_Rank(rank) );\
116  }} while (0)
117 
118 #define WARN_ASSERT(i,...) do { if (!(i)) xprintf(Warn,__VA_ARGS__); } while (0)
119 
120 
121 #else
122 
123 #define ASSERT(...)
124 #define WARN_ASSERT(...)
125 
126 #endif
127 
128 
129 
130 #ifdef DEBUG_ASSERTS
131 
132 #define ASSERT_EQUAL( a, b) do {\
133  stringstream ss; ss << (a) << " != " << (b); \
134  ASSERT( ((a) == (b)), "Violated assert: %s == %s,\n observed: %s.\n",#a, #b, ss.str().c_str()); \
135  } while (0)
136 #else
137 
138 #define ASSERT_EQUAL( a, b)
139 
140 #endif
141 
142 
143 
144 #ifdef DEBUG_ASSERTS
145 
146 #define ASSERT_LESS( a, b) do {\
147  stringstream ss; ss << (a) << " >= " << (b); \
148  ASSERT( ((a) < (b)) , "Violated assert: %s < %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
149  } while (0)
150 
151 
152 
153 
154 #if defined(ASSERT_LE) && defined(FLOW123D_INCLUDES_GTEST)
155 #undef ASSERT_LE
156 #endif
157 
158 
159 #define ASSERT_LE( a, b) do {\
160  stringstream ss; ss << (a) << " > " << (b); \
161  ASSERT( ((a) <= (b)) , "Violated assert: %s <= %s,\n observed: %s.\n",#a,#b, ss.str().c_str()); \
162  } while (0)
163 
164 #else
165 
166 #define ASSERT_LESS( a, b)
167 #define ASSERT_LE( a, b)
168 
169 #endif
170 
171 
172 
173 
174 #ifdef DEBUG_MESSAGES
175 
176 #define DBGMSG(...) do { xprintf(MsgDbg,__VA_ARGS__); fflush(NULL); } while (0)
177 
178 
179 /**
180  * Usage:
181  * DBGCOUT( << "xy" <<endl );
182  */
183 #define DBGCOUT(...) do { std::cout << " DBG (" \
184  << __FILE__ << ", " \
185  << __func__ << ", " \
186  << __LINE__ << ")" \
187  __VA_ARGS__; } while(0)
188 
189 
190 /**
191  * Usage:
192  * DBGVAR( arma::vec( "1 2 3" ) );
193  */
194 #define DBGVAR( var ) DBGCOUT( << #var << " = " << var << endl )
195 
196 #else
197 
198 #define DBGMSG(...)
199 #define DBGCOUT(...)
200 #define DBGVAR(var)
201 
202 #endif
203 
204 ///@}
205 
206 #endif // GLOBAL_DEFS_H