TTK
Loading...
Searching...
No Matches
Debug.h
Go to the documentation of this file.
1
2
9
22
23#pragma once
24
25#include <BaseClass.h>
26
27#include <algorithm>
28#include <array>
29#include <cerrno>
30#include <fstream>
31#include <iostream>
32#include <sstream>
33#include <string>
34#include <vector>
35
36namespace ttk {
37
38 COMMON_EXPORTS extern bool welcomeMsg_;
39 COMMON_EXPORTS extern bool goodbyeMsg_;
41
42 namespace debug {
43 enum class Priority : int {
44 ERROR, // 0
45 WARNING, // 1
46 PERFORMANCE, // 2
47 INFO, // 3
48 DETAIL, // 4
49 VERBOSE // 5
50 };
51
52 enum class Separator : char {
53 L0 = '%',
54 L1 = '=',
55 L2 = '-',
56 SLASH = '/',
57 BACKSLASH = '\\'
58 };
59
60 enum class LineMode : int {
61 NEW,
62 APPEND, // append
63 REPLACE // replace line and append
64 };
65
66 namespace output {
67 const std::string BOLD = "\33[0;1m";
68 const std::string GREY = "\33[2;1m";
69 const std::string ITALIC = "\33[3;1m";
70 const std::string UNDERLINED = "\33[4;1m";
71 const std::string FLASHING = "\33[5;1m";
72 const std::string INVERTED = "\33[7;1m";
73 const std::string STRIKETHROUGH = "\33[9;1m";
74 const std::string DARKGREY = "\33[30;1m";
75 const std::string RED = "\33[31;1m";
76 const std::string GREEN = "\33[32;1m";
77 const std::string YELLOW = "\33[33;1m";
78 const std::string BLUE = "\33[34;1m";
79 const std::string PINK = "\33[35;1m";
80 const std::string LIGHTBLUE = "\33[36;1m";
81 const std::string BRIGHTWHITE = "\33[37;1m";
82 const std::string ENDCOLOR = "\33[0m";
83 } // namespace output
84
85 const int LINEWIDTH = 80;
86 } // namespace debug
87
88 class Debug : public BaseClass {
89
90 public:
91 // 1) constructors, destructors, operators, etc.
92 Debug();
93
94 ~Debug() override;
95
96 // 2) functions
100 virtual int setDebugLevel(const int &debugLevel);
101
109 int setWrapper(const Wrapper *wrapper) override;
110
111 // =========================================================================
112 // New Debug Methods
113 // =========================================================================
114
118 inline int printMsg(const std::string &msg,
119 const debug::Priority &priority = debug::Priority::INFO,
120 const debug::LineMode &lineMode = debug::LineMode::NEW,
121 std::ostream &stream = std::cout) const {
122 if((this->debugLevel_ < (int)priority)
123 && (globalDebugLevel_ < (int)priority))
124 return 0;
125
126 return this->printMsgInternal(msg, priority, lineMode, stream);
127 }
128
132 inline int printMsg(const std::vector<std::string> &msgs,
133 const debug::Priority &priority = debug::Priority::INFO,
134 const debug::LineMode &lineMode = debug::LineMode::NEW,
135 std::ostream &stream = std::cout) const {
136 if((this->debugLevel_ < (int)priority)
137 && (globalDebugLevel_ < (int)priority))
138 return 0;
139
140 size_t prints = 0;
141 for(auto &msg : msgs)
142 prints += this->printMsgInternal(msg, priority, lineMode, stream);
143 return prints == msgs.size() ? 1 : 0;
144 }
145
149 inline int printErr(const std::string &msg,
150 const debug::LineMode &lineMode = debug::LineMode::NEW,
151 std::ostream &stream = std::cerr) const {
152 return this->printMsgInternal(
153 msg, debug::Priority::ERROR, lineMode, stream);
154 }
155
159 inline int printWrn(const std::string &msg,
160 const debug::LineMode &lineMode = debug::LineMode::NEW,
161 std::ostream &stream = std::cerr) const {
162 return this->printMsgInternal(
163 msg, debug::Priority::WARNING, lineMode, stream);
164 }
165
171 inline int printMsg(const std::string &msg,
172 const double &progress,
173 const double &time,
174 const int &threads,
175 const double &memory,
176 const debug::LineMode &lineMode = debug::LineMode::NEW,
177 const debug::Priority &priority
179 std::ostream &stream = std::cout) const {
180 if((this->debugLevel_ < (int)priority)
181 && (globalDebugLevel_ < (int)priority))
182 return 0;
183
184 std::array<std::string, 4> chunks{};
185 size_t q = 0;
186
187 if(memory >= 0)
188 chunks[q++] = std::to_string(static_cast<int>(memory)) + "MB";
189 if(time >= 0) {
190 std::stringstream sStream;
191 sStream.precision(3);
192 sStream << std::fixed;
193 sStream << time;
194 chunks[q++] = sStream.str() + "s";
195 }
196 if(threads >= 0)
197 chunks[q++] = std::to_string(threads) + "T";
198 if(progress >= 0)
199 chunks[q++] = std::to_string((int)(progress * 100)) + "%";
200
201 std::string stats = "";
202 if(q > 0) {
203 stats += " [";
204 stats += chunks[0];
205 for(size_t i = 1; i < q; i++)
206 stats += "|" + chunks[i];
207
208 stats += "]";
209 }
210
211 return this->printMsgInternal(
212 msg, stats, msg.length() < 1 ? ">" : ".", priority, lineMode, stream);
213 }
214
218 inline int printMsg(const std::string &msg,
219 const double &progress,
220 const double &time,
221 const debug::LineMode &lineMode = debug::LineMode::NEW,
222 const debug::Priority &priority
224 std::ostream &stream = std::cout) const {
225 return this->printMsg(
226 msg, progress, time, -1, -1, lineMode, priority, stream);
227 }
228
233 inline int printMsg(const std::string &msg,
234 const double &progress,
235 const double &time,
236 const int &threads,
237 const debug::LineMode &lineMode = debug::LineMode::NEW,
238 const debug::Priority &priority
240 std::ostream &stream = std::cout) const {
241 return this->printMsg(
242 msg, progress, time, threads, -1, lineMode, priority, stream);
243 }
244
248 inline int printMsg(const std::string &msg,
249 const double &progress,
250 const debug::LineMode &lineMode = debug::LineMode::NEW,
251 const debug::Priority &priority
253 std::ostream &stream = std::cout) const {
254 return this->printMsg(
255 msg, progress, -1, -1, -1, lineMode, priority, stream);
256 }
257
262 inline int printMsg(const std::string &msg,
263 const double &progress,
264 const debug::Priority &priority,
265 const debug::LineMode &lineMode = debug::LineMode::NEW,
266 std::ostream &stream = std::cout) const {
267 return this->printMsg(msg, progress, -1, -1, lineMode, priority, stream);
268 }
269
273 inline int printMsg(const std::vector<std::vector<std::string>> &rows,
274 const debug::Priority &priority = debug::Priority::INFO,
275 const bool hasHeader = true,
276 const debug::LineMode &lineMode = debug::LineMode::NEW,
277 std::ostream &stream = std::cout) const {
278 if((this->debugLevel_ < (int)priority)
279 && (globalDebugLevel_ < (int)priority))
280 return 0;
281
282 const int nRows = rows.size();
283 const int nCols = nRows > 0 ? rows[0].size() : 0;
284 if(nCols < 1)
285 return 0;
286
287 std::vector<std::string> formattedRows(nRows);
288 std::vector<size_t> colSizes(nCols, 0);
289 for(int i = 0; i < nRows; i++)
290 for(int j = 0; j < nCols; j++)
291 colSizes[j] = std::max(colSizes[j], rows[i][j].size());
292
293 auto formatCell = [](const std::string &value, const size_t &width,
294 const std::string &fillSymbol) {
295 std::string cell = value;
296 const int diff = width - cell.size();
297 for(int i = 0; i < diff; i++)
298 cell += fillSymbol;
299 return cell;
300 };
301
302 // Values
303 int resultIndex = 0;
304 for(int i = 0; i < nRows; i++) {
305 auto &row = formattedRows[resultIndex++];
306 row
307 = formatCell(rows[i][0], colSizes[0], " ") + (hasHeader ? ": " : "");
308 if(nCols > 1)
309 row += formatCell(rows[i][1], colSizes[1], " ");
310 for(int j = 2; j < nCols; j++)
311 row += "," + formatCell(rows[i][j], colSizes[j], " ");
312 }
313
314 return this->printMsg(formattedRows, priority, lineMode, stream);
315 }
316
320 inline int printMsg(const debug::Separator &separator,
321 const debug::LineMode &lineMode = debug::LineMode::NEW,
322 const debug::Priority &priority = debug::Priority::INFO,
323 std::ostream &stream = std::cout) const {
324 if((this->debugLevel_ < (int)priority)
325 && (globalDebugLevel_ < (int)priority))
326 return 0;
327
328 return this->printMsgInternal("", "",
329 std::string(1, (const char &)separator),
330 priority, lineMode, stream);
331 }
332
336 inline int printMsg(const debug::Separator &separator,
337 const debug::Priority &priority,
338 const debug::LineMode &lineMode = debug::LineMode::NEW,
339 std::ostream &stream = std::cout) const {
340 return this->printMsg(separator, lineMode, priority, stream);
341 }
342
346 inline int printMsg(const std::string &msg,
347 const debug::Separator &separator,
348 const debug::LineMode &lineMode = debug::LineMode::NEW,
349 const debug::Priority &priority = debug::Priority::INFO,
350 std::ostream &stream = std::cout) const {
351 if((this->debugLevel_ < (int)priority)
352 && (globalDebugLevel_ < (int)priority))
353 return 0;
354
355 return this->printMsgInternal(msg, "",
356 std::string(1, (const char &)separator),
357 priority, lineMode, stream);
358 }
359
364 inline void setDebugMsgPrefix(const std::string &prefix) {
365 this->debugMsgNamePrefix_ = prefix;
366#ifdef TTK_ENABLE_MPI
367 this->debugMsgPrefix_
368 = debugMsgNamePrefix_.length() > 0
369 ? "[" + debugMsgNamePrefix_ + "-" + std::to_string(MPIrank_) + "] "
370 : "";
371#else
372 this->debugMsgPrefix_ = debugMsgNamePrefix_.length() > 0
373 ? "[" + debugMsgNamePrefix_ + "] "
374 : "";
375#endif // TTK_ENABLE_MPI
376 }
377
378 protected:
379 mutable int debugLevel_;
380
382
383 std::string debugMsgPrefix_;
385
389 inline int
390 printMsgInternal(const std::string &msg,
391 const std::string &right,
392 const std::string &filler,
393 const debug::Priority &priority = debug::Priority::INFO,
394 const debug::LineMode &lineMode = debug::LineMode::NEW,
395 std::ostream &stream = std::cout) const {
396
397 std::string combinedMsg = msg;
398
399 if(filler.length() > 0) {
400 if(msg.length() > 0)
401 combinedMsg += " ";
402
403 int gapWidth = debug::LINEWIDTH - this->debugMsgPrefix_.length()
404 - combinedMsg.length() - right.length();
405 gapWidth = std::max(gapWidth / filler.length(), (size_t)1);
406
407 for(int i = 0; i < gapWidth; i++)
408 combinedMsg += filler;
409
410 combinedMsg += debug::output::BLUE + right + debug::output::ENDCOLOR;
411 }
412
413 return this->printMsgInternal(combinedMsg, priority, lineMode, stream);
414 }
415
419 inline int printMsgInternal(const std::string &msg,
420 const debug::Priority &priority,
421 const debug::LineMode &lineMode,
422 std::ostream &stream = std::cout) const {
423
424 if((this->debugLevel_ < (int)priority)
425 && (globalDebugLevel_ < (int)priority))
426 return 0;
427
428 // on error or warning print end of line
429 if((int)priority < 2 && this->lastLineMode == debug::LineMode::REPLACE)
430 stream << "\n";
431
432 // print prefix
433 if(lineMode != debug::LineMode::APPEND)
434 stream << debug::output::GREEN << this->debugMsgPrefix_
436
437 // print error or warning prefix
438 if((int)priority == 0)
439 stream << debug::output::RED << "[ERROR]" << debug::output::ENDCOLOR
440 << " ";
441 else if((int)priority == 1)
442 stream << debug::output::YELLOW << "[WARNING]"
443 << debug::output::ENDCOLOR << " ";
444
445 // print msg
446 stream << msg.data();
447
448 // go either into new line or replace current line
449 if(lineMode == debug::LineMode::NEW)
450 stream << "\n";
451 else if(lineMode == debug::LineMode::REPLACE)
452 stream << "\r";
453
454 // flush stream
455 stream.flush();
456
457 this->lastLineMode = lineMode;
458
459 return 1;
460 }
461
462 int welcomeMsg(std::ostream &stream);
463 };
464
465} // namespace ttk
466
467#include <Os.h>
468#include <Timer.h>
469
470namespace ttk {
472 class DebugTimer : public Timer {};
474 class DebugMemory : public Memory {};
475} // namespace ttk
476
477#include <OrderDisambiguation.h>
478
#define COMMON_EXPORTS
Definition BaseClass.h:31
TTK base package.
Definition BaseClass.h:70
Legacy backward compatibility.
Definition Debug.h:474
Legacy backward compatibility.
Definition Debug.h:472
Minimalist debugging class.
Definition Debug.h:88
int debugLevel_
Definition Debug.h:379
int printMsg(const debug::Separator &separator, const debug::Priority &priority, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cout) const
Definition Debug.h:336
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
Definition Debug.h:390
int printWrn(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:159
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
Definition Debug.h:233
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
Definition Debug.h:273
int welcomeMsg(std::ostream &stream)
Definition Debug.cpp:34
static COMMON_EXPORTS debug::LineMode lastLineMode
Definition Debug.h:381
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
Definition Debug.h:171
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
Definition Debug.h:248
int setWrapper(const Wrapper *wrapper) override
Definition Debug.cpp:155
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
Definition Debug.h:346
std::string debugMsgNamePrefix_
Definition Debug.h:384
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
virtual int setDebugLevel(const int &debugLevel)
Definition Debug.cpp:147
int printMsgInternal(const std::string &msg, const debug::Priority &priority, const debug::LineMode &lineMode, std::ostream &stream=std::cout) const
Definition Debug.h:419
~Debug() override
Definition Debug.cpp:24
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
Definition Debug.h:262
std::string debugMsgPrefix_
Definition Debug.h:383
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
Definition Debug.h:218
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
Definition Debug.h:132
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
Definition Debug.h:320
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
Definition Debug.h:118
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
Wrapper class to wrap ttk code.
Definition Wrapper.h:14
const std::string ENDCOLOR
Definition Debug.h:82
const std::string PINK
Definition Debug.h:79
const std::string BRIGHTWHITE
Definition Debug.h:81
const std::string ITALIC
Definition Debug.h:69
const std::string INVERTED
Definition Debug.h:72
const std::string BOLD
Definition Debug.h:67
const std::string FLASHING
Definition Debug.h:71
const std::string LIGHTBLUE
Definition Debug.h:80
const std::string GREY
Definition Debug.h:68
const std::string RED
Definition Debug.h:75
const std::string YELLOW
Definition Debug.h:77
const std::string GREEN
Definition Debug.h:76
const std::string DARKGREY
Definition Debug.h:74
const std::string STRIKETHROUGH
Definition Debug.h:73
const std::string BLUE
Definition Debug.h:78
const std::string UNDERLINED
Definition Debug.h:70
const int LINEWIDTH
Definition Debug.h:85
The Topology ToolKit.
COMMON_EXPORTS int MPIrank_
Definition BaseClass.cpp:9
COMMON_EXPORTS bool goodbyeMsg_
Definition Debug.cpp:7
COMMON_EXPORTS bool welcomeMsg_
Definition Debug.cpp:6
COMMON_EXPORTS int globalDebugLevel_
Definition Debug.cpp:8
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)