TkN 2.1
Toolkit for Nuclei
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 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#ifndef tklog_H
38#define tklog_H
39
40#include "tkn_config.h"
41
42#ifdef HAS_ROOT
43#include "TClass.h"
44#endif
45
46#include <iostream>
47#include <sstream>
48#include <mutex>
49
50#include "tkstring.h"
51
52namespace tkn {
53
54class tklog{
55
56 static std::recursive_mutex& get_mutex() {
57 static std::recursive_mutex mutex;
58 return mutex;
59 }
60
61 static thread_local tklog *g_log;
62
63 bool fskiplog=false;
64
66 friend tklog &do_endl(tklog &);
67 friend tklog &do_header(tklog &);
68 friend tklog &do_log(tklog &);
69 friend tklog &reset(tklog &);
70
72
73 friend tklog &skip_line(tklog &);
74
75 friend tklog &info(tklog &);
76 friend tklog &comment(tklog &);
77 friend tklog &warning(tklog &);
78 friend tklog &error(tklog &);
79
80 friend tklog &info_o(tklog &);
81 friend tklog &warning_o(tklog &);
82 friend tklog &error_o(tklog &);
83
84
86 friend tklog &info_v(tklog &);
87 friend tklog &warning_v(tklog &);
88 friend tklog &error_v(tklog &);
89
90 friend tklog &debug(tklog &log);
91 friend tklog &info_vo(tklog &);
92 friend tklog &warning_vo(tklog &);
93 friend tklog &error_vo(tklog &);
94
96
97 friend tklog &high_intensity(tklog &);
98
99 friend tklog &black(tklog &);
100 friend tklog &red(tklog &);
101 friend tklog &green(tklog &);
102 friend tklog &yellow(tklog &);
103 friend tklog &blue(tklog &);
104 friend tklog &purple(tklog &);
105 friend tklog &cyan(tklog &);
106 friend tklog &white(tklog &);
107
108 friend tklog &black_bg(tklog &);
109 friend tklog &red_bg(tklog &);
110 friend tklog &green_bg(tklog &);
111 friend tklog &yellow_bg(tklog &);
112 friend tklog &blue_bg(tklog &);
113 friend tklog &purple_bg(tklog &);
114 friend tklog &cyan_bg(tklog &);
115 friend tklog &white_bg(tklog &);
116
118 friend tklog &regular(tklog &);
119 friend tklog &bold(tklog &);
120 friend tklog &italic(tklog &);
121 friend tklog &underline(tklog &);
122 friend tklog &blink(tklog &);
123
124public:
126
127 std::ostringstream fType_message{};
128 std::ostringstream fCore_message{};
129 std::ostringstream fHeader{};
130 static const tkstring freset_font;
131
134
135 enum bColor {
137 kRed = 1,
140 kBlue = 4,
142 kCyan = 6,
145 };
146
147 enum bType {
149 kBold = 1,
153 };
154
159
161 bool fprint_warnings = true;
162
163public:
165 static tklog *the_log();
166
167 tklog() {}
168 virtual ~tklog() {}
169
170 void set_class(tkstring _class_name) {fClass = _class_name;}
171 void set_method(tkstring _method_name) {fMethod = _method_name;}
172
173 void progress_bar(int _max, int _value, const tkstring &_message="");
174
175 void set_log_level(log_level _level) {flog_level = _level;}
176 void set_warnings(bool _print_warnings=true) {fprint_warnings = _print_warnings;}
177
178 // Reset colors, and clear class and method name
179 void clear() {fClass=""; fMethod="";reset();}
180
182 virtual tklog & operator<< ( tklog &(*pf)(tklog&) )
183 { (*pf)(*this); return (*this); }
184
186 template<class T> tklog &operator<<(T t) {
187 if(fskiplog) return (*this);
188 fCore_message << t;
189 return (*this);
190 }
191
192 tklog &operator<<(std::basic_ostream<char> o) {
193 if(fskiplog) return (*this);
194 std::lock_guard<std::recursive_mutex> lock(get_mutex());
195 std::cout << &o;
196 std::cout.flush();
197 return (*this);
198 }
199
200private:
201
202 void set_type(bType _type);
203 void process_color();
204 void reset();
205
206#ifdef HAS_ROOT
208 ClassDef(tklog,0);
209#endif
210};
211
212inline tklog &do_header(tklog &log) {
213 log.process_color();
214 if(!log.fskiplog) {
215 log.fType_message << log.fHeader.str() << log.fCore_message.str() << tklog::freset_font;
216 }
217 log.reset();
218 log.fCore_message.str("");log.fCore_message.clear();
219 return log;
220}
221
222inline tklog &do_log(tklog &log) {
223 log.process_color();
224 if(!log.fskiplog) {
225 std::lock_guard<std::recursive_mutex> lock(log.get_mutex());
226 std::cout << log.fType_message.str() << log.fHeader.str() << log.fCore_message.str() << tklog::freset_font;
227 std::cout.flush();
228 }
229 log.reset();
230 log.fCore_message.str("");log.fCore_message.clear();
231 log.fType_message.str("");log.fType_message.clear();
232 return log;
233}
234
235inline tklog &do_endl(tklog &log) {
236 log.process_color();
237 if(!log.fskiplog) {
238 std::lock_guard<std::recursive_mutex> lock(log.get_mutex());
239 std::cout << log.fType_message.str() << log.fHeader.str() << log.fCore_message.str() << tklog::freset_font;
240 std::cout<<std::endl;
241 std::cout.flush();
242 }
243 log.reset();
244 log.fCore_message.str("");log.fCore_message.clear();
245 log.fType_message.str("");log.fType_message.clear();
246 return log;
247}
248
249inline tklog &reset(tklog &log) {
250 log.reset(); return log;
251}
252
254
256 log.fHigh_intensity_colors = true; return log; }
257
258inline tklog &black(tklog &log) {
259 log.fFG_color = tklog::kBlack; return log; }
260
261inline tklog &red(tklog &log) {
262 log.fFG_color = tklog::kRed; return log; }
263
264inline tklog &green(tklog &log) {
265 log.fFG_color = tklog::kGreen; return log; }
266
267inline tklog &yellow(tklog &log) {
268 log.fFG_color = tklog::kYellow; return log; }
269
270inline tklog &blue(tklog &log) {
271 log.fFG_color = tklog::kBlue; return log; }
272
273inline tklog &purple(tklog &log) {
274 log.fFG_color = tklog::kPurple; return log; }
275
276inline tklog &cyan(tklog &log) {
277 log.fFG_color = tklog::kCyan; return log; }
278
279inline tklog &white(tklog &log) {
280 log.fFG_color = tklog::kWhite; return log; }
281
282
283inline tklog &black_bg(tklog &log) {
284 log.fBG_color = tklog::kBlack; return log; }
285
286inline tklog &red_bg(tklog &log) {
287 log.fBG_color = tklog::kRed; return log; }
288
289inline tklog &green_bg(tklog &log) {
290 log.fBG_color = tklog::kGreen; return log; }
291
292inline tklog &yellow_bg(tklog &log) {
293 log.fBG_color = tklog::kYellow; return log; }
294
295inline tklog &blue_bg(tklog &log) {
296 log.fBG_color = tklog::kBlue; return log; }
297
298inline tklog &purple_bg(tklog &log) {
299 log.fBG_color = tklog::kPurple; return log; }
300
301inline tklog &cyan_bg(tklog &log) {
302 log.fBG_color = tklog::kCyan; return log; }
303
304inline tklog &white_bg(tklog &log) {
305 log.fBG_color = tklog::kWhite; return log; }
306
308
309inline tklog &regular(tklog &log) {
310 log.set_type(tklog::kRegular); return log; }
311
312inline tklog &bold(tklog &log) {
313 log.set_type(tklog::kBold); return log; }
314
315inline tklog &italic(tklog &log) {
316 log.set_type(tklog::kItalic); return log; }
317
318inline tklog &underline(tklog &log) {
319 log.set_type(tklog::kUnderline); return log; }
320
321inline tklog &blink(tklog &log) {
322 log.set_type(tklog::kBlink); return log; }
323
325
326inline tklog &skip_line(tklog &log) {
327 log << "\n" << " ";
328 return log;
329}
330
331inline tklog &info_o(tklog &log) {
332 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
333 log << high_intensity << white << blue_bg << underline << italic << bold << "\u2713 INFO :" << do_header << " " << blue;
334 return log;
335}
336inline tklog &info(tklog &log) {
337 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
338 log << high_intensity << blue << bold << "[ INFO ]" << do_header << " ";// << blue;
339 return log;
340}
341
342inline tklog &comment(tklog &log) {
343 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
344 log << high_intensity << green << bold <<"[ COMMENT ]" << do_header << " ";
345 return log;
346}
347
348inline tklog &warning_o(tklog &log) {
349 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
350 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
351 log << high_intensity << yellow_bg << underline << italic << bold << "\u26A0 WARNING :" << do_header << " " << yellow;
352 return log;
353}
354inline tklog &warning(tklog &log) {
355 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
356 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
357 log << high_intensity << yellow << bold << "[ WARNING ]" << do_header << " ";// << yellow;
358 return log;
359}
360
361inline tklog &error_o(tklog &log) {
362 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
363 log << high_intensity << white << red_bg << underline << italic << bold << "\u2716 ERROR :" << do_header << " " << red;
364 return log;
365}
366
367inline tklog &error(tklog &log) {
368 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
369 log << high_intensity << red << bold << "[ ERROR ]" << do_header << " ";// << red;
370 return log;
371}
372
373inline tklog &debug(tklog &log) {
374 log << high_intensity << green << bold << "[ DEBUG ]" << do_header << " ";
375 return log;
376}
377
378inline tklog &info_vo(tklog &log) {
379 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
380 log << high_intensity << white << blue_bg << underline << italic << bold << "\u2713 INFO :" << do_header << " " << blue;
381 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
382 log << underline << log.fClass << "::" << log.fMethod << do_header;
383 }
384 log << "\n";
385 return log;
386}
387
388inline tklog &info_v(tklog &log) {
389 if(log.flog_level>tklog::kinfo) {log.fskiplog = true; return log;}
390 log << high_intensity << blue << bold << "[ INFO ]" << do_header << " ";// << blue;
391 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
392 log << log.fClass << "::" << log.fMethod << do_header;
393 }
394 log << "\n ";// << blue;
395 return log;
396}
397
398inline tklog &warning_vo(tklog &log) {
399 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
400 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
401 log << high_intensity << yellow_bg << underline << italic << bold << "\u26A0 WARNING:" << do_header << " " << yellow;
402 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
403 log << underline << log.fClass << "::" << log.fMethod << do_header;
404 }
405 log << "\n";
406 return log;
407}
408
409inline tklog &warning_v(tklog &log) {
410 if(!log.fprint_warnings) {log.fskiplog = true; return log;}
411 if(log.flog_level>tklog::kwarning) {log.fskiplog = true; return log;}
412 log << high_intensity << yellow << bold << "[ WARNING ]" << do_header << " ";// << yellow;
413 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
414 log << log.fClass << "::" << log.fMethod << do_header;
415 }
416 log << "\n ";// << yellow;
417 return log;
418}
419
420inline tklog &error_vo(tklog &log) {
421 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
422 log << high_intensity << white << red_bg << underline << italic << bold << "\u2716 ERROR :" << do_header << " " << red;
423 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
424 log << underline << log.fClass << "::" << log.fMethod << do_header;
425 }
426 log << "\n";
427 return log;
428}
429
430inline tklog &error_v(tklog &log) {
431 if(log.flog_level>tklog::kerror) {log.fskiplog = true; return log;}
432 log << high_intensity << red << bold << "[ ERROR ]" << do_header << " ";// << red;
433 if(!log.fClass.is_empty() && !log.fMethod.is_empty()) {
434 log << log.fClass << "::" << log.fMethod << do_header;
435 }
436 log << "\n ";// << red;
437 return log;
438}
439
440}
441
442#define glog (*tkn::tklog::the_log())
443
444#ifdef HAS_DEBUG
445#define gdebug glog << debug
446#else
447#define gdebug if(0) glog << debug
448#endif
449
450#endif
Classe used to print debugs, infos, warnings and errors into the terminal.
Definition: tklog.h:54
virtual ~tklog()
Definition: tklog.h:168
friend tklog & error_vo(tklog &)
Definition: tklog.h:420
friend tklog & info_o(tklog &)
Definition: tklog.h:331
@ kPurple
Definition: tklog.h:141
@ kRed
Definition: tklog.h:137
@ kBlack
Definition: tklog.h:136
@ kGreen
Definition: tklog.h:138
@ kDefault_color
Definition: tklog.h:144
@ kWhite
Definition: tklog.h:143
@ kCyan
Definition: tklog.h:142
@ kBlue
Definition: tklog.h:140
@ kYellow
Definition: tklog.h:139
bool fprint_warnings
Definition: tklog.h:161
tklog()
Definition: tklog.h:167
void progress_bar(int _max, int _value, const tkstring &_message="")
Definition: tklog.cpp:64
friend tklog & regular(tklog &)
Attributes.
Definition: tklog.h:309
virtual tklog & operator<<(tklog &(*pf)(tklog &))
To allow manipulators.
Definition: tklog.h:182
log_level flog_level
Definition: tklog.h:160
tklog & operator<<(T t)
send any request to the underlying ostringstream
Definition: tklog.h:186
friend tklog & purple_bg(tklog &)
Definition: tklog.h:298
friend tklog & purple(tklog &)
Definition: tklog.h:273
friend tklog & reset(tklog &)
To print the current message, then reset the log.
Definition: tklog.h:249
friend tklog & cyan_bg(tklog &)
Definition: tklog.h:301
friend tklog & underline(tklog &)
Definition: tklog.h:318
void set_warnings(bool _print_warnings=true)
Definition: tklog.h:176
friend tklog & error_o(tklog &)
Definition: tklog.h:361
void set_class(tkstring _class_name)
Definition: tklog.h:170
friend tklog & do_log(tklog &)
To print the current message, then reset the log.
Definition: tklog.h:222
friend tklog & skip_line(tklog &)
Reset the log style.
Definition: tklog.h:326
friend tklog & info(tklog &)
Definition: tklog.h:336
friend tklog & debug(tklog &log)
Definition: tklog.h:373
friend tklog & yellow(tklog &)
Definition: tklog.h:267
friend tklog & comment(tklog &)
Definition: tklog.h:342
std::ostringstream fCore_message
Contains the type string to be printed with the current style.
Definition: tklog.h:128
tkstring fTypes
Definition: tklog.h:157
friend tklog & blue_bg(tklog &)
Definition: tklog.h:295
friend tklog & blue(tklog &)
Definition: tklog.h:270
friend tklog & cyan(tklog &)
Definition: tklog.h:276
friend tklog & white(tklog &)
Definition: tklog.h:279
friend tklog & warning_o(tklog &)
Definition: tklog.h:348
friend tklog & error_v(tklog &)
Definition: tklog.h:430
friend tklog & high_intensity(tklog &)
Colors.
Definition: tklog.h:255
friend tklog & black(tklog &)
Definition: tklog.h:258
@ kRegular
Definition: tklog.h:148
@ kItalic
Definition: tklog.h:150
@ kUnderline
Definition: tklog.h:151
@ kBold
Definition: tklog.h:149
@ kBlink
Definition: tklog.h:152
friend tklog & warning(tklog &)
Definition: tklog.h:354
friend tklog & green(tklog &)
Definition: tklog.h:264
friend tklog & red(tklog &)
Definition: tklog.h:261
friend tklog & black_bg(tklog &)
Definition: tklog.h:283
std::ostringstream fType_message
Definition: tklog.h:127
bColor fFG_color
Definition: tklog.h:155
friend tklog & do_endl(tklog &)
General.
Definition: tklog.h:235
bool fHigh_intensity_colors
Definition: tklog.h:158
void set_method(tkstring _method_name)
Definition: tklog.h:171
tkstring fClass
To put back the standard font in std::cout.
Definition: tklog.h:132
friend tklog & info_v(tklog &)
Predefined macros with verbose option (print the name of the class and method)
Definition: tklog.h:388
friend tklog & green_bg(tklog &)
Definition: tklog.h:289
tkstring fMethod
Definition: tklog.h:133
tklog & operator<<(std::basic_ostream< char > o)
Definition: tklog.h:192
friend tklog & italic(tklog &)
Definition: tklog.h:315
friend tklog & white_bg(tklog &)
Definition: tklog.h:304
static tklog * the_log()
glog is a singleton used for fancy prints in the terminal
Definition: tklog.cpp:55
friend tklog & yellow_bg(tklog &)
Definition: tklog.h:292
static const tkstring freset_font
Contains the style (color + type)
Definition: tklog.h:130
bColor fBG_color
Definition: tklog.h:156
friend tklog & blink(tklog &)
Definition: tklog.h:321
friend tklog & bold(tklog &)
Definition: tklog.h:312
void clear()
Definition: tklog.h:179
@ kinfo
Definition: tklog.h:125
@ kwarning
Definition: tklog.h:125
@ knone
Definition: tklog.h:125
@ kerror
Definition: tklog.h:125
friend tklog & red_bg(tklog &)
Definition: tklog.h:286
friend tklog & error(tklog &)
Definition: tklog.h:367
friend tklog & do_header(tklog &)
Reimplementation of std::endl.
Definition: tklog.h:212
std::ostringstream fHeader
Contains the core message string to be printed with the current style.
Definition: tklog.h:129
friend tklog & warning_v(tklog &)
Definition: tklog.h:409
friend tklog & warning_vo(tklog &)
Definition: tklog.h:398
friend tklog & info_vo(tklog &)
Definition: tklog.h:378
void set_log_level(log_level _level)
Definition: tklog.h:175
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
bool is_empty() const
Definition: tkstring.h:163
Definition: tklog.cpp:39
tklog & bold(tklog &log)
Definition: tklog.h:312
tklog & underline(tklog &log)
Definition: tklog.h:318
tklog & do_log(tklog &log)
Definition: tklog.h:222
tklog & blink(tklog &log)
Definition: tklog.h:321
tklog & error_o(tklog &log)
Definition: tklog.h:361
tklog & green_bg(tklog &log)
Definition: tklog.h:289
tklog & yellow_bg(tklog &log)
Definition: tklog.h:292
tklog & black_bg(tklog &log)
Definition: tklog.h:283
tklog & white(tklog &log)
Definition: tklog.h:279
tklog & yellow(tklog &log)
Definition: tklog.h:267
tklog & white_bg(tklog &log)
Definition: tklog.h:304
tklog & error_v(tklog &log)
Definition: tklog.h:430
tklog & regular(tklog &log)
Types.
Definition: tklog.h:309
tklog & warning_o(tklog &log)
Definition: tklog.h:348
tklog & info_vo(tklog &log)
Definition: tklog.h:378
tklog & cyan(tklog &log)
Definition: tklog.h:276
tklog & info_o(tklog &log)
Definition: tklog.h:331
tklog & debug(tklog &log)
Definition: tklog.h:373
tklog & reset(tklog &log)
Definition: tklog.h:249
tklog & info(tklog &log)
Definition: tklog.h:336
tklog & red(tklog &log)
Definition: tklog.h:261
tklog & purple_bg(tklog &log)
Definition: tklog.h:298
tklog & skip_line(tklog &log)
Predefined macros.
Definition: tklog.h:326
tklog & warning_v(tklog &log)
Definition: tklog.h:409
tklog & do_header(tklog &log)
Definition: tklog.h:212
tklog & error_vo(tklog &log)
Definition: tklog.h:420
tklog & purple(tklog &log)
Definition: tklog.h:273
tklog & error(tklog &log)
Definition: tklog.h:367
tklog & blue_bg(tklog &log)
Definition: tklog.h:295
tklog & green(tklog &log)
Definition: tklog.h:264
tklog & info_v(tklog &log)
Definition: tklog.h:388
tklog & italic(tklog &log)
Definition: tklog.h:315
tklog & warning_vo(tklog &log)
Definition: tklog.h:398
tklog & black(tklog &log)
Definition: tklog.h:258
tklog & do_endl(tklog &log)
Definition: tklog.h:235
tklog & warning(tklog &log)
Definition: tklog.h:354
tklog & red_bg(tklog &log)
Definition: tklog.h:286
tklog & comment(tklog &log)
Definition: tklog.h:342
tklog & blue(tklog &log)
Definition: tklog.h:270
tklog & cyan_bg(tklog &log)
Definition: tklog.h:301
tklog & high_intensity(tklog &log)
Colors.
Definition: tklog.h:255