-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cc
102 lines (94 loc) · 2.02 KB
/
Logger.cc
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
#include "Logger.h"
#include "Timestamp.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdarg>
// 获取日志类唯一的实例对象
Logger &Logger::instance()
{
static Logger logger;
return logger;
}
// 设置日志级别
void Logger::setLogLevel(int level)
{
logLevel_ = level;
}
void Logger::setLogName(const std::string &logName)
{
logName_ = logName;
}
// 写日志接口
// 格式:[级别] time : msg
void Logger::log(std::string msg)
{
switch (logLevel_)
{
case INFO:
std::cout << "[INFO]";
break;
case ERROR:
std::cout << "[ERROR]";
break;
case FATAL:
std::cout << "[FATAL]";
break;
case DEBUG:
std::cout << "[DEBUG]";
break;
default:
break;
}
// 打印时间和msg -> 时间直接用muduo的timestamp
std::cout << Timestamp::now().toString() << " : " << msg << std::endl;
}
void Logger::asyncWrite(int logLevel, const std::string &format, ...)
{
std::stringstream ss;
{
va_list args;
va_start(args, format);
char buffer[128];
vsnprintf(buffer, sizeof(buffer), format.c_str(), args);
ss << buffer;
va_end(args);
}
std::lock_guard<std::mutex> lock(mutex_);
queue_.push(ss.str());
cond_.notify_one();
}
void Logger::stop()
{
stop_ = true;
cond_.notify_all();
}
Logger::Logger():stop_(false), logName_("log.txt")
{
std::thread([this]() {
while(true)
{
// 如果队列不为空,需要判断是否停止
// 如果队列为空,但是如果停止了也要跳出wait
std::unique_lock<std::mutex> lock(mutex_);
cond_.wait(lock,[&](){ return !queue_.empty() || stop_; });
if(stop_)
{
return;
}
std::string task = queue_.front();
queue_.pop();
lock.unlock();
processTask(task);
} })
.detach();
}
void Logger::processTask(std::string task)
{
// 做本地化存储
task.insert(0, Timestamp::now().toString() + " ");
std::ofstream logFile(logName_, std::ios::app);
logFile << task << std::endl;
logFile.flush();
logFile.close();
}