TkN 2.4
Toolkit for Nuclei
Loading...
Searching...
No Matches
tklog.h
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#ifndef tklog_H
15#define tklog_H
16
17#include "tkn_config.h"
18
19#ifdef HAS_ROOT
20#include "TClass.h"
21#endif
22
23#include <iostream>
24#include <sstream>
25#include <mutex>
26
27#include "tkstring.h"
28
29namespace tkn {
30
31class tklog{
32
33 static std::recursive_mutex& get_mutex() {
34 static std::recursive_mutex mutex;
35 return mutex;
36 }
37
38 static thread_local tklog *g_log;
39
40 bool fskiplog=false;
41
43 friend tklog &do_endl(tklog &);
44 friend tklog &do_header(tklog &);
45 friend tklog &do_log(tklog &);
46 friend tklog &reset(tklog &);
47
49
50 friend tklog &skip_line(tklog &);
51
52 friend tklog &info(tklog &);
53 friend tklog &comment(tklog &);
54 friend tklog &warning(tklog &);
55 friend tklog &error(tklog &);
56
57 friend tklog &info_o(tklog &);
58 friend tklog &warning_o(tklog &);
59 friend tklog &error_o(tklog &);
60
61
63 friend tklog &info_v(tklog &);
64 friend tklog &warning_v(tklog &);
65 friend tklog &error_v(tklog &);
66
67 friend tklog &debug(tklog &log);
68 friend tklog &info_vo(tklog &);
69 friend tklog &warning_vo(tklog &);
70 friend tklog &error_vo(tklog &);
71
73
74 friend tklog &high_intensity(tklog &);
75
76 friend tklog &black(tklog &);
77 friend tklog &red(tklog &);
78 friend tklog &green(tklog &);
79 friend tklog &yellow(tklog &);
80 friend tklog &blue(tklog &);
81 friend tklog &purple(tklog &);
82 friend tklog &cyan(tklog &);
83 friend tklog &white(tklog &);
84
85 friend tklog &black_bg(tklog &);
86 friend tklog &red_bg(tklog &);
87 friend tklog &green_bg(tklog &);
88 friend tklog &yellow_bg(tklog &);
89 friend tklog &blue_bg(tklog &);
90 friend tklog &purple_bg(tklog &);
91 friend tklog &cyan_bg(tklog &);
92 friend tklog &white_bg(tklog &);
93
95 friend tklog &regular(tklog &);
96 friend tklog &bold(tklog &);
97 friend tklog &italic(tklog &);
98 friend tklog &underline(tklog &);
99 friend tklog &blink(tklog &);
100
101public:
103
104 std::ostringstream fType_message{};
105 std::ostringstream fCore_message{};
106 std::ostringstream fHeader{};
107 static const tkstring freset_font;
108
111
123
124 enum bType {
126 kBold = 1,
130 };
131
136
138 bool fprint_warnings = true;
139
140public:
142 static tklog *the_log();
143
144 tklog() {}
145 virtual ~tklog() {}
146
147 void set_class(tkstring _class_name) {fClass = _class_name;}
148 void set_method(tkstring _method_name) {fMethod = _method_name;}
149
150 void progress_bar(int _max, int _value, const tkstring &_message="");
151
152 void set_log_level(log_level _level) {flog_level = _level;}
153 void set_warnings(bool _print_warnings=true) {fprint_warnings = _print_warnings;}
154
155 // Reset colors, and clear class and method name
156 void clear() {fClass=""; fMethod="";reset();}
157
159 virtual tklog & operator<< ( tklog &(*pf)(tklog&) )
160 { (*pf)(*this); return (*this); }
161
163 template<class T> tklog &operator<<(T t) {
164 if(fskiplog) return (*this);
165 fCore_message << t;
166 return (*this);
167 }
168
169 tklog &operator<<(std::basic_ostream<char> o) {
170 if(fskiplog) return (*this);
171 std::lock_guard<std::recursive_mutex> lock(get_mutex());
172 std::cout << &o;
173 std::cout.flush();
174 return (*this);
175 }
176
177private:
178
179 void set_type(bType _type);
180 void process_color();
181 void reset();
182
183#ifdef HAS_ROOT
185 ClassDef(tklog,0);
186#endif
187};
188
189inline tklog &do_header(tklog &log) {
190 log.process_color();
191 if(!log.fskiplog) {
192 log.fType_message << log.fHeader.str() << log.fCore_message.str() << tklog::freset_font;
193 }
194 log.reset();
195 log.fCore_message.str("");log.fCore_message.clear();
196 return log;
197}
198
199inline tklog &do_log(tklog &log) {
200 log.process_color();
201 if(!log.fskiplog) {
202 std::lock_guard<std::recursive_mutex> lock(log.get_mutex());
203 std::cout << log.fType_message.str() << log.fHeader.str() << log.fCore_message.str() << tklog::freset_font;
204 std::cout.flush();
205 }
206 log.reset();
207 log.fCore_message.str("");log.fCore_message.clear();
208 log.fType_message.str("");log.fType_message.clear();
209 return log;
210}
211
212inline tklog &do_endl(tklog &log) {
213 log.process_color();
214 if(!log.fskiplog) {
215 std::lock_guard<std::recursive_mutex> lock(log.get_mutex());
216 std::cout << log.fType_message.str() << log.fHeader.str() << log.fCore_message.str() << tklog::freset_font;
217 std::cout<<std::endl;
218 std::cout.flush();
219 }
220 log.reset();
221 log.fCore_message.str("");log.fCore_message.clear();
222 log.fType_message.str("");log.fType_message.clear();
223 return log;
224}
225
226inline tklog &reset(tklog &log) {
227 log.reset(); return log;
228}
229
231
233 log.fHigh_intensity_colors = true; return log; }
234
235inline tklog &black(tklog &log) {
236 log.fFG_color = tklog::kBlack; return log; }
237
238inline tklog &red(tklog &log) {
239 log.fFG_color = tklog::kRed; return log; }
240
241inline tklog &green(tklog &log) {
242 log.fFG_color = tklog::kGreen; return log; }
243
244inline tklog &yellow(tklog &log) {
245 log.fFG_color = tklog::kYellow; return log; }
246
247inline tklog &blue(tklog &log) {
248 log.fFG_color = tklog::kBlue; return log; }
249
250inline tklog &purple(tklog &log) {
251 log.fFG_color = tklog::kPurple; return log; }
252
253inline tklog &cyan(tklog &log) {
254 log.fFG_color = tklog::kCyan; return log; }
255
256inline tklog &white(tklog &log) {
257 log.fFG_color = tklog::kWhite; return log; }
258
259
260inline tklog &black_bg(tklog &log) {
261 log.fBG_color = tklog::kBlack; return log; }
262
263inline tklog &red_bg(tklog &log) {
264 log.fBG_color = tklog::kRed; return log; }
265
266inline tklog &green_bg(tklog &log) {
267 log.fBG_color = tklog::kGreen; return log; }
268
269inline tklog &yellow_bg(tklog &log) {
270 log.fBG_color = tklog::kYellow; return log; }
271
272inline tklog &blue_bg(tklog &log) {
273 log.fBG_color = tklog::kBlue; return log; }
274
275inline tklog &purple_bg(tklog &log) {
276 log.fBG_color = tklog::kPurple; return log; }
277
278inline tklog &cyan_bg(tklog &log) {
279 log.fBG_color = tklog::kCyan; return log; }
280
281inline tklog &white_bg(tklog &log) {
282 log.fBG_color = tklog::kWhite; return log; }
283
285
286inline tklog &regular(tklog &log) {
287 log.set_type(tklog::kRegular); return log; }
288
289inline tklog &bold(tklog &log) {
290 log.set_type(tklog::kBold); return log; }
291
292inline tklog &italic(tklog &log) {
293 log.set_type(tklog::kItalic); return log; }
294
295inline tklog &underline(tklog &log) {
296 log.set_type(tklog::kUnderline); return log; }
297
298inline tklog &blink(tklog &log) {
299 log.set_type(tklog::kBlink); return log; }
300
302
303inline tklog &skip_line(tklog &log) {
304 log << "\n" << " ";
305 return log;
306}
307
308inline tklog &info_o(tklog &log) {
309 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
310 log << high_intensity << white << blue_bg << underline << italic << bold << "\u2713 INFO :" << do_header << " " << blue;
311 return log;
312}
313inline tklog &info(tklog &log) {
314 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
315 log << high_intensity << blue << bold << "[ INFO ]" << do_header << " ";// << blue;
316 return log;
317}
318
319inline tklog &comment(tklog &log) {
320 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
321 log << high_intensity << green << bold <<"[ COMMENT ]" << do_header << " ";
322 return log;
323}
324
325inline tklog &warning_o(tklog &log) {
326 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
327 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
328 log << high_intensity << yellow_bg << underline << italic << bold << "\u26A0 WARNING :" << do_header << " " << yellow;
329 return log;
330}
331inline tklog &warning(tklog &log) {
332 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
333 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
334 log << high_intensity << yellow << bold << "[ WARNING ]" << do_header << " ";// << yellow;
335 return log;
336}
337
338inline tklog &error_o(tklog &log) {
339 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
340 log << high_intensity << white << red_bg << underline << italic << bold << "\u2716 ERROR :" << do_header << " " << red;
341 return log;
342}
343
344inline tklog &error(tklog &log) {
345 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
346 log << high_intensity << red << bold << "[ ERROR ]" << do_header << " ";// << red;
347 return log;
348}
349
350inline tklog &debug(tklog &log) {
351 log << high_intensity << green << bold << "[ DEBUG ]" << do_header << " ";
352 return log;
353}
354
355inline tklog &info_vo(tklog &log) {
356 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
357 log << high_intensity << white << blue_bg << underline << italic << bold << "\u2713 INFO :" << do_header << " " << blue;
358 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
359 log << underline << log.fClass << "::" << log.fMethod << do_header;
360 }
361 log << "\n";
362 return log;
363}
364
365inline tklog &info_v(tklog &log) {
366 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
367 log << high_intensity << blue << bold << "[ INFO ]" << do_header << " ";// << blue;
368 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
369 log << log.fClass << "::" << log.fMethod << do_header;
370 }
371 log << "\n ";// << blue;
372 return log;
373}
374
375inline tklog &warning_vo(tklog &log) {
376 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
377 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
378 log << high_intensity << yellow_bg << underline << italic << bold << "\u26A0 WARNING:" << do_header << " " << yellow;
379 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
380 log << underline << log.fClass << "::" << log.fMethod << do_header;
381 }
382 log << "\n";
383 return log;
384}
385
386inline tklog &warning_v(tklog &log) {
387 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
388 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
389 log << high_intensity << yellow << bold << "[ WARNING ]" << do_header << " ";// << yellow;
390 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
391 log << log.fClass << "::" << log.fMethod << do_header;
392 }
393 log << "\n ";// << yellow;
394 return log;
395}
396
397inline tklog &error_vo(tklog &log) {
398 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
399 log << high_intensity << white << red_bg << underline << italic << bold << "\u2716 ERROR :" << do_header << " " << red;
400 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
401 log << underline << log.fClass << "::" << log.fMethod << do_header;
402 }
403 log << "\n";
404 return log;
405}
406
407inline tklog &error_v(tklog &log) {
408 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
409 log << high_intensity << red << bold << "[ ERROR ]" << do_header << " ";// << red;
410 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
411 log << log.fClass << "::" << log.fMethod << do_header;
412 }
413 log << "\n ";// << red;
414 return log;
415}
416
417}
418
419#define glog (*tkn::tklog::the_log())
420
421#ifdef HAS_DEBUG
422#define gdebug glog << debug
423#else
424#define gdebug if(0) glog << debug
425#endif
426
427#endif
Classe used to print debugs, infos, warnings and errors into the terminal.
Definition tklog.h:31
friend tklog & bold(tklog &)
Definition tklog.h:289
virtual ~tklog()
Definition tklog.h:145
@ kPurple
Definition tklog.h:118
@ kDefault_color
Definition tklog.h:121
@ kYellow
Definition tklog.h:116
friend tklog & underline(tklog &)
Definition tklog.h:295
bool fprint_warnings
Definition tklog.h:138
friend tklog & do_log(tklog &)
To print the current message, then reset the log.
Definition tklog.h:199
void progress_bar(int _max, int _value, const tkstring &_message="")
Definition tklog.cpp:41
virtual tklog & operator<<(tklog &(*pf)(tklog &))
To allow manipulators.
Definition tklog.h:159
log_level flog_level
Definition tklog.h:137
tklog & operator<<(T t)
send any request to the underlying ostringstream
Definition tklog.h:163
friend tklog & blink(tklog &)
Definition tklog.h:298
friend tklog & error_o(tklog &)
Definition tklog.h:338
friend tklog & green_bg(tklog &)
Definition tklog.h:266
friend tklog & yellow_bg(tklog &)
Definition tklog.h:269
friend tklog & black_bg(tklog &)
Definition tklog.h:260
friend tklog & white(tklog &)
Definition tklog.h:256
void set_warnings(bool _print_warnings=true)
Definition tklog.h:153
friend tklog & yellow(tklog &)
Definition tklog.h:244
friend tklog & white_bg(tklog &)
Definition tklog.h:281
void set_class(tkstring _class_name)
Definition tklog.h:147
friend tklog & error_v(tklog &)
Definition tklog.h:407
friend tklog & regular(tklog &)
Attributes.
Definition tklog.h:286
friend tklog & warning_o(tklog &)
Definition tklog.h:325
friend tklog & info_vo(tklog &)
Definition tklog.h:355
friend tklog & cyan(tklog &)
Definition tklog.h:253
friend tklog & info_o(tklog &)
Definition tklog.h:308
friend tklog & debug(tklog &log)
Definition tklog.h:350
friend tklog & reset(tklog &)
To print the current message, then reset the log.
Definition tklog.h:226
std::ostringstream fCore_message
Contains the type string to be printed with the current style.
Definition tklog.h:105
tkstring fTypes
Definition tklog.h:134
friend tklog & info(tklog &)
Definition tklog.h:313
friend tklog & red(tklog &)
Definition tklog.h:238
friend tklog & purple_bg(tklog &)
Definition tklog.h:275
@ kRegular
Definition tklog.h:125
@ kItalic
Definition tklog.h:127
@ kUnderline
Definition tklog.h:128
friend tklog & skip_line(tklog &)
Reset the log style.
Definition tklog.h:303
std::ostringstream fType_message
Definition tklog.h:104
bColor fFG_color
Definition tklog.h:132
bool fHigh_intensity_colors
Definition tklog.h:135
void set_method(tkstring _method_name)
Definition tklog.h:148
tkstring fClass
To put back the standard font in std::cout.
Definition tklog.h:109
tkstring fMethod
Definition tklog.h:110
friend tklog & warning_v(tklog &)
Definition tklog.h:386
friend tklog & do_header(tklog &)
Reimplementation of std::endl.
Definition tklog.h:189
friend tklog & error_vo(tklog &)
Definition tklog.h:397
friend tklog & purple(tklog &)
Definition tklog.h:250
tklog & operator<<(std::basic_ostream< char > o)
Definition tklog.h:169
friend tklog & error(tklog &)
Definition tklog.h:344
static tklog * the_log()
glog is a singleton used for fancy prints in the terminal
Definition tklog.cpp:32
friend tklog & blue_bg(tklog &)
Definition tklog.h:272
friend tklog & green(tklog &)
Definition tklog.h:241
friend tklog & info_v(tklog &)
Predefined macros with verbose option (print the name of the class and method)
Definition tklog.h:365
static const tkstring freset_font
Contains the style (color + type)
Definition tklog.h:107
friend tklog & italic(tklog &)
Definition tklog.h:292
friend tklog & warning_vo(tklog &)
Definition tklog.h:375
bColor fBG_color
Definition tklog.h:133
friend tklog & black(tklog &)
Definition tklog.h:235
friend tklog & do_endl(tklog &)
General.
Definition tklog.h:212
void clear()
Definition tklog.h:156
@ kwarning
Definition tklog.h:102
friend tklog & warning(tklog &)
Definition tklog.h:331
std::ostringstream fHeader
Contains the core message string to be printed with the current style.
Definition tklog.h:106
friend tklog & red_bg(tklog &)
Definition tklog.h:263
friend tklog & comment(tklog &)
Definition tklog.h:319
friend tklog & blue(tklog &)
Definition tklog.h:247
friend tklog & cyan_bg(tklog &)
Definition tklog.h:278
void set_log_level(log_level _level)
Definition tklog.h:152
friend tklog & high_intensity(tklog &)
Colors.
Definition tklog.h:232
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:31
bool is_empty() const
Definition tkstring.h:140
Definition tklog.cpp:16
tklog & bold(tklog &log)
Definition tklog.h:289
tklog & underline(tklog &log)
Definition tklog.h:295
tklog & do_log(tklog &log)
Definition tklog.h:199
tklog & blink(tklog &log)
Definition tklog.h:298
tklog & error_o(tklog &log)
Definition tklog.h:338
tklog & green_bg(tklog &log)
Definition tklog.h:266
tklog & yellow_bg(tklog &log)
Definition tklog.h:269
tklog & black_bg(tklog &log)
Definition tklog.h:260
tklog & white(tklog &log)
Definition tklog.h:256
tklog & yellow(tklog &log)
Definition tklog.h:244
tklog & white_bg(tklog &log)
Definition tklog.h:281
tklog & error_v(tklog &log)
Definition tklog.h:407
tklog & regular(tklog &log)
Types.
Definition tklog.h:286
tklog & warning_o(tklog &log)
Definition tklog.h:325
tklog & info_vo(tklog &log)
Definition tklog.h:355
tklog & cyan(tklog &log)
Definition tklog.h:253
tklog & info_o(tklog &log)
Definition tklog.h:308
tklog & debug(tklog &log)
Definition tklog.h:350
tklog & reset(tklog &log)
Definition tklog.h:226
tklog & info(tklog &log)
Definition tklog.h:313
tklog & red(tklog &log)
Definition tklog.h:238
tklog & purple_bg(tklog &log)
Definition tklog.h:275
tklog & skip_line(tklog &log)
Predefined macros.
Definition tklog.h:303
tklog & warning_v(tklog &log)
Definition tklog.h:386
tklog & do_header(tklog &log)
Definition tklog.h:189
tklog & error_vo(tklog &log)
Definition tklog.h:397
tklog & purple(tklog &log)
Definition tklog.h:250
tklog & error(tklog &log)
Definition tklog.h:344
tklog & blue_bg(tklog &log)
Definition tklog.h:272
tklog & green(tklog &log)
Definition tklog.h:241
tklog & info_v(tklog &log)
Definition tklog.h:365
tklog & italic(tklog &log)
Definition tklog.h:292
tklog & warning_vo(tklog &log)
Definition tklog.h:375
tklog & black(tklog &log)
Definition tklog.h:235
tklog & do_endl(tklog &log)
Definition tklog.h:212
tklog & warning(tklog &log)
Definition tklog.h:331
tklog & red_bg(tklog &log)
Definition tklog.h:263
tklog & comment(tklog &log)
Definition tklog.h:319
tklog & blue(tklog &log)
Definition tklog.h:247
tklog & cyan_bg(tklog &log)
Definition tklog.h:278
tklog & high_intensity(tklog &log)
Colors.
Definition tklog.h:232