/
Debug.h
479 lines (407 loc) · 15.8 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/// \namespace ttk The Topology ToolKit
/// \mainpage TTK 1.2 Documentation
/// \image html "splash.png"
/// Useful links:
/// - TTK Home:
/// <a href="https://topology-tool-kit.github.io/"
/// target="new">http://topology-tool-kit .github.io/</a>
/// \defgroup base base
/// \brief The Topology ToolKit - Base code processing packages.
/// @{
/// \class ttk::Debug
/// \author Julien Tierny <julien.tierny@lip6.fr>
/// \date February 2011.
///
/// \brief Minimalist debugging class.
///
/// %Debug provides a few mechanisms to handle debugging messages at a global
/// and local scope, time and memory measurements, etc.
/// Each ttk class should inheritate from it.
#pragma once
#include <BaseClass.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace ttk {
COMMON_EXPORTS extern bool welcomeMsg_;
COMMON_EXPORTS extern bool goodbyeMsg_;
COMMON_EXPORTS extern int globalDebugLevel_;
namespace debug {
enum class Priority : int {
ERROR, // 0
WARNING, // 1
PERFORMANCE, // 2
INFO, // 3
DETAIL, // 4
VERBOSE // 5
};
enum class Separator : char {
L0 = '%',
L1 = '=',
L2 = '-',
SLASH = '/',
BACKSLASH = '\\'
};
enum class LineMode : int {
NEW,
APPEND, // append
REPLACE // replace line and append
};
namespace output {
const std::string BOLD = "\33[0;1m";
const std::string GREY = "\33[2;1m";
const std::string ITALIC = "\33[3;1m";
const std::string UNDERLINED = "\33[4;1m";
const std::string FLASHING = "\33[5;1m";
const std::string INVERTED = "\33[7;1m";
const std::string STRIKETHROUGH = "\33[9;1m";
const std::string DARKGREY = "\33[30;1m";
const std::string RED = "\33[31;1m";
const std::string GREEN = "\33[32;1m";
const std::string YELLOW = "\33[33;1m";
const std::string BLUE = "\33[34;1m";
const std::string PINK = "\33[35;1m";
const std::string LIGHTBLUE = "\33[36;1m";
const std::string BRIGHTWHITE = "\33[37;1m";
const std::string ENDCOLOR = "\33[0m";
} // namespace output
const int LINEWIDTH = 80;
} // namespace debug
class Debug : public BaseClass {
public:
// 1) constructors, destructors, operators, etc.
Debug();
~Debug() override;
// 2) functions
/// Set the debug level of a particular object. The global variable
/// globalDebugLevel_ will over-ride this setting if it has a lower value.
/// \return Returns 0 upon success, negative values otherwise.
virtual int setDebugLevel(const int &debugLevel);
/// Specify a pointer to a calling object that wraps the current class
/// deriving from ttk::BaseClass.
///
/// This function is useful to pass the execution context (debug level,
/// number of threads, etc.) from a wrapper to a base object.
/// \param wrapper Pointer to the wrapping object.
/// \return Returns 0 upon success, negative values otherwise.
int setWrapper(const Wrapper *wrapper) override;
// =========================================================================
// New Debug Methods
// =========================================================================
/**
* Prints a string debug message.
*/
inline int printMsg(const std::string &msg,
const debug::Priority &priority = debug::Priority::INFO,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
return this->printMsgInternal(msg, priority, lineMode, stream);
}
/**
* Prints multiple string debug messages at once.
*/
inline int printMsg(const std::vector<std::string> &msgs,
const debug::Priority &priority = debug::Priority::INFO,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
size_t prints = 0;
for(auto &msg : msgs)
prints += this->printMsgInternal(msg, priority, lineMode, stream);
return prints == msgs.size() ? 1 : 0;
}
/**
* Prints an error debug message.
*/
inline int printErr(const std::string &msg,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cerr) const {
return this->printMsgInternal(
msg, debug::Priority::ERROR, lineMode, stream);
}
/**
* Prints a warning debug message.
*/
inline int printWrn(const std::string &msg,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cerr) const {
return this->printMsgInternal(
msg, debug::Priority::WARNING, lineMode, stream);
}
/**
* Prints a performance debug message with specified progress, time,
* nThreads, and memory (values can be omitted form the message by passing
* the value -1)
*/
inline int printMsg(const std::string &msg,
const double &progress,
const double &time,
const int &threads,
const double &memory,
const debug::LineMode &lineMode = debug::LineMode::NEW,
const debug::Priority &priority
= debug::Priority::PERFORMANCE,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
std::array<std::string, 4> chunks{};
size_t q = 0;
if(memory >= 0)
chunks[q++] = std::to_string(static_cast<int>(memory)) + "MB";
if(time >= 0) {
std::stringstream sStream;
sStream.precision(3);
sStream << std::fixed;
sStream << time;
chunks[q++] = sStream.str() + "s";
}
if(threads >= 0)
chunks[q++] = std::to_string(threads) + "T";
if(progress >= 0)
chunks[q++] = std::to_string((int)(progress * 100)) + "%";
std::string stats = "";
if(q > 0) {
stats += " [";
stats += chunks[0];
for(size_t i = 1; i < q; i++)
stats += "|" + chunks[i];
stats += "]";
}
return this->printMsgInternal(
msg, stats, msg.length() < 1 ? ">" : ".", priority, lineMode, stream);
}
/**
* Prints a performance debug message with specified progress and time.
*/
inline int printMsg(const std::string &msg,
const double &progress,
const double &time,
const debug::LineMode &lineMode = debug::LineMode::NEW,
const debug::Priority &priority
= debug::Priority::PERFORMANCE,
std::ostream &stream = std::cout) const {
return this->printMsg(
msg, progress, time, -1, -1, lineMode, priority, stream);
}
/**
* Prints a performance debug message with specified progress, time, and
* threads.
*/
inline int printMsg(const std::string &msg,
const double &progress,
const double &time,
const int &threads,
const debug::LineMode &lineMode = debug::LineMode::NEW,
const debug::Priority &priority
= debug::Priority::PERFORMANCE,
std::ostream &stream = std::cout) const {
return this->printMsg(
msg, progress, time, threads, -1, lineMode, priority, stream);
}
/**
* Prints a performance debug message with specified progress.
*/
inline int printMsg(const std::string &msg,
const double &progress,
const debug::LineMode &lineMode = debug::LineMode::NEW,
const debug::Priority &priority
= debug::Priority::PERFORMANCE,
std::ostream &stream = std::cout) const {
return this->printMsg(
msg, progress, -1, -1, -1, lineMode, priority, stream);
}
/**
* Prints a performance debug message with specified progress and custom
* priority.
*/
inline int printMsg(const std::string &msg,
const double &progress,
const debug::Priority &priority,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cout) const {
return this->printMsg(msg, progress, -1, -1, lineMode, priority, stream);
}
/**
* Prints a table.
*/
inline int printMsg(const std::vector<std::vector<std::string>> &rows,
const debug::Priority &priority = debug::Priority::INFO,
const bool hasHeader = true,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
const int nRows = rows.size();
const int nCols = nRows > 0 ? rows[0].size() : 0;
if(nCols < 1)
return 0;
std::vector<std::string> formattedRows(nRows);
std::vector<size_t> colSizes(nCols, 0);
for(int i = 0; i < nRows; i++)
for(int j = 0; j < nCols; j++)
colSizes[j] = std::max(colSizes[j], rows[i][j].size());
auto formatCell = [](const std::string &value, const size_t &width,
const std::string &fillSymbol) {
std::string cell = value;
const int diff = width - cell.size();
for(int i = 0; i < diff; i++)
cell += fillSymbol;
return cell;
};
// Values
int resultIndex = 0;
for(int i = 0; i < nRows; i++) {
auto &row = formattedRows[resultIndex++];
row
= formatCell(rows[i][0], colSizes[0], " ") + (hasHeader ? ": " : "");
if(nCols > 1)
row += formatCell(rows[i][1], colSizes[1], " ");
for(int j = 2; j < nCols; j++)
row += "," + formatCell(rows[i][j], colSizes[j], " ");
}
return this->printMsg(formattedRows, priority, lineMode, stream);
}
/**
* Prints a separator.
*/
inline int printMsg(const debug::Separator &separator,
const debug::LineMode &lineMode = debug::LineMode::NEW,
const debug::Priority &priority = debug::Priority::INFO,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
return this->printMsgInternal("", "",
std::string(1, (const char &)separator),
priority, lineMode, stream);
}
/**
* Prints a separator with custom priority.
*/
inline int printMsg(const debug::Separator &separator,
const debug::Priority &priority,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cout) const {
return this->printMsg(separator, lineMode, priority, stream);
}
/**
* Prints a message and fills the remaining space with a separator.
*/
inline int printMsg(const std::string &msg,
const debug::Separator &separator,
const debug::LineMode &lineMode = debug::LineMode::NEW,
const debug::Priority &priority = debug::Priority::INFO,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
return this->printMsgInternal(msg, "",
std::string(1, (const char &)separator),
priority, lineMode, stream);
}
/**
* Sets the prefix that will be print at the beginning of every
* debug message.
*/
inline void setDebugMsgPrefix(const std::string &prefix) {
this->debugMsgNamePrefix_ = prefix;
#ifdef TTK_ENABLE_MPI
this->debugMsgPrefix_
= debugMsgNamePrefix_.length() > 0
? "[" + debugMsgNamePrefix_ + "-" + std::to_string(MPIrank_) + "] "
: "";
#else
this->debugMsgPrefix_ = debugMsgNamePrefix_.length() > 0
? "[" + debugMsgNamePrefix_ + "] "
: "";
#endif // TTK_ENABLE_MPI
}
protected:
mutable int debugLevel_;
COMMON_EXPORTS static debug::LineMode lastLineMode;
std::string debugMsgPrefix_;
std::string debugMsgNamePrefix_;
/**
* Internal debug method that formats debug messages.
*/
inline int
printMsgInternal(const std::string &msg,
const std::string &right,
const std::string &filler,
const debug::Priority &priority = debug::Priority::INFO,
const debug::LineMode &lineMode = debug::LineMode::NEW,
std::ostream &stream = std::cout) const {
std::string combinedMsg = msg;
if(filler.length() > 0) {
if(msg.length() > 0)
combinedMsg += " ";
int gapWidth = debug::LINEWIDTH - this->debugMsgPrefix_.length()
- combinedMsg.length() - right.length();
gapWidth = std::max(gapWidth / filler.length(), (size_t)1);
for(int i = 0; i < gapWidth; i++)
combinedMsg += filler;
combinedMsg += debug::output::BLUE + right + debug::output::ENDCOLOR;
}
return this->printMsgInternal(combinedMsg, priority, lineMode, stream);
}
/**
* Internal debug method that actually prints messages.
*/
inline int printMsgInternal(const std::string &msg,
const debug::Priority &priority,
const debug::LineMode &lineMode,
std::ostream &stream = std::cout) const {
if((this->debugLevel_ < (int)priority)
&& (globalDebugLevel_ < (int)priority))
return 0;
// on error or warning print end of line
if((int)priority < 2 && this->lastLineMode == debug::LineMode::REPLACE)
stream << "\n";
// print prefix
if(lineMode != debug::LineMode::APPEND)
stream << debug::output::GREEN << this->debugMsgPrefix_
<< debug::output::ENDCOLOR;
// print error or warning prefix
if((int)priority == 0)
stream << debug::output::RED << "[ERROR]" << debug::output::ENDCOLOR
<< " ";
else if((int)priority == 1)
stream << debug::output::YELLOW << "[WARNING]"
<< debug::output::ENDCOLOR << " ";
// print msg
stream << msg.data();
// go either into new line or replace current line
if(lineMode == debug::LineMode::NEW)
stream << "\n";
else if(lineMode == debug::LineMode::REPLACE)
stream << "\r";
// flush stream
stream.flush();
this->lastLineMode = lineMode;
return 1;
}
int welcomeMsg(std::ostream &stream);
};
} // namespace ttk
#include <Os.h>
#include <Timer.h>
namespace ttk {
/// \brief Legacy backward compatibility
class DebugTimer : public Timer {};
/// \brief Legacy backward compatibility.
class DebugMemory : public Memory {};
} // namespace ttk
#include <OrderDisambiguation.h>
/// @}