SimpleLog.h 6.81 KB
Newer Older
Your Name's avatar
Your Name committed
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
// 简易日志

#ifndef __SIMPLE_LOG_H__
#define __SIMPLE_LOG_H__

#include <time.h>
#include <string>
#include <map>
#include <thread>
#include <mutex>
#if (defined WIN32 || defined _WIN32)
#include <Windows.h>
#else
#include <sys/time.h>
#endif

using namespace std;


/** 简易日志
 * 
 * 轻量级日志系统,不依赖于其他第三方库,只需要包含一个头文件就可以使用。提供了4种日志级别,包括INFO,DEBUG,WARN和ERROR。
 * 
 * 示例1:
    // 初始化日志,在./Log/目录下创建两个日志文件log1.log和log2.log(注意:目录./Log/需要存在,否则日志创建失败)
    LogManager::GetInstance()->Initialize("./Log/","log1");
    LogManager::GetInstance()->Initialize("./Log/","log2");

    // 写日志
    string log = "Hello World";
    LOG_INFO(LogManager::GetInstance()->GetLogFile("log1"), "%s\n", log.c_str()); // 写入log1.log
    LOG_INFO(LogManager::GetInstance()->GetLogFile("log2"), "%s\n", log.c_str()); // 写入log2.log

    // 关闭日志
    LogManager::GetInstance()->Close("log1");
    LogManager::GetInstance()->Close("log2");
  
  * 示例2:
    // 将日志输出到控制台
    string log = "Hello World";
    LOG_INFO(stdout, "%s\n", log.c_str()); 

  * 注意:
    1. 需要C++11
    2. 多线程的时候需要加锁(打开#define LOG_MUTEX),否则会导致日志显示混乱

*/

// #define LOG_MUTEX  // 加锁

class LogManager
{
private:
    LogManager(){}

public:
    ~LogManager(){}
    inline void Initialize(const string &parentPath,const string &logName)
    {
        // 日志名为空表示输出到控制台
        if(logName.size()==0)
            return;

        // 查找该日志文件,如果没有则创建
        std::map<string, FILE*>::const_iterator iter = logMap.find(logName);
        if (iter == logMap.end())
        {
            string pathOfLog = parentPath+ logName + ".log";
            FILE *logFile = fopen(pathOfLog.c_str(), "a"); // w:覆盖原有文件,a:追加
            if(logFile!=NULL)
            {
                logMap.insert(std::make_pair(logName, logFile));
            }
        }

    }
    inline FILE* GetLogFile(const string &logName)
    {
        std::map<string, FILE*>::const_iterator iter=logMap.find(logName);
        if(iter==logMap.end())
        {
            return NULL;
        }

        return (*iter).second;
    }
    inline void Close(const string &logName)
    {
        std::map<string, FILE*>::const_iterator iter=logMap.find(logName);
        if(iter==logMap.end())
        {
            return;
        }

        fclose((*iter).second);
        logMap.erase(iter);
    }
    inline std::mutex &GetLogMutex()
    {
        return logMutex;
    }

    // Singleton
    static LogManager* GetInstance()
    {
        static LogManager logManager;
        return &logManager;
    }
private:
    std::map<string, FILE*> logMap;
    std::mutex logMutex;
};

#ifdef LOG_MUTEX
    #define LOCK	LogManager::GetInstance()->GetLogMutex().lock()
    #define UNLOCK	LogManager::GetInstance()->GetLogMutex().unlock()
#else
    #define LOCK
    #define UNLOCK
#endif

// log time
typedef struct  _LogTime
{
    string year;
    string month;
    string day;
    string hour;
    string minute;
    string second;
    string millisecond; // ms
    string microsecond; // us
    string weekDay;
}LogTime;

inline LogTime GetTime()
{
    LogTime currentTime;

#if (defined WIN32 || defined _WIN32)
    SYSTEMTIME systemTime;
    GetLocalTime(&systemTime);

    char temp[8] = { 0 };
    sprintf(temp, "%04d", systemTime.wYear);
    currentTime.year=string(temp);
    sprintf(temp, "%02d", systemTime.wMonth);
    currentTime.month=string(temp);
    sprintf(temp, "%02d", systemTime.wDay);
    currentTime.day=string(temp);
    sprintf(temp, "%02d", systemTime.wHour);
    currentTime.hour=string(temp);
    sprintf(temp, "%02d", systemTime.wMinute);
    currentTime.minute=string(temp);
    sprintf(temp, "%02d", systemTime.wSecond);
    currentTime.second=string(temp);
    sprintf(temp, "%03d", systemTime.wMilliseconds);
    currentTime.millisecond=string(temp);
    sprintf(temp, "%d", systemTime.wDayOfWeek);
    currentTime.weekDay=string(temp);
#else
    struct timeval    tv;
    struct tm         *p;
    gettimeofday(&tv, NULL);
    p = localtime(&tv.tv_sec);

    char temp[8]={0};
    sprintf(temp,"%04d",1900+p->tm_year);
    currentTime.year=string(temp);
    sprintf(temp,"%02d",1+p->tm_mon);
    currentTime.month=string(temp);
    sprintf(temp,"%02d",p->tm_mday);
    currentTime.day=string(temp);
    sprintf(temp,"%02d",p->tm_hour);
    currentTime.hour=string(temp);
    sprintf(temp,"%02d",p->tm_min);
    currentTime.minute=string(temp);
    sprintf(temp,"%02d",p->tm_sec);
    currentTime.second=string(temp);
    sprintf(temp,"%03d",(int)(tv.tv_usec/1000));
    currentTime.millisecond = string(temp);
    sprintf(temp, "%03d", (int)(tv.tv_usec % 1000));
    currentTime.microsecond = string(temp);
    sprintf(temp, "%d", p->tm_wday);
    currentTime.weekDay = string(temp);
#endif
    return currentTime;
}

#define LOG_TIME(logFile) \
do\
{\
    LogTime currentTime=GetTime(); \
    fprintf(((logFile == NULL) ? stdout : logFile), "%s-%s-%s %s:%s:%s.%s\t",currentTime.year.c_str(),currentTime.month.c_str(),currentTime.day.c_str(),currentTime.hour.c_str(),currentTime.minute.c_str(),currentTime.second.c_str(),currentTime.millisecond.c_str()); \
}while (0)


#define LOG_INFO(logFile,logInfo, ...) \
do\
{\
    LOCK; \
    LOG_TIME(logFile); \
    fprintf(((logFile == NULL) ? stdout : logFile), "INFO\t"); \
    fprintf(((logFile == NULL) ? stdout : logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
    fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ## __VA_ARGS__); \
    fflush(logFile); \
    UNLOCK; \
} while (0)

#define LOG_DEBUG(logFile,logInfo, ...) \
do\
{\
    LOCK; \
    LOG_TIME(logFile);\
    fprintf(((logFile==NULL)?stdout:logFile), "DEBUG\t"); \
    fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
    fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
    fflush(logFile); \
    UNLOCK; \
} while (0)

#define LOG_ERROR(logFile,logInfo, ...) \
do\
{\
    LOCK; \
    LOG_TIME(logFile);\
    fprintf(((logFile==NULL)?stdout:logFile), "ERROR\t"); \
    fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
    fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
    fflush(logFile); \
    UNLOCK; \
} while (0)

#define LOG_WARN(logFile,logInfo, ...) \
do\
{\
    LOCK; \
    LOG_TIME(logFile);\
    fprintf(((logFile==NULL)?stdout:logFile), "WARN\t"); \
    fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
    fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
    fflush(logFile); \
    UNLOCK; \
} while (0)

#endif // __SIMPLE_LOG_H__