-
Notifications
You must be signed in to change notification settings - Fork 1
/
asynclog.cpp
136 lines (122 loc) · 2.97 KB
/
asynclog.cpp
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
#include "asynclog.h"
AsyncLog::AsyncLog()
: m_bRunning(false),
m_mutex (new Mutex),
m_mainThreadRunCond(new Condition(*m_mutex)),
m_subThreadFlushCond(new Condition(*m_mutex)),
m_thread(std::bind(&AsyncLog::ThreadFunc, this)),
m_curBuff(new Buffer),
m_nextBuff(new Buffer),
m_logFilePtr(new LogFile)
{
m_buffers.reserve(8);
}
AsyncLog::~AsyncLog()
{
if (m_bRunning)
{
Stop();
}
Sync();
}
void AsyncLog::Start()
{
m_bRunning = true;
m_thread.Start();
LockGuard lock(*m_mutex);
m_mainThreadRunCond->Wait();
//std::cout << "Main thread Running " << __LINE__ << std::endl;
}
void AsyncLog::Stop()
{
m_bRunning = false;
m_subThreadFlushCond->Notify();
m_thread.Join();
}
void AsyncLog::Sync()
{
m_logFilePtr->Sync();
}
void AsyncLog::NotifyLog()
{
m_subThreadFlushCond->Notify();
}
void AsyncLog::Append(const char * data, size_t writeSize)
{
//std::cout << "Main thread, " << __LINE__ << std::endl;
LockGuard lock(*m_mutex);
if (m_curBuff->Avail() > writeSize)
{
//std::cout << __FILE__ << " " << __LINE__ << " " << __func__ << " m_curBuff->Avail()=" << m_curBuff->Avail() << " writeSize=" << writeSize << std::endl;
m_curBuff->Append(data, writeSize);
}
else
{
m_buffers.push_back(std::move(m_curBuff));
if (m_nextBuff)
{
m_curBuff = std::move(m_nextBuff);
}
else
{
m_curBuff.reset(new Buffer);
}
m_curBuff->Append(data, writeSize);
m_subThreadFlushCond->Notify();
}
//std::cout << "Main thread, " << __LINE__ << std::endl;
}
void AsyncLog::ThreadFunc()
{
BufferPtr firstBuffer(new Buffer);
BufferPtr secondBuffer(new Buffer);
std::vector<BufferPtr> vecWriteBuffers;
vecWriteBuffers.reserve(16);
{
LockGuard lock(*m_mutex);
m_mainThreadRunCond->Notify();
}
//std::cout << "Sub thread, " << __LINE__ << " Running " << std::endl;
while(m_bRunning)
{
{
LockGuard lock(*m_mutex);
if (m_buffers.empty())
{
//std::cout << "Sub thread, wait for Condition, " << __LINE__ << std::endl;
m_subThreadFlushCond->WaitForSeconds(3);
//m_subThreadFlushCond.Wait();
}
//std::cout << "Sub thread, " << __LINE__ << " Buffer size, " <<m_buffers.size() << std::endl;
m_buffers.push_back(std::move(m_curBuff));
m_curBuff = std::move(firstBuffer);
m_curBuff->Reset();
if (!m_nextBuff)
{
m_nextBuff = std::move(secondBuffer);
m_nextBuff->Reset();
}
m_buffers.swap(vecWriteBuffers);
m_buffers.clear();
}
for (size_t i = 0; i < vecWriteBuffers.size(); ++i)
{
m_logFilePtr->Append(vecWriteBuffers[i]->Data(), vecWriteBuffers[i]->Size());
}
firstBuffer = std::move(vecWriteBuffers[0]);
if (2 <= vecWriteBuffers.size())
{
secondBuffer = std::move(vecWriteBuffers[1]);
}
vecWriteBuffers.clear();
m_logFilePtr->Flush();
}
// push data in buffer to the log file
LockGuard lock(*m_mutex);
m_buffers.push_back(std::move(m_curBuff));
for (size_t i = 0; i < m_buffers.size(); ++i)
{
m_logFilePtr->Append(m_buffers[i]->Data(), m_buffers[i]->Size());
}
m_logFilePtr->Flush();
}