SEVN
Loading...
Searching...
No Matches
sevnlog.h
Go to the documentation of this file.
1//
2// Created by Giuliano on 29/11/19.
3// Header to store logging functions and definitions (mostly for debug)
4//
5
6#ifndef SEVN_SEVNLOG_H
7#define SEVN_SEVNLOG_H
8
9#ifdef _OPENMP
10#include <omp.h>
11#endif
12
13#include <string>
14#include <sstream>
15#include <iostream>
16#include "errhand.h"
17
18
19//TODO Remove this and correctly use SevnLogging
20//GI291119: Define the DEBUG_LOG functions to print the Debug message only if enable in the compilation
21#ifdef DEBUG
22#define DEBUG_LOG(str) do { std::cout << "DEBUG: FILE::" << __FILE__ << " LINE::" <<__LINE__ << std::endl << " -> " << str << " <- " << std::endl; } while (false)
23#else
24#define DEBUG_LOG(str) do { } while (false)
25#endif
26
27//TODO Add the possibility to directly flush the output in some log file(s).
28//TODO It is really thread safe?
29namespace sevnstd{
30
31 class sevnerr;
32
44
45 public:
46
51
52 //TODO Is ok for a single instance to modify the log_level. Is maybe better to modify only a local log_level and the use this in the various log function?
57 SevnLogging(int level) {
58 set_level(level);
59 }
64
73 void log(int level, std::string errstate, const char *file_input = nullptr, int line_input = -1, int stop = 0) const;
74
81 void debug(std::string errstate, const char *file_input = nullptr, int line_input = -1) const;
82
83
90 void info(std::string errstate, const char *file_input = nullptr, int line_input = -1) const;
91
92
99 void warning(std::string errstate, const char *file_input = nullptr, int line_input = -1) const;
100
108 void error(std::string errstate, const char *file_input = nullptr, int line_input = -1, bool stop = true) const;
109
119 template<class E>
120 void error(std::string errstate, const char *file_input = nullptr, int line_input = -1, bool stop = true, E&& err= nullptr) const{
121
122 #ifdef _OPENMP
123 int num_thread=omp_get_thread_num();
124 #else
125 int num_thread=0;
126 #endif
127
128 std::ostringstream oss;
129 oss << " LOG::ERROR (Thread " << num_thread << "): " << std::endl;
130 oss << " Message : " << errstate << std::endl;
131 if (file_input)
132 oss << " From file: " << std::string(file_input) << std::endl;
133 if (line_input >= 0)
134 oss << " From line: " << line_input << std::endl;
135 std::string err_mess=oss.str();
136 if (stop)
137 throw err.istance(err_mess);
138 else
139 std::cerr << oss.str();
140
141#ifdef _OPENMP
142#pragma omp atomic
143#endif
144 count_error++;
145 }
146
147
155 void critical(std::string errstate, const char *file_input = nullptr, int line_input = -1) const;
156
157
166 template<class E>
167 void critical(std::string errstate, const char *file_input = nullptr, int line_input = -1, E&& err= nullptr) const{
168
169 #ifdef _OPENMP
170 int num_thread=omp_get_thread_num();
171 #else
172 int num_thread=0;
173 #endif
174
175 std::ostringstream oss;
176 oss << " LOG::CRITICAL (Thread " << num_thread << "): " << std::endl;
177 oss << " Message : " << errstate << std::endl;
178 if (file_input)
179 oss << " From file: " << std::string(file_input) << std::endl;
180 if (line_input >= 0)
181 oss << " From line: " << line_input << std::endl;
182 std::string err_mess=oss.str();
183 throw err.istance(err_mess);
184 }
185
186
187
188
190 //debug
191 void inline pdebug() const {
192
193 #ifdef _OPENMP
194 int num_thread=omp_get_thread_num();
195 #else
196 int num_thread=0;
197 #endif
198
199 std::cout<<"\nLOG::DEBUG (Thread " << num_thread << ")"<< std::endl;
200 #ifdef _OPENMP
201 #pragma omp atomic
202 #endif
203 count_debug++;}
204
205 template<typename T, typename... Tail>
206 void pdebug(T head, Tail... tail) const{
208 std::cout << head << " ";
209 pdebug(tail...);
210 }
211 }
212
213
214 //info
215 void inline pinfo() const {
216
217 #ifdef _OPENMP
218 int num_thread=omp_get_thread_num();
219 #else
220 int num_thread=0;
221 #endif
222
223 std::cout<<"\nLOG::INFO (Thread " << num_thread << ")"<< std::endl;
224 #ifdef _OPENMP
225 #pragma omp atomic
226 #endif
227 count_info++;}
228
229 template<typename T, typename... Tail>
230 void pinfo(T head, Tail... tail) const {
232 std::cout << head << " ";
233 pinfo(tail...);
234 }
235 }
236
237 //warning
238 void inline pwarning() const {
239
240
241 #ifdef _OPENMP
242 int num_thread=omp_get_thread_num();
243 #else
244 int num_thread=0;
245 #endif
246
247 std::cerr<<"\nLOG::WARNING (Thread " << num_thread << ")"<< std::endl;
248 #ifdef _OPENMP
249 #pragma omp atomic
250 #endif
252 }
253
254 template<typename T, typename... Tail>
255 void pwarning(T head, Tail... tail) const {
256 //if (_LOG_LEVEL::_warning>=log_level and count_warning<=MAX_N_WARNING) {
258 std::cerr << head << " ";
259 pwarning(tail...);
260 }
261 }
262
263
265/* *//*
270 template<typename... Args>
271 void pdebug(Args... args){
272 if (_LOG_LEVEL::_debug>=log_level){
273 std::cout << " LOG::DEBUG (Thread " << omp_get_thread_num() << "): " << std::endl;
274 std::cout << " Message:";
275 ((std::cout << " "<<args), ...);
276 #pragma omp atomic
277 count_debug++;
278 }
279 }
280
281 *//*
286 template<typename... Args>
287 void pinfo(Args... args){
288 if (_LOG_LEVEL::_info>=log_level){
289 std::cout << " LOG::INFO (Thread " << omp_get_thread_num() << "): " << std::endl;
290 std::cout << " Message:";
291 ((std::cout << " "<<args), ...);
292 #pragma omp atomic
293 count_info++;
294 }
295 }*/
296
297
298
299
300
301
306 inline int get_level() { return log_level;};
307
312 inline unsigned int get_Ndebug() { return count_debug;};
317 inline unsigned int get_Ninfo() { return count_info;};
322 inline unsigned int get_Nwarning() { return count_warning;};
327 inline unsigned int get_Nerror() { return count_error;};
332 inline unsigned int get_Ncustom() { return count_custom_log;};
333
338 void set_level(std::string level);
339
340 protected:
341
342
347 _debug = 10,
348 _info = 20,
349 _warning = 30,
350 _error = 40,
351 _critical = 100,
352 };
353
354 //const unsigned int MAX_N_WARNING=10;
355
360 inline void set_level(int level) {log_level=level;};
361
362
363
364 static int log_level;
366 //GI This counter should be thread safe because each update is proteceted by the openmp atomic directive.
367 static unsigned int count_debug;
368 static unsigned int count_info;
369 static unsigned int count_warning;
370 static unsigned int count_error;
371 static unsigned int count_custom_log;
375 //NB the critical has not a counter since it always throws an exception, so we cannot have more than one call at runtime.
376 };
377
378
379}
380
381
382
383#endif //SEVN_SEVNLOG_H
Definition: sevnlog.h:43
void error(std::string errstate, const char *file_input=nullptr, int line_input=-1, bool stop=true) const
Definition: sevnlog.cpp:105
int get_level()
Definition: sevnlog.h:306
static unsigned int count_info
Definition: sevnlog.h:368
static unsigned int count_debug
Definition: sevnlog.h:367
unsigned int get_Ninfo()
Definition: sevnlog.h:317
void pwarning(T head, Tail... tail) const
Definition: sevnlog.h:255
void set_level(std::string level)
Definition: sevnlog.cpp:29
void critical(std::string errstate, const char *file_input=nullptr, int line_input=-1, E &&err=nullptr) const
Definition: sevnlog.h:167
void debug(std::string errstate, const char *file_input=nullptr, int line_input=-1) const
Definition: sevnlog.cpp:194
unsigned int get_Nerror()
Definition: sevnlog.h:327
_LOG_LEVEL
Definition: sevnlog.h:345
@ _notset
Definition: sevnlog.h:346
@ _debug
Definition: sevnlog.h:347
@ _warning
Definition: sevnlog.h:349
@ _error
Definition: sevnlog.h:350
@ _info
Definition: sevnlog.h:348
@ _critical
Definition: sevnlog.h:351
SevnLogging(int level)
Definition: sevnlog.h:57
SevnLogging()
Definition: sevnlog.h:50
unsigned int get_Nwarning()
Definition: sevnlog.h:322
static int log_level
Definition: sevnlog.h:364
static unsigned int count_warning
Definition: sevnlog.h:369
void set_level(int level)
Definition: sevnlog.h:360
~SevnLogging()
Definition: sevnlog.h:63
void info(std::string errstate, const char *file_input=nullptr, int line_input=-1) const
Definition: sevnlog.cpp:166
unsigned int get_Ncustom()
Definition: sevnlog.h:332
void pdebug(T head, Tail... tail) const
Definition: sevnlog.h:206
unsigned int get_Ndebug()
Definition: sevnlog.h:312
void pinfo() const
Definition: sevnlog.h:215
void pinfo(T head, Tail... tail) const
Definition: sevnlog.h:230
static unsigned int count_custom_log
Definition: sevnlog.h:371
static unsigned int count_error
Definition: sevnlog.h:370
void critical(std::string errstate, const char *file_input=nullptr, int line_input=-1) const
Definition: sevnlog.cpp:85
void pdebug() const
Variadic prints.
Definition: sevnlog.h:191
void error(std::string errstate, const char *file_input=nullptr, int line_input=-1, bool stop=true, E &&err=nullptr) const
Definition: sevnlog.h:120
void pwarning() const
Definition: sevnlog.h:238
void warning(std::string errstate, const char *file_input=nullptr, int line_input=-1) const
Definition: sevnlog.cpp:137
void log(int level, std::string errstate, const char *file_input=nullptr, int line_input=-1, int stop=0) const
Definition: sevnlog.cpp:51
Definition: errhand.h:22