forked from zsummer/log4z
/
log4z.h
467 lines (414 loc) · 10.2 KB
/
log4z.h
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Log4z License
* -----------
*
* Log4z is licensed under the terms of the MIT license reproduced below.
* This means that Log4z is free software and can be used for both academic
* and commercial purposes at absolutely no cost.
*
*
* ===============================================================================
*
* Copyright (C) 2012 YaweiZhang <yawei_zhang@foxmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ===============================================================================
*
* (end of COPYRIGHT)
*/
/*
* AUTHORS: YaweiZhang <yawei_zhang@foxmail.com>
* VERSION: 1.2.0
* PURPOSE: A lightweight library for error reporting and logging to file and screen .
* CREATION: 2010.10.4
* LCHANGE: 2013.04.05
* LICENSE: Expat/MIT License, See Copyright Notice at the begin of this file.
*/
/*
*
* QQ Group: 19811947
* Web Site: www.zsummer.net
* mail: yawei_zhang@foxmail.com
*/
/*
* UPDATES
*
* VERSION 0.1.0 <DATE: 2010.10.4>
* create the first project.
* It support put log to screen and files,
* support log level, support one day one log file.
* support multiple thread, multiple operating system.
*
* VERSION .... <DATE: ...>
* ...
*
* VERSION 0.9.0 <DATE: 2012.12.24>
* support config files.
* support color text in screen.
* support multiple logger.
*
* VERSION 1.0.0 <DATE: 2012.12.29>
* support comments in the config file.
* add a advanced demo in the ./project
* fix some details.
*
* VERSION 1.0.1 <DATE: 2013.01.01>
* the source code haven't any change.
* fix some Comments in the log4z
* add some comments in the test projects.
* delete some needless code in the 'fast_test' demo projects, it's so simple.
*
* VERSION 1.1.0 <DATE: 2013.01.24>
* the method Start will wait for the logger thread started.
* config and add method change.
* namespace change.
* append some macro
*
* VERSION 1.1.1 <DATE: 2013.02.23>
* add GetStatus**** mothed.
* optimize.
*
* VERSION 1.2.0 <DATE: 2013.04.05>
* optimize
*
*/
#pragma once
#ifndef _ZSUMMER_LOG4Z_H_
#define _ZSUMMER_LOG4Z_H_
#include <string>
#include <sstream>
#include <errno.h>
//the max logger count.
const static int LOGGER_MAX = 20;
//the max log content length.
const static int LOG_BUF_SIZE = 2048;
typedef int LoggerId;
#define INVALID_LOGGERID -1
enum ENUM_LOG_LEVEL
{
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_WARN,
LOG_LEVEL_ERROR,
LOG_LEVEL_ALARM,
LOG_LEVEL_FATAL,
};
#ifndef _ZSUMMER_BEGIN
#define _ZSUMMER_BEGIN namespace zsummer {
#endif
#ifndef _ZSUMMER_LOG4Z_BEGIN
#define _ZSUMMER_LOG4Z_BEGIN namespace log4z {
#endif
_ZSUMMER_BEGIN
_ZSUMMER_LOG4Z_BEGIN
//log4z class
class ILog4zManager
{
public:
ILog4zManager(){};
virtual ~ILog4zManager(){};
//log4z Singleton
static ILog4zManager * GetInstance();
//Get Main Logger.
virtual bool ConfigMainLogger(std::string path="",
std::string name ="",
int nLevel = LOG_LEVEL_DEBUG,
bool display = true) = 0;
virtual LoggerId GetMainLogger() = 0;
///Get other loggers, The first must call ConfigFromFile() to configure.
virtual std::string GetExampleConfig() = 0;
virtual bool ConfigFromFile(std::string cfg) = 0;
virtual LoggerId GetLoggerFromName(std::string name) =0;
//Get other loggers.
virtual LoggerId DynamicCreateLogger( std::string path="",
std::string name ="",
int nLevel = LOG_LEVEL_DEBUG,
bool display = true) = 0;
// dynamic change logger's attribute.
virtual bool ChangeLoggerLevel(LoggerId nLoggerID, int nLevel) = 0;
virtual bool ChangeLoggerDisplay(LoggerId nLoggerID, bool enable) = 0;
// get log4z runtime status.
virtual unsigned long long GetStatusTotalWriteCount() = 0;
virtual unsigned long long GetStatusTotalWriteBytes() = 0;
virtual unsigned long long GetStatusWaitingCount() = 0;
virtual unsigned int GetStatusActiveLoggers() = 0;
//start and stop method.
virtual bool Start() = 0;
virtual bool Stop() = 0;
//push a base log
virtual bool PushLog(LoggerId id, int level, const char * log) = 0;
};
#ifndef _ZSUMMER_END
#define _ZSUMMER_END }
#endif
#ifndef _ZSUMMER_LOG4Z_END
#define _ZSUMMER_LOG4Z_END }
#endif
_ZSUMMER_LOG4Z_END
_ZSUMMER_END
class CStringStream;
#ifdef WIN32
extern __declspec(thread) char g_log4zstreambuf[LOG_BUF_SIZE];
#else
extern __thread char g_log4zstreambuf[LOG_BUF_SIZE];
#endif
//base log micro.
#define LOG_STREAM(id, level, log)\
{\
zsummer::log4z::CStringStream ss(g_log4zstreambuf, LOG_BUF_SIZE);\
ss << log;\
ss << " ( " << __FILE__ << " ) : " << __LINE__;\
zsummer::log4z::ILog4zManager::GetInstance()->PushLog(id, level, g_log4zstreambuf);\
}
//log micro
#define LOG_DEBUG(id, log) LOG_STREAM(id, LOG_LEVEL_DEBUG, log)
#define LOG_INFO(id, log) LOG_STREAM(id, LOG_LEVEL_INFO, log)
#define LOG_WARN(id, log) LOG_STREAM(id, LOG_LEVEL_WARN, log)
#define LOG_ERROR(id, log) LOG_STREAM(id, LOG_LEVEL_ERROR, log)
#define LOG_ALARM(id, log) LOG_STREAM(id, LOG_LEVEL_ALARM, log)
#define LOG_FATAL(id, log) LOG_STREAM(id, LOG_LEVEL_FATAL, log)
///fast log micro. It write log to the MainLogger.
#define LOGD( log ) LOG_DEBUG( zsummer::log4z::ILog4zManager::GetInstance()->GetMainLogger(), log )
#define LOGI( log ) LOG_INFO( zsummer::log4z::ILog4zManager::GetInstance()->GetMainLogger(), log )
#define LOGW( log ) LOG_WARN( zsummer::log4z::ILog4zManager::GetInstance()->GetMainLogger(), log )
#define LOGE( log ) LOG_ERROR( zsummer::log4z::ILog4zManager::GetInstance()->GetMainLogger(), log )
#define LOGA( log ) LOG_ALARM( zsummer::log4z::ILog4zManager::GetInstance()->GetMainLogger(), log )
#define LOGF( log ) LOG_FATAL( zsummer::log4z::ILog4zManager::GetInstance()->GetMainLogger(), log )
_ZSUMMER_BEGIN
_ZSUMMER_LOG4Z_BEGIN
//! ÐÔÄÜÓÅ»¯
#ifdef WIN32
#pragma warning(push)
#pragma warning(disable:4996)
#endif
class CStringStream
{
public:
CStringStream(char * buf, int len)
{
m_pBegin = buf;
m_pEnd = buf + len;
m_pCur = m_pBegin;
}
template<class T>
void WriteData(const char * ft, T t)
{
if (m_pCur < m_pEnd)
{
int len = 0;
int count = (int)(m_pEnd - m_pCur);
#ifdef WIN32
len = _snprintf(m_pCur, count, ft, t);
int err = errno;
if (len == count || (len == -1 && errno == ERANGE))
{
len = count;
*(m_pEnd-1) = '\0';
}
else if (len < 0)
{
*m_pCur = '\0';
len = 0;
}
#else
len = snprintf(m_pCur, count, ft, t);
if (len < 0)
{
*m_pCur = '\0';
len = 0;
}
else if (len >= count)
{
len = count;
*(m_pEnd-1) = '\0';
}
#endif
m_pCur += len;
}
}
template<class T>
CStringStream & operator <<(T t)
{
return *this;
}
template<class T>
CStringStream & operator <<(const T * t)
{
#ifdef WIN32
if (sizeof(t) == 8)
{
WriteData("%016I64x", (unsigned long long)t);
}
else
{
WriteData("%08I64x", (unsigned long long)t);
}
#else
if (sizeof(t) == 8)
{
WriteData("%016llx", (unsigned long long)t);
}
else
{
WriteData("%08llx", (unsigned long long)t);
}
#endif
return *this;
}
template<class T>
CStringStream & operator <<(T * t)
{
#ifdef WIN32
if (sizeof(t) == 8)
{
WriteData("%016I64x", (unsigned long long)t);
}
else
{
WriteData("%08I64x", (unsigned long long)t);
}
#else
if (sizeof(t) == 8)
{
WriteData("%016llx", (unsigned long long)t);
}
else
{
WriteData("%08llx", (unsigned long long)t);
}
#endif
return *this;
}
CStringStream & operator <<(char * t)
{
WriteData("%s", t);
return *this;
}
CStringStream & operator <<(const char * t)
{
WriteData("%s", t);
return *this;
}
CStringStream & operator <<(bool t)
{
if(t)WriteData("%s", "true");
else WriteData("%s", "false");
return *this;
}
CStringStream & operator <<(char t)
{
WriteData("%c", t);
return *this;
}
CStringStream & operator <<(unsigned char t)
{
WriteData("%d",(int)t);
return *this;
}
CStringStream & operator <<(short t)
{
WriteData("%d", (int)t);
return *this;
}
CStringStream & operator <<(unsigned short t)
{
WriteData("%d", (int)t);
return *this;
}
CStringStream & operator <<(int t)
{
WriteData("%d", t);
return *this;
}
CStringStream & operator <<(unsigned int t)
{
WriteData("%ud", t);
return *this;
}
CStringStream & operator <<(long t)
{
if (sizeof(long) == sizeof(int))
{
WriteData("%d", t);
}
else
{
WriteData("%lld", t);
}
return *this;
}
CStringStream & operator <<(unsigned long t)
{
if (sizeof(unsigned long) == sizeof(unsigned int))
{
WriteData("%ud", t);
}
else
{
WriteData("%llu", t);
}
return *this;
}
CStringStream & operator <<(long long t)
{
#ifdef WIN32
WriteData("%I64d", t);
#else
WriteData("%lld", t);
#endif
return *this;
}
CStringStream & operator <<(unsigned long long t)
{
#ifdef WIN32
WriteData("%I64u", t);
#else
WriteData("%llu", t);
#endif
return *this;
}
CStringStream & operator <<(float t)
{
WriteData("%.4f", t);
return *this;
}
CStringStream & operator <<(double t)
{
WriteData("%.6lf", t);
return *this;
}
CStringStream & operator <<(const std::string t)
{
WriteData("%s", t.c_str());
return *this;
}
private:
CStringStream(){}
CStringStream(CStringStream &){}
char * m_pBegin;
char * m_pEnd;
char * m_pCur;
};
#ifdef WIN32
#pragma warning(pop)
#endif
_ZSUMMER_LOG4Z_END
_ZSUMMER_END
#endif