-
Notifications
You must be signed in to change notification settings - Fork 0
/
alt-log.h
414 lines (357 loc) · 9.9 KB
/
alt-log.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
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <codecvt>
#include <vector>
#include <memory>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
namespace alt
{
class Log : protected std::ostream
{
public:
enum Color
{
BLACK, LBLACK,
RED, LRED,
GREEN, LGREEN,
BLUE, LBLUE,
YELLOW, LYELLOW,
MAGENTA, LMAGENTA,
CYAN, LCYAN,
WHITE, LWHITE
};
using StdStreamManip = std::ostream&(*)(std::ostream&);
using LogManip = Log & (*)(Log&);
Log& Write(const std::string& val)
{
for (auto& s : streams)
s->Put(val);
return *this;
}
template<class T>
Log& Put(const T& val)
{
(*static_cast<std::ostream*>(this)) << val;
return *this;
}
Log& Put(bool val)
{
return Put(val ? "true" : "false");
}
Log& Put(LogManip val)
{
return val(*this);
}
Log& Put(StdStreamManip val)
{
(*static_cast<std::ostream*>(this)) << val;
return *this;
}
template<class... Args>
Log& Put(LogManip val, const Args&... args)
{
return Put(val).Put(args...);
}
template<class T, class... Args>
Log& Put(const T& val, const Args&... args)
{
return Put(val).Put(" ").Put(args...);
}
Log& PutTime()
{
const time_t t = time(nullptr);
(*static_cast<std::ostream*>(this)) << std::put_time(localtime(&t), "[%H:%M:%S]") << std::flush;
return *this;
}
Log& PutColor(Color col)
{
*this << std::flush;
for (auto& s : streams)
s->PutColor(col);
return *this;
}
template<class T> Log& operator<<(const T& val) { return Put(val); }
Log& operator<<(StdStreamManip val) { return Put(val); }
Log& operator<<(LogManip val) { return Put(val); }
// Manipulators
static Log& Black(Log& log) { return log.PutColor(BLACK); }
static Log& LBlack(Log& log) { return log.PutColor(LBLACK); }
static Log& Red(Log& log) { return log.PutColor(RED); }
static Log& LRed(Log& log) { return log.PutColor(LRED); }
static Log& Green(Log& log) { return log.PutColor(GREEN); }
static Log& LGreen(Log& log) { return log.PutColor(LGREEN); }
static Log& Blue(Log& log) { return log.PutColor(BLUE); }
static Log& LBlue(Log& log) { return log.PutColor(LBLUE); }
static Log& Yellow(Log& log) { return log.PutColor(YELLOW); }
static Log& LYellow(Log& log) { return log.PutColor(LYELLOW); }
static Log& Magenta(Log& log) { return log.PutColor(MAGENTA); }
static Log& LMagenta(Log& log) { return log.PutColor(LMAGENTA); }
static Log& Cyan(Log& log) { return log.PutColor(CYAN); }
static Log& LCyan(Log& log) { return log.PutColor(LCYAN); }
static Log& White(Log& log) { return log.PutColor(WHITE); }
static Log& LWhite(Log& log) { return log.PutColor(LWHITE); }
static Log& Endl(Log& log) { return log.Put(std::endl).Put(std::dec).PutColor(WHITE); }
static Log& Time(Log& log) { return log.PutTime(); }
struct Log_Base
{
virtual Log& Begin() const = 0;
template<class... Args> Log& operator()(const Args&... args) const { return Begin().Put<Args...>(args...).Put(Endl); }
template<class T> Log& operator<<(const T& val) const { return Begin().Put<T>(val); }
};
#if __cpp_constexpr >= 201603L
static constexpr struct Log_Raw : public Log_Base {
Log& Begin() const override { return Instance(); }
} Raw{};
static constexpr struct Log_Info : public Log_Base {
Log& Begin() const override { return Instance().Put(White, Time, " "); }
} Info{};
static constexpr struct Log_Warning : public Log_Base {
Log& Begin() const override { return Instance().Put(LYellow, Time, "[Warning] "); }
} Warning{};
static constexpr struct Log_Error : public Log_Base {
Log& Begin() const override { return Instance().Put(Red, Time, "[Error] "); }
} Error{};
#ifndef NDEBUG
static constexpr struct Log_Debug : public Log_Base {
private:
Log& Begin() const override { return Instance().Put(Yellow, Time, "[Debug] "); }
} Debug{};
#else
static constexpr struct Log_Debug {
const Log_Debug& Begin() const { return *this; }
template<class T> const Log_Debug& operator<<(T) const { return *this; }
const Log_Debug& operator<<(StdStreamManip) const { return *this; }
template<class... Args> const Log_Debug& operator()(const Args&... args) const { return *this; }
} Debug{};
#endif // NDEBUG
#define ALT_LOG_IMPL
#else
static struct Log_Raw : public Log_Base {
Log& Begin() const override { return Instance(); }
} Raw;
static struct Log_Info : public Log_Base {
Log& Begin() const override { return Instance().Put(White, Time, " "); }
} Info;
static struct Log_Warning : public Log_Base {
Log& Begin() const override { return Instance().Put(LYellow, Time, "[Warning] "); }
} Warning;
static struct Log_Error : public Log_Base {
Log& Begin() const override { return Instance().Put(Red, Time, "[Error] "); }
} Error;
#ifndef NDEBUG
static struct Log_Debug : public Log_Base {
private:
Log& Begin() const override { return Instance().Put(Yellow, Time, "[Debug] "); }
} Debug;
#else
static struct Log_Debug {
private:
const Log_Debug& Begin() const { return *this; }
public:
template<class T> const Log_Debug& operator<<(T) const { return *this; }
const Log_Debug& operator<<(StdStreamManip) const { return *this; }
template<class... Args> const Log_Debug& operator()(const Args&... args) const { return *this; }
} Debug;
#endif // NDEBUG
#define ALT_LOG_IMPL \
::alt::Log::Log_Raw alt::Log::Raw; \
::alt::Log::Log_Info alt::Log::Info; \
::alt::Log::Log_Warning alt::Log::Warning; \
::alt::Log::Log_Error alt::Log::Error; \
::alt::Log::Log_Debug alt::Log::Debug;
#endif // __cpp_constexpr
static Log& Instance() noexcept
{
static Log s;
return s;
}
class Stream
{
public:
virtual Stream& Put(const std::string& val) = 0;
virtual Stream& PutColor(Color color) = 0;
};
void AddOut(Stream* stream) { streams.emplace_back(stream); }
static void Push(Stream* stream) { Instance().AddOut(stream); }
class ConsoleStream : public Stream
{
public:
Stream& Put(const std::string& val) override
{
#ifdef _WIN32
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(hConsole, val.data(), val.size(), NULL, NULL);
#else
std::cout << val;
#endif // _WIN32
return *this;
}
Stream& PutColor(Color val) override
{
#ifdef _WIN32
WORD col = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
switch (val)
{
case BLACK:
col = 0;
break;
case LBLACK:
col = FOREGROUND_INTENSITY;
break;
case RED:
col = FOREGROUND_RED;
break;
case LRED:
col = FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case GREEN:
col = FOREGROUND_GREEN;
break;
case LGREEN:
col = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case BLUE:
col = FOREGROUND_BLUE;
break;
case LBLUE:
col = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case YELLOW:
col = FOREGROUND_RED | FOREGROUND_GREEN;
break;
case LYELLOW:
col = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case MAGENTA:
col = FOREGROUND_RED | FOREGROUND_BLUE;
break;
case LMAGENTA:
col = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case CYAN:
col = FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case LCYAN:
col = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
case WHITE:
col = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
break;
case LWHITE:
col = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
break;
}
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, col);
#else
switch (val)
{
case BLACK:
std::cout << "\033[30m";
break;
case LBLACK:
std::cout << "\033[90m";
break;
case RED:
std::cout << "\033[31m";
break;
case LRED:
std::cout << "\033[91m";
break;
case GREEN:
std::cout << "\033[32m";
break;
case LGREEN:
std::cout << "\033[92m";
break;
case BLUE:
std::cout << "\033[34m";
break;
case LBLUE:
std::cout << "\033[94m";
break;
case YELLOW:
std::cout << "\033[33m";
break;
case LYELLOW:
std::cout << "\033[93m";
break;
case MAGENTA:
std::cout << "\033[35m";
break;
case LMAGENTA:
std::cout << "\033[95m";
break;
case CYAN:
std::cout << "\033[36m";
break;
case LCYAN:
std::cout << "\033[96m";
break;
case WHITE:
std::cout << "\033[37m";
break;
case LWHITE:
std::cout << "\033[97m";
break;
}
#endif // _WIN32
return *this;
}
};
class FileStream : public Stream
{
std::ofstream file;
public:
FileStream(const std::string& fileName) : file(fileName) { }
#if _MSC_VER
FileStream(const std::wstring& fileName) : file(fileName) { }
#endif
Stream& Put(const std::string& val) override
{
file << val << std::flush;
return *this;
}
Stream& PutColor(Color val) override { return *this; }
};
private:
class Buffer : public std::streambuf
{
static const std::size_t BUF_SIZE = 1024;
char buf[BUF_SIZE];
public:
using Traits = std::char_traits<char>;
Buffer() { setp(buf, buf + BUF_SIZE); }
protected:
virtual Traits::int_type overflow(Traits::int_type c = Traits::eof()) override
{
put(pbase(), pptr());
if (c != Traits::eof()) {
char c2 = c;
put(&c2, &c2 + 1);
}
setp(buf, buf + BUF_SIZE);
return c;
}
virtual int sync() override
{
put(pbase(), pptr());
setp(buf, buf + BUF_SIZE);
return 0;
}
private:
void put(const char* begin, const char* end) { Log::Instance().Write(std::string(begin, end)); }
};
Log() :std::ostream(&buf) { };
Log(const Log&) = delete;
Log& operator=(const Log&) = delete;
Buffer buf;
std::vector<std::unique_ptr<Stream>> streams;
};
}