forked from iden3/rapidsnark-old
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.cpp
371 lines (322 loc) · 8.44 KB
/
logger.cpp
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
///////////////////////////////////////////////////////////////////////////////
// @File Name: Logger.cpp //
// @Author: Pankaj Choudhary //
// @Version: 0.0.1 //
// @L.M.D: 13th April 2015 //
// @Description: For Logging into file //
// //
// Detail Description: //
// Implemented complete logging mechanism, Supporting multiple logging type //
// like as file based logging, console base logging etc. It also supported //
// for different log type. //
// //
// Thread Safe logging mechanism. Compatible with VC++ (Windows platform) //
// as well as G++ (Linux platform) //
// //
// Supported Log Type: ERROR, ALARM, ALWAYS, INFO, BUFFER, TRACE, DEBUG //
// //
// No control for ERROR, ALRAM and ALWAYS messages. These type of messages //
// should be always captured. //
// //
// BUFFER log type should be use while logging raw buffer or raw messages //
// //
// Having direct interface as well as C++ Singleton inface. can use //
// whatever interface want. //
// //
///////////////////////////////////////////////////////////////////////////////
// C++ Header File(s)
#include <iostream>
#include <cstdlib>
#include <ctime>
// Code Specific Header Files(s)
#include "logger.hpp"
using namespace std;
using namespace CPlusPlusLogging;
Logger* Logger::m_Instance = 0;
// Log file name. File name should be change from here only
const string logFileName = "MyLogFile.log";
Logger::Logger()
{
m_File.open(logFileName.c_str(), ios::out|ios::app);
m_LogLevel = LOG_LEVEL_TRACE;
m_LogType = FILE_LOG;
// Initialize mutex
#ifdef WIN32
InitializeCriticalSection(&m_Mutex);
#else
int ret=0;
ret = pthread_mutexattr_init(&m_Attr); // TODO: Pull request - Add this change to the common repo
if (ret != 0)
{
printf("Logger::Logger() -- Mutex attribute could not initialize!!\n");
exit(0);
}
ret = pthread_mutexattr_settype(&m_Attr, PTHREAD_MUTEX_ERRORCHECK);
if(ret != 0)
{
printf("Logger::Logger() -- Mutex attribute could not set type!!\n");
exit(0);
}
ret = pthread_mutex_init(&m_Mutex,&m_Attr);
if(ret != 0)
{
printf("Logger::Logger() -- Mutex not initialize!!\n");
exit(0);
}
#endif
}
Logger::~Logger()
{
m_File.close();
#ifdef WIN32
DeleteCriticalSection(&m_Mutex);
#else
pthread_mutexattr_destroy(&m_Attr);
pthread_mutex_destroy(&m_Mutex);
#endif
}
Logger* Logger::getInstance() throw ()
{
if (m_Instance == 0)
{
m_Instance = new Logger();
}
return m_Instance;
}
void Logger::lock()
{
#ifdef WIN32
EnterCriticalSection(&m_Mutex);
#else
pthread_mutex_lock(&m_Mutex);
#endif
}
void Logger::unlock()
{
#ifdef WIN32
LeaveCriticalSection(&m_Mutex);
#else
pthread_mutex_unlock(&m_Mutex);
#endif
}
void Logger::logIntoFile(std::string& data)
{
lock();
m_File << getCurrentTime() << " " << data << endl;
unlock();
}
void Logger::logOnConsole(std::string& data)
{
cout << getCurrentTime() << " " << data << endl;
}
string Logger::getCurrentTime()
{
string currTime;
//Current date/time based on current time
time_t now = time(0);
// Convert current time to string
currTime.assign(ctime(&now));
// Last charactor of currentTime is "\n", so remove it
string currentTime = currTime.substr(0, currTime.size()-1);
return currentTime;
}
// Interface for Error Log
void Logger::error(const char* text) throw()
{
string data;
data.append("[ERROR]: ");
data.append(text);
// ERROR must be capture
if(m_LogType == FILE_LOG)
{
logIntoFile(data);
}
else if(m_LogType == CONSOLE)
{
logOnConsole(data);
}
}
void Logger::error(std::string& text) throw()
{
error(text.data());
}
void Logger::error(std::ostringstream& stream) throw()
{
string text = stream.str();
error(text.data());
}
// Interface for Alarm Log
void Logger::alarm(const char* text) throw()
{
string data;
data.append("[ALARM]: ");
data.append(text);
// ALARM must be capture
if(m_LogType == FILE_LOG)
{
logIntoFile(data);
}
else if(m_LogType == CONSOLE)
{
logOnConsole(data);
}
}
void Logger::alarm(std::string& text) throw()
{
alarm(text.data());
}
void Logger::alarm(std::ostringstream& stream) throw()
{
string text = stream.str();
alarm(text.data());
}
// Interface for Always Log
void Logger::always(const char* text) throw()
{
string data;
data.append("[ALWAYS]: ");
data.append(text);
// No check for ALWAYS logs
if(m_LogType == FILE_LOG)
{
logIntoFile(data);
}
else if(m_LogType == CONSOLE)
{
logOnConsole(data);
}
}
void Logger::always(std::string& text) throw()
{
always(text.data());
}
void Logger::always(std::ostringstream& stream) throw()
{
string text = stream.str();
always(text.data());
}
// Interface for Buffer Log
void Logger::buffer(const char* text) throw()
{
// Buffer is the special case. So don't add log level
// and timestamp in the buffer message. Just log the raw bytes.
if((m_LogType == FILE_LOG) && (m_LogLevel >= LOG_LEVEL_BUFFER))
{
lock();
m_File << text << endl;
unlock();
}
else if((m_LogType == CONSOLE) && (m_LogLevel >= LOG_LEVEL_BUFFER))
{
cout << text << endl;
}
}
void Logger::buffer(std::string& text) throw()
{
buffer(text.data());
}
void Logger::buffer(std::ostringstream& stream) throw()
{
string text = stream.str();
buffer(text.data());
}
// Interface for Info Log
void Logger::info(const char* text) throw()
{
string data;
data.append("[INFO]: ");
data.append(text);
if((m_LogType == FILE_LOG) && (m_LogLevel >= LOG_LEVEL_INFO))
{
logIntoFile(data);
}
else if((m_LogType == CONSOLE) && (m_LogLevel >= LOG_LEVEL_INFO))
{
logOnConsole(data);
}
}
void Logger::info(std::string& text) throw()
{
info(text.data());
}
void Logger::info(std::ostringstream& stream) throw()
{
string text = stream.str();
info(text.data());
}
// Interface for Trace Log
void Logger::trace(const char* text) throw()
{
string data;
data.append("[TRACE]: ");
data.append(text);
if((m_LogType == FILE_LOG) && (m_LogLevel >= LOG_LEVEL_TRACE))
{
logIntoFile(data);
}
else if((m_LogType == CONSOLE) && (m_LogLevel >= LOG_LEVEL_TRACE))
{
logOnConsole(data);
}
}
void Logger::trace(std::string& text) throw()
{
trace(text.data());
}
void Logger::trace(std::ostringstream& stream) throw()
{
string text = stream.str();
trace(text.data());
}
// Interface for Debug Log
void Logger::debug(const char* text) throw()
{
string data;
data.append("[DEBUG]: ");
data.append(text);
if((m_LogType == FILE_LOG) && (m_LogLevel >= LOG_LEVEL_DEBUG))
{
logIntoFile(data);
}
else if((m_LogType == CONSOLE) && (m_LogLevel >= LOG_LEVEL_DEBUG))
{
logOnConsole(data);
}
}
void Logger::debug(std::string& text) throw()
{
debug(text.data());
}
void Logger::debug(std::ostringstream& stream) throw()
{
string text = stream.str();
debug(text.data());
}
// Interfaces to control log levels
void Logger::updateLogLevel(LogLevel logLevel)
{
m_LogLevel = logLevel;
}
// Enable all log levels
void Logger::enaleLog()
{
m_LogLevel = ENABLE_LOG;
}
// Disable all log levels, except error and alarm
void Logger:: disableLog()
{
m_LogLevel = DISABLE_LOG;
}
// Interfaces to control log Types
void Logger::updateLogType(LogType logType)
{
m_LogType = logType;
}
void Logger::enableConsoleLogging()
{
m_LogType = CONSOLE;
}
void Logger::enableFileLogging()
{
m_LogType = FILE_LOG ;
}