-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debug.h
215 lines (185 loc) · 4.17 KB
/
Debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* @file Debug.h
* @author Dan R. Lipsa
* @date 15 Feb. 2006
* @ingroup utils
* @brief Debugging support
*/
#ifndef __DEBUG_H__
#define __DEBUG_H__
inline void RuntimeAssert (bool condition, const string& message)
{
if (! condition)
throw logic_error (message);
}
inline void ThrowException (const string& message)
{
throw logic_error (message);
}
template<typename T>
void RuntimeAssert (bool condition, const string& message, const T& t)
{
if (! condition)
{
ostringstream ostr;
ostr << message
<< " " << t << ends;
ThrowException (ostr.str ());
}
}
template<typename T1, typename T2>
void RuntimeAssert (bool condition, const string& message, const T1& t1,
const T2& t2)
{
if (! condition)
{
ostringstream ostr;
ostr << message
<< " " << t1
<< " " << t2 << ends;
ThrowException (ostr.str ());
}
}
template<typename T1, typename T2, typename T3>
void RuntimeAssert (bool condition,
const string& message,
const T1& t1, const T2& t2, const T3& t3)
{
if (! condition)
{
ostringstream ostr;
ostr << message
<< " " << t1
<< " " << t2
<< " " << t3 << ends;
ThrowException (ostr.str ());
}
}
template<typename T1, typename T2, typename T3, typename T4>
void RuntimeAssert (bool condition,
const string& message,
const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
if (! condition)
{
ostringstream ostr;
ostr << message
<< " " << t1
<< " " << t2
<< " " << t3
<< " " << t4 << ends;
ThrowException (ostr.str ());
}
}
template<typename T1, typename T2, typename T3, typename T4, typename T5>
void RuntimeAssert (bool condition,
const string& message,
const T1& t1, const T2& t2, const T3& t3, const T4& t4,
const T5& t5)
{
if (! condition)
{
ostringstream ostr;
ostr << message
<< " " << t1
<< " " << t2
<< " " << t3
<< " " << t4
<< " " << t5 << ends;
ThrowException (ostr.str ());
}
}
template<typename T>
void ThrowException (const string& message, const T& t)
{
RuntimeAssert (false, message, t);
}
template<typename T1, typename T2>
void ThrowException (const string& message, const T1& t1, const T2& t2)
{
RuntimeAssert (false, message, t1, t2);
}
template<typename T1, typename T2, typename T3>
void ThrowException (const string& message, const T1& t1, const T2& t2,
const T3& t3)
{
RuntimeAssert (false, message, t1, t2, t3);
}
/**
* Use the following pattern to log debugging code in functions:
* __ENABLE_LOGGING__;
* ...
* __LOG__ (logging code);
* When finished debugging, delete the __ENABLE_LOGGING__ statement
*/
#define __LOG__(code) if (__LOGGED__) {code}
#define __ENABLE_LOGGING__ const bool __LOGGED__ = true
const bool __LOGGED__ = false;
#ifdef _MSC_VER
/**
* @brief Debugging output stream for the debug console in Visual Studio.
*/
class DebugStream
{
public:
typedef ostream& (*EndlFunction) (ostream& o);
template <typename A>
DebugStream& operator<<(A a)
{
ostringstream ostr;
ostr << a << ends;
OutputDebugStringA (ostr.str ().c_str ());
return *this;
}
DebugStream& operator<< (EndlFunction)
{
OutputDebugStringA ("\n");
return *this;
}
};
/**
* Stream where all debugging output should be sent. Works bor both MS
* Visual C++ and for g++.
*/
extern DebugStream cdbg;
#else //_MSC_VER
/**
* Stream where all debugging output should be sent. Works bor both MS
* Visual C++ and for g++.
*/
extern ostream& cdbg;
#endif //_MSC_VER
/**
* @brief Measures time
*/
class MeasureTime
{
public:
MeasureTime ();
void StartInterval ();
void EndInterval (const char* intervalName);
void EndInterval (const string& intervalName)
{
EndInterval (intervalName.c_str ());
}
private:
clock_t m_start;
};
/**
* @brief Measures time for VTK routines
*/
class MeasureTimeVtk : public vtkCommand
{
public:
MeasureTimeVtk ()
{
}
void Execute (vtkObject *caller, unsigned long eventId, void *callData);
void Measure (vtkObject *caller);
private:
MeasureTime m_measure;
};
#endif //__DEBUG_H__
// Local Variables:
// mode: c++
// End: