-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolelog.hpp
216 lines (189 loc) · 4.83 KB
/
consolelog.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
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
// console.log v1.2
// Javascript-styled console logger for C++
// https://github.com/111116/console.log
#pragma once
#include <iostream>
#include <unistd.h> // isatty
#include <unordered_map> // label-time pairs
#include <string> // labels
#include <chrono> // time
#include <cmath>
#include <mutex>
namespace github111116 {
class ConsoleLogger
{
protected:
std::ostream& out;
public:
std::string infoColor = "\033[0;32m";
std::string warnColor = "\033[1;33m";
std::string errorColor = "\033[1;31m";
bool colored = false;
int loglevel = 0;
ConsoleLogger(std::ostream& out, bool colored);
// basic logging functions
template <typename... Args> void log(const Args&... args); // loglevel:0
template <typename... Args> void info(const Args&... args); // loglevel:1
template <typename... Args> void warn(const Args&... args); // loglevel:2
template <typename... Args> void error(const Args&... args); // loglevel:3
template <typename... Args> void debug(const Args&... args); // only affected by NDEBUG flag
// timer functions
void time(const std::string& label = "default");
void timeLog(const std::string& label = "default");
void timeEnd(const std::string& label = "default");
// counter functions
long long count(const std::string& label = "default");
long long countReset(const std::string& label = "default");
protected:
void print();
template <typename T>
void print(const T& t);
template <typename T, typename... Args>
void print(const T& t, const Args&... args);
std::unordered_map<std::string, std::chrono::time_point<std::chrono::system_clock>> starttimes;
std::unordered_map<std::string, long long> counts;
std::mutex mtx;
};
}
// defining global variable "console"
__attribute__((weak)) github111116::ConsoleLogger console(std::cerr, isatty(fileno(stderr)));
// ========== Implementation Starts ===========
namespace github111116 {
inline ConsoleLogger::ConsoleLogger(std::ostream& out, bool colored):
out(out), colored(colored)
{
}
template <typename... Args>
void ConsoleLogger::log(const Args&... args)
{
if (loglevel > 0) return;
mtx.lock();
out << " ";
print(args...);
mtx.unlock();
}
template <typename... Args>
void ConsoleLogger::debug(const Args&... args)
{
#ifndef NDEBUG
mtx.lock();
out << "... ";
print(args...);
mtx.unlock();
#endif
}
template <typename... Args>
void ConsoleLogger::info(const Args&... args)
{
if (loglevel > 1) return;
mtx.lock();
if (colored) out << infoColor;
out << "[i] ";
if (colored) out << "\033[0m";
print(args...);
mtx.unlock();
}
template <typename... Args>
void ConsoleLogger::warn(const Args&... args)
{
if (loglevel > 2) return;
mtx.lock();
if (colored) out << warnColor;
out << "[!] ";
print(args...);
if (colored) out << "\033[0m";
mtx.unlock();
}
template <typename... Args>
void ConsoleLogger::error(const Args&... args)
{
if (loglevel > 3) return;
mtx.lock();
if (colored) out << errorColor;
out << "[X] ";
print(args...);
if (colored) out << "\033[0m";
mtx.unlock();
}
inline void ConsoleLogger::print()
{
out << std::endl;
}
template <typename T>
void ConsoleLogger::print(const T& t)
{
out << t;
print();
}
template <typename T, typename... Args>
void ConsoleLogger::print(const T& t, const Args&... args)
{
out << t << " ";
print(args...);
}
inline void ConsoleLogger::time(const std::string& label)
{
if (starttimes.count(label))
{
warn("Timer \"" + label + "\" already exists.");
return;
}
mtx.lock();
starttimes[label] = std::chrono::system_clock::now();
mtx.unlock();
}
inline void ConsoleLogger::timeLog(const std::string& label)
{
if (!starttimes.count(label))
{
warn("Timer \"" + label + "\" doesn't exist.");
return;
}
mtx.lock();
auto now = std::chrono::system_clock::now();
std::chrono::duration<double> seconds = now - starttimes[label];
if (loglevel <= 0)
out << " "+label+": " << (long long)(seconds.count()*1000)/1000 << "s" << std::endl;
mtx.unlock();
}
inline void ConsoleLogger::timeEnd(const std::string& label)
{
if (!starttimes.count(label))
{
warn("Timer \"" + label + "\" doesn't exist.");
return;
}
mtx.lock();
auto now = std::chrono::system_clock::now();
std::chrono::duration<double> seconds = now - starttimes[label];
if (loglevel <= 0)
out << " "+label+": " << 0.001*round(seconds.count()*1000) << "s - timer ended" << std::endl;
starttimes.erase(label);
mtx.unlock();
}
inline long long ConsoleLogger::countReset(const std::string& label)
{
if (!counts.count(label))
{
mtx.lock();
warn("Counter \"" + label + "\" doesn't exist.");
mtx.unlock();
return -1;
}
mtx.lock();
counts[label] = 0;
if (loglevel <= 0)
out << " "+label+": 0" << std::endl;
mtx.unlock();
return 0;
}
inline long long ConsoleLogger::count(const std::string& label)
{
mtx.lock();
int t = ++counts[label];
if (loglevel <= 0)
out << " "+label+": " << t << std::endl;
mtx.unlock();
return t;
}
} // end namespace