-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDebugger.hpp
118 lines (94 loc) · 5.25 KB
/
MyDebugger.hpp
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
#ifndef MYDEBUGGER_H
#define MYDEBUGGER_H
#include <iostream>
#include <tuple>
#include <vector>
#include <set>
#include <map>
// REFER: https://github.com/fenilgmehta/CS744-PA4-Key-Value-Store/blob/master/src/MyDebugger.hpp
// DEBUG macros, to set the flag from commnad line, comment the below line and use `g++ name.cpp -DPRINT_DEBUG`
#define PRINT_DEBUG
#define db(...) dbg(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#define dbiter(...) dbgIter(#__VA_ARGS__, __VA_ARGS__)
#ifdef PRINT_DEBUG
#define printFunction(outStream, functionName, argDelimiter, lineDelimiter) template <typename Arg, typename... Args> inline void functionName(Arg&& arg, Args&&... args) { outStream << arg; (void)(int[]){0, (void(outStream << argDelimiter << args),0)...}; outStream << lineDelimiter; }
printFunction(std::cerr, printErr, " "<<"\033[1;31m"<<"●"<<"\033[0m"<<" ", '\n');
template<class T, class... U> void dbg(int32_t lineNo, const char *sdbg, T h, U... a) {std::cerr<<"\033[1;31m"<<"Debug (" << lineNo << ") : "<<"\033[0m"; std::cerr<<sdbg; std::cerr<<" "<<"\033[1;41m"<<"="<<"\033[0m"<<" "; printErr(h, a...); std::cout.flush(); std::cerr.flush();}
template <class S, class T>std::ostream& operator <<(std::ostream& os, const std::pair<S, T>& p) {return os << "pair(" << p.first << "\033[1;31m" << ", " << "\033[0m" << p.second << ")";}
template <class T>std::ostream& operator <<(std::ostream& os, const std::vector<T>& p) {os << "\033[1;32m" << "vector[ " << "\033[0m"; for (const auto& it : p) os << it << "\033[1;31m" << ", " << "\033[0m"; return os << "\033[1;32m" << "]" << "\033[0m";}
template <class T>std::ostream& operator <<(std::ostream& os, const std::set<T>& p) {os << "\033[1;32m" << "set[ "; for (const auto& it : p) os << it << "\033[1;31m" << ", " << "\033[0m"; return os << "\033[1;32m" << "]" << "\033[0m";}
template <class S, class T>std::ostream& operator <<(std::ostream& os, const std::map<S, T>& p) {os << "\033[1;32m" << "map[ " << "\033[0m"; for (const auto& it : p) os << it << "\033[1;31m" << ", " << "\033[0m"; return os << "\033[1;32m" << "]" << "\033[0m";}
template <class T> void dbgIter(const char *sdbg, T a, T b) {std::cerr<<"\033[1;31m"<<"Debug: "<<"\033[0m"; std::cerr<<sdbg; std::cerr<<"\033[1;31m"<<" = "<<"\033[0m"; std::cerr << "["; for (T i = a; i != b; ++i) {if (i != a) std::cerr << ", "; std::cerr << *i;} std::cerr << "]\n"; std::cout.flush(); std::cerr.flush();}
#else
template<class T, class... U> void dbg(int32_t lineNo, const char *sdbg, T h, U... a) {}
template <class T> void dbgIter(const char *sdbg, T a, T b) {}
#endif
// REFER: https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_YELLOW = 33,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
BG_YELLOW = 43,
BG_BLUE = 44,
BG_DEFAULT = 49
};
#define color(enum_code) "\033[" << enum_code << "m"
// NOTE: TID is Thread ID
// REFER: https://www.geeksforgeeks.org/thread-get_id-function-in-c/
// TODO: comment this line when doing performance testing and when using IDE CmakeLists.txt
// #define DEBUGGING_ON
#ifdef DEBUGGING_ON
// REFER: https://stackoverflow.com/questions/7432100/how-to-get-integer-thread-id-in-c11
// (std::this_thread::get_id())
/* Blue message */
template <typename T>
void log_info(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false) {
if(prependNewLine) std::cout << '\n';
std::cout << color(FG_BLUE) << "INFO : " << color(FG_DEFAULT) << msg << '\n';
if(appendExtraNewLine) std::cout << '\n';
std::cout.flush();
}
/* Yellow message */
template <typename T>
void log_warning(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false) {
if(prependNewLine) std::cerr << '\n';
std::cerr << color(FG_YELLOW) << "WARNING : " << color(FG_DEFAULT) << msg << '\n';
if(appendExtraNewLine) std::cerr << '\n';
std::cerr.flush();
}
#else
template<typename T>
void log_info(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false) {}
template <typename T>
void log_warning(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false) {}
#endif
/* Yellow message */
template <typename T>
void log_error_warning(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false) {
if(prependNewLine) std::cerr << '\n';
std::cerr << color(FG_YELLOW) << "WARNING : " << color(FG_DEFAULT) << msg << '\n';
if(appendExtraNewLine) std::cerr << '\n';
std::cerr.flush();
}
/* Green message */
template <typename T>
void log_success(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false) {
if(prependNewLine) std::cout << '\n';
std::cout << color(FG_GREEN) << "SUCCESS : " << color(FG_DEFAULT) << msg << '\n';
if(appendExtraNewLine) std::cout << '\n';
std::cout.flush();
}
/* Red message */
template <typename T>
void log_error(const T& msg, bool prependNewLine = false, bool appendExtraNewLine = false){
if(prependNewLine) std::cerr << '\n';
std::cerr << color(FG_RED) << "ERROR : " << color(FG_DEFAULT) << msg << std::endl;
if(appendExtraNewLine) std::cerr << '\n';
std::cerr.flush();
}
#undef color
#endif // MYDEBUGGER_H