-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLog.h
More file actions
86 lines (72 loc) · 2.68 KB
/
MyLog.h
File metadata and controls
86 lines (72 loc) · 2.68 KB
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
#ifndef MYLOG_H
#define MYLOG_H
#include<log4cpp/Category.hh>
#include<iostream>
class MyLog
{
public:
static MyLog& getInstance();
static void destory();
void tableError(const char* msg, ...);
void tableWarn(const char* msg, ...);
void tableInfo(const char* msg, ...);
void tableDebug(const char* msg, ...);
private:
MyLog();
static MyLog *plog_;
log4cpp::Category &table;
};
// Convert int to string
inline std::string int2string(int line) {
std::ostringstream oss;
oss << line;
return oss.str();
}
// Define a Macro to add File Name, Function Name, Line Number to the end of log
#define logSuffix(msg) std::string(msg).append(" ##")\
.append(__FILE__).append(":").append(__func__)\
.append(":").append(int2string(__LINE__))\
.append("##").c_str()
// Noneed to call getInstance every time, only write #define _LOG4CPP_ in the main function file
#ifdef _LOG4CPP_
MyLog &logger = MyLog::getInstance();
#else
extern MyLog &logger;
#endif
//#define LogTableError(msg) logger.tableError(logSuffix(msg))
#define LogTableError(msg, ...) \
do { \
size_t size = 1024; \
char* buffer = new char[size]; \
sprintf(buffer, msg, ## __VA_ARGS__); \
logger.tableError(logSuffix(buffer)); \
} \
while (0)
//#define LogTableWarn(msg) logger.tableWarn(logSuffix(msg))
#define LogTableWarn(msg, ...) \
do { \
size_t size = 1024; \
char* buffer = new char[size]; \
sprintf(buffer, msg, ## __VA_ARGS__); \
logger.tableWarn(logSuffix(buffer)); \
} \
while (0)
//#define LogTableInfo(msg) logger.tableInfo(logSuffix(msg))
#define LogTableInfo(msg, ...) \
do { \
size_t size = 1024; \
char* buffer = new char[size]; \
sprintf(buffer, msg, ## __VA_ARGS__); \
logger.tableInfo(logSuffix(buffer)); \
} \
while (0)
//#define LogTableDebug(msg) logger.tableDebug(logSuffix(msg))
#define LogTableDebug(msg, ...) \
do { \
size_t size = 1024; \
char* buffer = new char[size]; \
sprintf(buffer, msg, ## __VA_ARGS__); \
logger.tableDebug(logSuffix(buffer)); \
} \
while (0)
#endif // MYLOG_H