-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.h
59 lines (47 loc) · 1.58 KB
/
logger.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
/**
* 日志类头文件, Logger.h
* zhangyl 2017.02.28
**/
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include <string>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <list>
//struct FILE;
class Logger
{
public:
static Logger& GetInstance();
void SetFileName(const char* filename);
bool Start();
void Stop();
void AddToQueue(const char* pszLevel,
const char* pszFile,
int lineNo,
const char* pszFuncSig,
char* pszFmt, ...);
private:
Logger() = default;
Logger(const Logger& rhs) = delete;
Logger& operator =(Logger& rhs) = delete;
void threadfunc();
private:
std::string filename_;
FILE* fp_{};
std::shared_ptr<std::thread> spthread_;
std::mutex mutex_;
std::condition_variable cv_;
//有新的日志到来的标识
bool exit_{false};
std::list<std::string> queue_;
};
#define LogInfo(...) \
Logger::GetInstance().AddToQueue("INFO", __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
#define LogWarning(...) \
Logger::GetInstance().AddToQueue("WARNING", __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
#define LogError(...) \
Logger::GetInstance().AddToQueue("ERROR", __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
#endif //!__LOGGER_H__