forked from Lbardoux/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.hpp
417 lines (405 loc) · 17.3 KB
/
logs.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
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
#ifndef MTL_LOGS_HPP_INCLUDED
#define MTL_LOGS_HPP_INCLUDED
#include <chrono>
#include <ctime>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
// if -pthread or -fopenmp provided only
#ifdef _REENTRANT
# define MTL_LOG_WITH_THREADS
#endif
#ifdef MTL_LOG_WITH_THREADS
# include <thread>
# include <mutex>
#endif
#ifndef MTL_LOG_NAMESPACE
# define MTL_LOG_NAMESPACE mlog
#endif
#ifndef DECLSPEC
# if defined(__WIN32__) || defined(__WINRT__)
# define DECLSPEC __declspec(dllexport)
# else
# if defined(__GNUC__) && __GNUC__ >= 4
# define DECLSPEC __attribute__((__visibility__("default")))
# elif defined(__GNUC__) && __GNUC__ >= 2
# define DECLSPEC __declspec(dllexport)
# else
# define DECLSPEC
# endif
# endif
#endif
#define MTL_LOG_VARIABLE(varname) #varname " =", varname
namespace MTL_LOG_NAMESPACE
{
# define MTL_LOG_DECLARE(funcName) \
template<typename... Args> DECLSPEC void funcName(const Args&... args);
MTL_LOG_DECLARE(info)
MTL_LOG_DECLARE(warning)
MTL_LOG_DECLARE(error)
MTL_LOG_DECLARE(fatal)
MTL_LOG_DECLARE(debug)
MTL_LOG_DECLARE(trace)
inline DECLSPEC bool loadConfiguration(const std::string& fname);
# undef MTL_LOG_DECLARE
namespace __details
{
class _Logger;
# define MTL_LOG_FRIEND(funcName) \
template<typename... Args> friend void MTL_LOG_NAMESPACE::funcName(const Args&... args);
# define __DETAILS_FRIENDSHIPS \
MTL_LOG_FRIEND(info) \
MTL_LOG_FRIEND(warning) \
MTL_LOG_FRIEND(error) \
MTL_LOG_FRIEND(fatal) \
MTL_LOG_FRIEND(debug) \
MTL_LOG_FRIEND(trace)
class __Header final
{
public:
__Header(void) = delete;
__Header(const std::string& str) : pattern(str)
{
this->build();
}
__Header& operator=(const std::string& str)
{
this->pattern = str;
this->build();
return *this;
}
~__Header(void) = default;
operator std::string(void)
{
return this->pattern;
}
void display(std::ostream& out, const std::string& type, const char *const color,
const char *const nocolor, bool colorEnabled)
{
for(const auto& p : this->chunks)
{
switch(p.first)
{
case -1:
{
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm ltm = *localtime(&tt);
this->printTwoDigits(out, ltm.tm_mon+1) << '/';
this->printTwoDigits(out, ltm.tm_mday) << '/';
out << ltm.tm_year + 1900;
break;
}
case -4:
{
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm ltm = *localtime(&tt);
this->printTwoDigits(out, ltm.tm_hour) << ':';
this->printTwoDigits(out, ltm.tm_min) << ':';
this->printTwoDigits(out, ltm.tm_sec);
break;
}
case -3:
# ifdef MTL_LOG_WITH_THREADS
out << "0x" << std::hex << std::this_thread::get_id() << std::dec;
# endif
break;
case -2:
if (colorEnabled)
{
out << color;
}
out << type;
if (colorEnabled)
{
out << nocolor;
}
break;
default:
for(int i=p.first;i<p.second;++i)
{
out << this->pattern.at(i);
}
}
}
}
private:
std::string pattern;
std::vector<std::pair<int, int>> chunks;
inline std::ostream& printTwoDigits(std::ostream& out, int value)
{
return out << ((value < 10) ? "0" : "") << value;
}
void splitAndMerge(int begin, int end, int id)
{
for(auto it=this->chunks.begin();it!=this->chunks.end();++it)
{
if (it->second > begin)
{
std::pair<int, int> mid(id, static_cast<int>(std::string::npos));
decltype(mid) last(begin+end, it->second);
it->second = begin;
it = this->chunks.insert(std::next(it), last);
this->chunks.insert(it, mid);
break;
}
}
}
void checkAndMergeIfFound(const std::string& tag, int id)
{
std::size_t pos = this->pattern.find(tag);
if (pos != std::string::npos)
{
this->splitAndMerge(static_cast<int>(pos), static_cast<int>(tag.length()), id);
}
}
void build(void)
{
this->chunks.clear();
this->chunks.push_back({0, static_cast<int>(this->pattern.size())});
this->checkAndMergeIfFound("{TYPE}", -2);
this->checkAndMergeIfFound("{DATE}", -1);
this->checkAndMergeIfFound("{THREAD}", -3);
this->checkAndMergeIfFound("{TIME}", -4);
}
};
template<typename T>
class __Static_declarer
{
protected:
static bool ENABLE_LOG;
static bool ENABLE_COLOR;
static bool ENABLE_SPACING;
static bool ENABLE_DEBUG;
static bool ENABLE_INFO;
static bool ENABLE_ERROR;
static bool ENABLE_WARNING;
static bool ENABLE_FATAL;
static bool ENABLE_TRACE;
static bool ENABLE_HEADER;
static std::ostream* OUT;
static bool ENABLE_ALPHA_BOOL;
static MTL_LOG_NAMESPACE::__details::__Header FORMAT;
private:
# ifdef MTL_LOG_WITH_THREADS
static std::mutex MUTEX;
# endif
static constexpr const char *const C_YELLOW = "\033[1;33m";
static constexpr const char *const C_RED = "\033[1;31m";
static constexpr const char *const C_GREEN = "\033[1;32m";
static constexpr const char *const C_BLUE = "\033[1;36m";
static constexpr const char *const C_BLANK = "\033[0m";
friend class MTL_LOG_NAMESPACE::__details::_Logger;
__DETAILS_FRIENDSHIPS
__Static_declarer(void) = delete;
};
# define STATIC_DECLARATION(type, name, value) \
template<typename T> type __Static_declarer<T>::name = value;
STATIC_DECLARATION(bool, ENABLE_LOG, true)
STATIC_DECLARATION(bool, ENABLE_COLOR, false)
STATIC_DECLARATION(bool, ENABLE_SPACING, true)
STATIC_DECLARATION(bool, ENABLE_DEBUG, true)
STATIC_DECLARATION(bool, ENABLE_INFO, true)
STATIC_DECLARATION(bool, ENABLE_ERROR, true)
STATIC_DECLARATION(bool, ENABLE_WARNING, true)
STATIC_DECLARATION(bool, ENABLE_FATAL, true)
STATIC_DECLARATION(bool, ENABLE_TRACE, true)
STATIC_DECLARATION(bool, ENABLE_HEADER, true)
STATIC_DECLARATION(std::ostream*, OUT, &std::cout)
STATIC_DECLARATION(bool, ENABLE_ALPHA_BOOL, true)
# ifdef MTL_LOG_WITH_THREADS
template<typename T> std::mutex __Static_declarer<T>::MUTEX;
# endif
STATIC_DECLARATION(MTL_LOG_NAMESPACE::__details::__Header, FORMAT, std::string("[{TYPE} {DATE} {TIME}] : "))
# undef STATIC_DECLARATION
}
# ifndef MTL_LOG_WITH_THREAD
# define MTL_LOG_LOCK \
do{}while(false)
# else
# define MTL_LOG_LOCK \
std::unique_lock<std::mutex> lck(MTL_LOG_NAMESPACE::Options::MUTEX)
# endif
# define MTL_LOG_GET_SET(type, name, option) \
static void enable##name(type value) noexcept\
{\
MTL_LOG_LOCK;\
MTL_LOG_NAMESPACE::Options::option = value;\
}\
static type is##name##Enabled(void) noexcept\
{\
MTL_LOG_LOCK;\
return MTL_LOG_NAMESPACE::Options::option;\
}
class DECLSPEC Options final : public MTL_LOG_NAMESPACE::__details::__Static_declarer<MTL_LOG_NAMESPACE::Options>
{
public:
Options(void) = delete;
MTL_LOG_GET_SET(bool, Log, ENABLE_LOG)
MTL_LOG_GET_SET(bool, Color, ENABLE_COLOR)
MTL_LOG_GET_SET(bool, Spacing, ENABLE_SPACING)
MTL_LOG_GET_SET(bool, AlphaBool, ENABLE_ALPHA_BOOL)
MTL_LOG_GET_SET(bool, Debug, ENABLE_DEBUG)
MTL_LOG_GET_SET(bool, Warning, ENABLE_WARNING)
MTL_LOG_GET_SET(bool, Error, ENABLE_ERROR)
MTL_LOG_GET_SET(bool, Fatal, ENABLE_FATAL)
MTL_LOG_GET_SET(bool, Info, ENABLE_INFO)
MTL_LOG_GET_SET(bool, Header, ENABLE_HEADER)
MTL_LOG_GET_SET(bool, Trace, ENABLE_TRACE)
static void setOutputStream(std::ostream* out)
{
MTL_LOG_LOCK;
MTL_LOG_NAMESPACE::Options::OUT = out;
}
static std::ostream* getOutputStream(void)
{
MTL_LOG_LOCK;
return MTL_LOG_NAMESPACE::Options::OUT;
}
static void setFormat(const std::string& format)
{
MTL_LOG_LOCK;
MTL_LOG_NAMESPACE::Options::FORMAT = format;
}
static std::string getFormat(void)
{
MTL_LOG_LOCK;
return MTL_LOG_NAMESPACE::Options::FORMAT;
}
};
# undef MTL_LOG_GET_SET
namespace __details
{
class _Logger final
{
private:
_Logger(void) = delete;
__DETAILS_FRIENDSHIPS
static void _print_(void)
{
*MTL_LOG_NAMESPACE::Options::OUT << std::endl;
}
template<typename Actual>
static void _print_(const Actual& a)
{
*MTL_LOG_NAMESPACE::Options::OUT << a << std::endl;
}
template<typename Actual, typename... Args>
static void _print_(const Actual& a, const Args&... args)
{
*MTL_LOG_NAMESPACE::Options::OUT << a;
if (MTL_LOG_NAMESPACE::Options::ENABLE_SPACING)
{
*MTL_LOG_NAMESPACE::Options::OUT << ' ';
}
MTL_LOG_NAMESPACE::__details::_Logger::_print_(args...);
}
static void assertValidity(void)
{
if (MTL_LOG_NAMESPACE::Options::OUT == nullptr)
{
throw std::ios_base::failure("OUT must not be nullptr !");
}
if (!MTL_LOG_NAMESPACE::Options::OUT->good())
{
throw std::ios_base::failure("OUT must be a \"good()\" std::ostream* !");
}
}
template<typename... Args>
static void _start_print(const char *const tag, const char *const color, const Args&... args)
{
MTL_LOG_LOCK;
MTL_LOG_NAMESPACE::__details::_Logger::assertValidity();
*MTL_LOG_NAMESPACE::Options::OUT << ((MTL_LOG_NAMESPACE::Options::isAlphaBoolEnabled()) ? std::boolalpha
: std::noboolalpha);
if (MTL_LOG_NAMESPACE::Options::isHeaderEnabled())
{
MTL_LOG_NAMESPACE::Options::FORMAT.display(*MTL_LOG_NAMESPACE::Options::OUT,
tag, color,
MTL_LOG_NAMESPACE::Options::C_BLANK,
MTL_LOG_NAMESPACE::Options::isColorEnabled());
}
MTL_LOG_NAMESPACE::__details::_Logger::_print_(args...);
}
};
}
# define MTL_LOG_METHOD(funcName, type, color, option) \
template<typename... Args>\
DECLSPEC void funcName(const Args&... args)\
{\
if (MTL_LOG_NAMESPACE::Options::isLogEnabled() && \
MTL_LOG_NAMESPACE::Options::is##option##Enabled())\
{\
MTL_LOG_NAMESPACE::__details::_Logger::_start_print(type,\
MTL_LOG_NAMESPACE::Options::color,\
args...);\
}\
}
MTL_LOG_METHOD(info, "INFO ", C_GREEN, Info)
MTL_LOG_METHOD(warning, "WARNING", C_YELLOW, Warning)
MTL_LOG_METHOD(error, "ERROR ", C_RED, Error)
MTL_LOG_METHOD(fatal, "FATAL ", C_RED, Fatal)
MTL_LOG_METHOD(debug, "DEBUG ", C_BLUE, Debug)
MTL_LOG_METHOD(trace, "TRACE ", C_BLUE, Trace)
# undef MTL_LOG_METHOD
# undef MTL_LOG_LOCK
# define MTL_LOG_DUMP_LINE(file, text) file << text << std::endl;
# define MTL_LOG_READ_BOOL(varname) \
config >> foo >> equal >> value;\
MTL_LOG_NAMESPACE::Options::enable##varname(value);
inline DECLSPEC bool loadConfiguration(const std::string& fname)
{
std::ifstream config(fname);
if (config.good())
{
std::string foo;
char equal;
bool value;
MTL_LOG_READ_BOOL(Log);
MTL_LOG_READ_BOOL(Color);
MTL_LOG_READ_BOOL(Spacing);
MTL_LOG_READ_BOOL(AlphaBool);
MTL_LOG_READ_BOOL(Info);
MTL_LOG_READ_BOOL(Warning);
MTL_LOG_READ_BOOL(Error);
MTL_LOG_READ_BOOL(Fatal);
MTL_LOG_READ_BOOL(Debug);
MTL_LOG_READ_BOOL(Trace);
MTL_LOG_READ_BOOL(Header);
config >> foo >> equal;
std::getline(config, foo);
MTL_LOG_NAMESPACE::Options::setFormat(foo);
config.close();
return true;
}
else
{
std::ofstream dump(fname);
if (dump.good())
{
MTL_LOG_DUMP_LINE(dump, "ENABLE_LOG:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_COLOR:bool = 0");
MTL_LOG_DUMP_LINE(dump, "ENABLE_SPACING:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_ALPHA_BOOL:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_INFO:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_WARNING:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_ERROR:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_FATAL:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_DEBUG:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_TRACE:bool = 1");
MTL_LOG_DUMP_LINE(dump, "ENABLE_HEADER:bool = 1");
MTL_LOG_DUMP_LINE(dump, "HEADER_FORMAT:string =[{TYPE} {DATE}] : ");
dump.close();
}
return false;
}
}
# undef MTL_LOG_DUMP_LINE
# undef MTL_LOG_READ_BOOL
# undef __DETAILS_FRIENDSHIPS
# undef MTL_LOG_FRIEND
# undef MTL_LOG_WITH_THREADS
}
#endif