-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogstream.cc
332 lines (304 loc) · 8.06 KB
/
logstream.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "logstream.h"
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <sstream>
#include <chrono>
namespace Logger_nsp
{
namespace details
{
std::atomic<bool> LogStream::inProgress (false);
std::atomic<short> LogStream::atomicCounter(0);
const char digits[] = "9876543210123456789";
static_assert(sizeof(digits) == 20, "digits bits not equal");
static const char* zero = digits + 9;
const char hexDigits[] = "0123456789ABCDEF";
static_assert(sizeof(hexDigits) == 17, "digits bits not equal");
//Efficient Conversion From Integer to String
template <typename T>
size_t Convert(char buf[], T value)
{
T data = value;
char* ptr = buf;
do
{
int lsd = static_cast<int>(data % 10);
*ptr++ = zero[lsd];
data /= 10;
} while (data != 0);
if (value < 0)
{
*ptr++ = '-';
}
*ptr = '\0';
std::reverse(buf, ptr);
return ptr - buf;
}
size_t ConvertHex(char buf[], uintptr_t value)
{
uintptr_t data = value;
char* ptr = buf;
do
{
int lsd = static_cast<int>(data % 16);
*ptr++ = hexDigits[lsd];
data /= 16;
} while (data != 0);
*ptr = '\0';
std::reverse(buf, ptr);
return ptr - buf;
}
template <typename T>
void LogStream::FormatInteger(T value)
{
std::lock_guard<std::mutex>lk(lockMutex);
if (!inProgress)
{
inProgress = true;
}
if (buffer.Avail() >= kMaxDigitlen)
{
size_t len = Convert(buffer.Current(), value);
buffer.Increase(len);
}
}
LogStream::Self& LogStream:: operator<<(short s)
{
*this << static_cast<int>(s);
return *this;
}
LogStream::Self& LogStream::operator<<(unsigned short us)
{
*this << static_cast<short>(us);
return *this;
}
LogStream::Self& LogStream::operator<<(int t)
{
FormatInteger(t);
return *this;
}
LogStream::Self& LogStream::operator<<(unsigned t)
{
FormatInteger(t);
return *this;
}
LogStream::Self& LogStream::operator<<(long t)
{
FormatInteger(t);
return *this;
}
LogStream::Self& LogStream::operator<<(unsigned long t)
{
FormatInteger(t);
return *this;
}
LogStream::Self& LogStream::operator<<(long long t)
{
FormatInteger(t);
return *this;
}
LogStream::Self& LogStream::operator<<(unsigned long long t)
{
FormatInteger(t);
return *this;
}
LogStream::Self& LogStream::operator<<(float v)
{
*this << static_cast<double>(v);
return *this;
}
LogStream::Self& LogStream::operator<<(double v)
{
char temp[20] = { 0 };
std::ostringstream str;
#if defined(_WIN32) && defined(_MSC_VER) // using windows vc compiler
sprintf_s(temp, sizeof(temp), "%.12f", v);
#elif defined(__GNUC__) // using GCC compiler
snprintf(temp, sizeof(temp), "%.12.f", v);
#endif
str << temp;
*this << str.str();
return *this;
}
LogStream::Self& LogStream::operator<<(char v)
{
*this << (unsigned char)v;
return *this;
}
LogStream::Self& LogStream::operator<<(unsigned char v)
{
std::lock_guard<std::mutex>guard(lockMutex);
if (!inProgress)
{
inProgress = true;
}
buffer.Append((char*)&v, 1);
return *this;
}
LogStream::Self& LogStream::operator<<(const void*v)
{
std::lock_guard<std::mutex>guard(lockMutex);
if (!inProgress)
{
inProgress = true;
}
uintptr_t value = reinterpret_cast<uintptr_t>(v);
if (buffer.Avail() >= kMaxDigitlen)
{
char* ptr = const_cast<char*>(buffer.Current());
ptr[0] = '0';
ptr[1] = 'X';
size_t len = ConvertHex(ptr + 2, value);
buffer.Increase(len + 2);
}
return *this;
}
LogStream::Self& LogStream::operator<<(const char* v)
{
std::lock_guard<std::mutex>guard(lockMutex);
if (!inProgress)
{
inProgress = true;
}
if (v != nullptr)
{
buffer.Append(v, strlen(v));
}
else
buffer.Append("(NULL)", 6);
return *this;
}
LogStream::Self& LogStream::operator<<(const std::string& v)
{
*this << v.c_str();
return *this;
}
void LogStream::Submit()
{
static int counter = 0;
size_t lenth = 0;
//std::unique_lock<std::mutex>lk(util->GetMutex());
std::unique_lock<std::mutex>lk(buffer.GetUtil()->GetMutex());
{
//printf("提交数据\n");
size_t index = buffer.GetSubmitIndex();
const char* dataSource = buffer.GetData() + index;
lenth = buffer.Length() - index;
printf("提交数据长度为 %d\n", lenth);
if (lenth > 0)
{
if (lenth >= 131072)
{
lenth = buffer.GetUtil()->AvailSubmitBuffer() - 64;
}
index += lenth;
//printf("util.fp 0x%x %d %s\n", buffer.GetUtil()->getfp(), __LINE__, __FILE__);
char* dataDestination = (char*)buffer.GetUtil()->GetSubmitBuffer();
char* origin = buffer.GetUtil()->GetOriginBuffer();
/**
* 注意这里不能直接提交给写入线程,由于在密集写入的情况下,根据CPU调度的策略
* 此时几乎不会分给写入线程相应的时间片(主要是写入线程依靠信号量来通知),而
* 提交线程则是每秒10次的速度提交,所以此时有大量的数据写入Fixbuffer的缓冲区,
* 而此时如果直接提交给写入线程,由于写入线程没有机会运行,所以很快会导致写入缓冲
* 溢出,这会直接导致程序退出。解决策略是提交之前判断写入缓冲有多少Available缓冲
* 区,若足够则直接进行写入,否则只是更新提交缓冲的index标志,缓冲数据留待下次
* 能够写入的时候再进行写入。
*/
if ((dataDestination + lenth) < (origin + 131072))
{
buffer.SetSubmitIndex(index);
// lenth + datadestination > 65536;
assert((dataDestination + lenth) < ( origin + 131072) && "Caution ! Buffer Out of range!");
memcpy(dataDestination, dataSource, lenth);
*(dataDestination + lenth) = '\0';
if (index == buffer.Length() && buffer.Has_unSubmitBuf())
{
printf("全部内容已提交\n");
printf("全部提交时index为 %d\n", index);
/************************************************************************/
/* 这里可以进行文件句柄切换。 */
/************************************************************************/
if (buffer.GetFirstLengthSize() == index)
{
buffer.SetSubmitFinished(false);
buffer.SetFirstBufferLength(-1);
}
else if (buffer.GetSecondLengthSize() == index)
{
buffer.SetifStillHasUnSubmit(false);
buffer.SetSecondBufferLength(-1);
buffer.SetOutofRangeState(false);
}
buffer.SetSubmitIndex(0);
/************************************************************************/
/* 可能会有一个尾巴数据会写到下一个文件当中 */
/************************************************************************/
printf("切换文件句柄\n");
buffer.RollFile();
}
}
else
{
outOfRange = true;
}
}
}
if (lenth > 0 && !outOfRange)
{
buffer.GetUtil()->SetSubmitSize(lenth);
}
lk.unlock();
if (lenth > 0)
{
buffer.GetUtil()->NotifyMe();
}
}
LogStream::LogStream(const std::string& fileName)
:finished(false),
outOfRange(false)
//lastSubmitIndex(0)
{
buffer.SetBaseFileName(fileName);
buffer.Start();
StartCounter();
}
LogStream::~LogStream()
{
printf("LogStream 析构\n");
std::this_thread::sleep_for(std::chrono::seconds(6));
StopCounter();
}
void LogStream::Tick()
{
while (!finished)
{
if (!inProgress)
{
this->Submit();
}
std::unique_lock<std::mutex>lk(lockMutex);
inProgress = false;
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void LogStream::StartCounter()
{
try
{
counter = std::thread(&LogStream::Tick, this);
}
catch (...)
{
finished = true;
throw;
}
}
void LogStream::StopCounter()
{
finished = true;
counter.join();
}
}
}