Medial Code Documentation
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1
15
16#ifndef __LOGGER__H__
17#define __LOGGER__H__
18
19#include <stdio.h>
20#include <vector>
21#include <string>
22#include <stdlib.h>
23#include <stdarg.h>
24#include <chrono>
25#include <stdexcept>
26
27#include <iostream>
28#include <fstream>
29#include <sstream>
30
31using namespace std;
32
34#define LOG_APP 0
36#define LOG_DEF 1
37// InfraMed sections
38#define LOG_INFRA 2
39#define LOG_REP 3
40#define LOG_INDEX 4
41#define LOG_DICT 5
42#define LOG_SIG 6
43#define LOG_CONVERT 7
44// MedUtils sections
45#define LOG_MED_UTILS 8
46#define LOG_MEDMAT 9
47#define LOG_MEDIO 10
48#define LOG_DATA_STRUCTURES 11
49// MedAlgo sections
50#define LOG_MEDALGO 12
51// MedFeat sections
52#define LOG_MEDFEAT 13
53// MedStat sections
54#define LOG_MEDSTAT 14
55// RepCleaner section
56#define LOG_REPCLEANER 15
57// FtrGenerator section
58#define LOG_FTRGNRTR 16
59// CrossValidator section
60#define LOG_CV 17
61// FeatCleaner section
62#define LOG_FEATCLEANER 18
63// ValueCleaner section
64#define LOG_VALCLNR 19
65// MedSamples section
66#define MED_SAMPLES_CV 20
67// FeatsSelector section
68#define LOG_FEAT_SELECTOR 21
69// SampleFilter section
70#define LOG_SMPL_FILTER 22
71// SerializableObject section
72#define LOG_SRL 23
73// MedModel section
74#define LOG_MED_MODEL 24
75// RepositoryType section
76#define LOG_REPTYPE 25
77
78#define MAX_LOG_SECTION 26
79
80
84
85#define NO_LOG_LEVEL 0
86#define MIN_LOG_LEVEL 0
87
88#define DEBUG_LOG_LEVEL 3
89
90#define LOG_DEF_LEVEL 5
91
92#define MAX_LOG_LEVEL 10
93#define VERBOSE_LOG_LEVEL 10
94
95
96extern vector<string> log_section_to_name;
97extern vector<string> log_level_to_name;
98
99class MedLogger {
100public:
101 vector<int> levels;
102 vector<vector<FILE *>> fds; //each section can write to multipal buffers
103 vector<vector<string>> file_names; // required to avoid multiple closing of same file
104 FILE *out_fd;
105 string out_file_name;
106 vector<string> format;
107
108 MedLogger();
109 ~MedLogger();
110
111 void init_all_levels(int level);
112 int init_all_files(const string &fname);
113 void init_level(int section, int level);
114 void init_file(int section, FILE *of);
115 int init_file(int section, const string &fname);
116 int add_file(int section, const string &fname);
117
118 void init_out(); //default output (stdout)
119 void init_out(FILE *of);
120 void init_out(const string &fname);
121
122 int log(int section, int print_level, const char *fmt, ...);
123 void out(char *fmt, ...);
124
130 void init_format(int section, const string &new_format);
131
135 void init_all_formats(int section, const string &new_format);
136};
137
138extern MedLogger global_logger;
139
141#define MEDLOG(Section,Level,fmt,...) global_logger.log(Section,Level,fmt, ##__VA_ARGS__)
143#define MDBG(Level,fmt,...) global_logger.log(LOCAL_SECTION,Level,fmt, ##__VA_ARGS__)
145#define MLOG(fmt,...) global_logger.log(LOCAL_SECTION, LOCAL_LEVEL, fmt, ##__VA_ARGS__)
147#define MLOG_V(fmt,...) global_logger.log(LOCAL_SECTION, VERBOSE_LOG_LEVEL, fmt, ##__VA_ARGS__)
149#define MLOG_D(fmt,...) global_logger.log(LOCAL_SECTION, DEBUG_LOG_LEVEL, fmt, ##__VA_ARGS__)
151#define MERR(fmt,...) global_logger.log(LOCAL_SECTION, MAX_LOG_LEVEL, fmt, ##__VA_ARGS__)
153#define MWARN(fmt,...) global_logger.log(LOCAL_SECTION, MAX_LOG_LEVEL-1, fmt, ##__VA_ARGS__)
154
155#define MOUT(fmt,...) global_logger.out(fmt, ##__VA_ARGS__)
156
157#define MTHROW_AND_ERR_STR(fmt,...) {char buff[300];snprintf(buff, sizeof(buff), fmt, ##__VA_ARGS__);global_logger.log(LOCAL_SECTION, MAX_LOG_LEVEL, buff); throw runtime_error(string(buff));}
158
159#define MTHROW_AND_ERR(fmt,...) {char buff[300];snprintf(buff, sizeof(buff), fmt, ##__VA_ARGS__);global_logger.log(LOCAL_SECTION,MAX_LOG_LEVEL,"RunTime ERROR: ");global_logger.log(LOCAL_SECTION, MAX_LOG_LEVEL, buff); throw std::runtime_error("Error");}
160
161
162// next works and compiles also in H files
163#define HMTHROW_AND_ERR(fmt,...) {char buff[300];snprintf(buff, sizeof(buff), fmt, ##__VA_ARGS__);global_logger.log(0, MAX_LOG_LEVEL, buff); throw runtime_error(string(buff));}
164
165
169class MedTimer {
170public:
171 string name;
172 chrono::high_resolution_clock::time_point t[2];
173 unsigned long long diff;
174
175 MedTimer(const string &tname) { name = tname; }
176 MedTimer() { name = string(""); }
177
178 void start() { t[0] = chrono::high_resolution_clock::now(); }
179 void take_curr_time() { t[1] = chrono::high_resolution_clock::now(); diff = (unsigned long long)(chrono::duration_cast<chrono::microseconds>(t[1] - t[0]).count()); }
180 unsigned long long get_clock_micro() {
181 auto t_now = chrono::high_resolution_clock::now();
182 auto micro = chrono::duration_cast<chrono::microseconds>(t_now.time_since_epoch());
183 return (unsigned long long)(micro.count());
184 }
185
186 double diff_microsec() { return (double)diff; }
187 double diff_milisec() { return (double)diff / 1000.0; }
188 double diff_sec() { return (double)diff / 1000000.0; }
189
190};
191
196public:
197 chrono::high_resolution_clock::time_point tm_start;
198 chrono::high_resolution_clock::time_point tm_prog;
199 int progress;
200 int max_loop;
201 int print_interval;
202 string print_title;
203 int max_threads;
204 bool alway_print_total;
205
206 int print_section;
207 int print_level;
208
214 MedProgress(const string &title, int mprocess_cnt, int print_inter = 30, int max_th = 50);
215
217 void update();
218
220 void skip_update();
221
223 void update_no_check();
224};
225
226void get_current_time(string &time_str);
227
228#endif
229
Definition Logger.h:99
void init_format(int section, const string &new_format)
sets log format.
Definition Logger.cpp:270
void init_all_formats(int section, const string &new_format)
sets log format for all sections - refer to init_format for more details
Definition Logger.cpp:275
vector< string > format
log format for each section - the message is s, reset is variables with $: time,section,...
Definition Logger.h:106
class to print progress of long process - multithreaded or not by time interval
Definition Logger.h:195
void skip_update()
update action when skiping action - update job counter
Definition Logger.cpp:383
void update_no_check()
update action when without checking if needs to print, faster
Definition Logger.cpp:389
void update()
update when completed process 1 item
Definition Logger.cpp:337
MedTimer - a very simple class to allow very easy time measures.
Definition Logger.h:169
Definition StdDeque.h:58