-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKAsyncLogger.cpp
More file actions
373 lines (320 loc) · 8.94 KB
/
Copy pathKAsyncLogger.cpp
File metadata and controls
373 lines (320 loc) · 8.94 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "KAsyncLogger.h"
#include <chrono>
#include <condition_variable>
#include <cstdarg>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
namespace kback {
namespace {
constexpr size_t kMaxLogLineBytes = 1024;
constexpr size_t kInitialBufferedRecords = 1024;
constexpr size_t kMaxBufferedRecords = 16384;
constexpr size_t kNotifyThreshold = 256;
constexpr auto kFlushInterval = std::chrono::milliseconds(500);
struct LogRecord {
char data[kMaxLogLineBytes];
size_t size;
};
const char *defaultLogFilePath() {
const char *envPath = std::getenv("WEBSERVER_LOG_FILE");
if (envPath != nullptr && envPath[0] != '\0') {
return envPath;
}
return "webserver.log";
}
const char *basenameOf(const char *path) {
if (path == nullptr) {
return "unknown";
}
const char *slash = std::strrchr(path, '/');
return slash == nullptr ? path : slash + 1;
}
const char *levelToString(LogLevel level) {
switch (level) {
case LogLevel::kTrace:
return "TRACE";
case LogLevel::kDebug:
return "DEBUG";
case LogLevel::kInfo:
return "INFO";
case LogLevel::kWarn:
return "WARN";
case LogLevel::kError:
return "ERROR";
case LogLevel::kFatal:
return "FATAL";
case LogLevel::kSysErr:
return "SYSERR";
case LogLevel::kSysFatal:
return "SYSFATAL";
}
return "UNKNOWN";
}
unsigned long currentThreadId() {
return static_cast<unsigned long>(::syscall(SYS_gettid));
}
void writeAll(int fd, const char *data, size_t size) {
while (size > 0) {
const ssize_t written = ::write(fd, data, size);
if (written > 0) {
data += written;
size -= static_cast<size_t>(written);
continue;
}
if (written < 0 && errno == EINTR) {
continue;
}
break;
}
}
size_t appendFormatted(char *buffer, size_t capacity, size_t offset,
const char *format, va_list args) {
if (offset >= capacity) {
return capacity;
}
const int written =
std::vsnprintf(buffer + offset, capacity - offset, format, args);
if (written < 0) {
return offset;
}
const size_t advanced = static_cast<size_t>(written);
if (offset + advanced >= capacity) {
return capacity - 1;
}
return offset + advanced;
}
LogRecord formatLogRecord(LogLevel level, const char *file, int line,
int savedErrno, const char *format, va_list args) {
LogRecord record{};
char timestamp[64];
struct timespec ts;
::clock_gettime(CLOCK_REALTIME, &ts);
struct tm localTime;
::localtime_r(&ts.tv_sec, &localTime);
std::snprintf(timestamp, sizeof(timestamp), "%04d-%02d-%02d %02d:%02d:%02d",
localTime.tm_year + 1900, localTime.tm_mon + 1,
localTime.tm_mday, localTime.tm_hour, localTime.tm_min,
localTime.tm_sec);
size_t offset = static_cast<size_t>(std::snprintf(
record.data, sizeof(record.data),
"%s.%06ld [%s] [tid=%lu] %s:%d ", timestamp, ts.tv_nsec / 1000,
levelToString(level), currentThreadId(), basenameOf(file), line));
if (offset >= sizeof(record.data)) {
offset = sizeof(record.data) - 1;
}
offset = appendFormatted(record.data, sizeof(record.data), offset, format,
args);
if (savedErrno != 0 && offset < sizeof(record.data)) {
const char *errorString = std::strerror(savedErrno);
const int written = std::snprintf(record.data + offset,
sizeof(record.data) - offset,
" (errno=%d: %s)", savedErrno,
errorString == nullptr ? "unknown"
: errorString);
if (written > 0) {
offset += static_cast<size_t>(written);
if (offset >= sizeof(record.data)) {
offset = sizeof(record.data) - 1;
}
}
}
if (offset >= sizeof(record.data) - 1) {
offset = sizeof(record.data) - 2;
}
record.data[offset++] = '\n';
record.data[offset] = '\0';
record.size = offset;
return record;
}
class AsyncFileLogger {
public:
AsyncFileLogger()
: logFilePath_(defaultLogFilePath()), fd_(-1), running_(false),
stopRequested_(false), droppedMessages_(0) {
pending_.reserve(kInitialBufferedRecords);
}
~AsyncFileLogger() { shutdown(); }
void setLogFile(std::string_view path) {
if (path.empty()) {
return;
}
std::lock_guard<std::mutex> lock(mutex_);
if (running_ || fd_ >= 0) {
return;
}
logFilePath_.assign(path.data(), path.size());
}
void append(LogLevel level, const char *file, int line, int savedErrno,
const char *format, va_list args) {
LogRecord record = formatLogRecord(level, file, line, savedErrno, format,
args);
if (level == LogLevel::kFatal || level == LogLevel::kSysFatal) {
directWrite(record);
return;
}
bool shouldNotify = false;
bool shouldFallback = false;
{
std::lock_guard<std::mutex> lock(mutex_);
startLocked();
if (!running_) {
shouldFallback = true;
} else if (pending_.size() >= kMaxBufferedRecords) {
++droppedMessages_;
} else {
pending_.push_back(record);
shouldNotify = pending_.size() == 1 || pending_.size() >= kNotifyThreshold;
}
}
if (shouldFallback) {
directWrite(record);
return;
}
if (shouldNotify) {
cv_.notify_one();
}
}
void shutdown() {
std::thread worker;
{
std::lock_guard<std::mutex> lock(mutex_);
if (!running_) {
if (fd_ >= 0) {
::close(fd_);
fd_ = -1;
}
return;
}
stopRequested_ = true;
cv_.notify_one();
worker = std::move(worker_);
}
if (worker.joinable()) {
worker.join();
}
std::lock_guard<std::mutex> lock(mutex_);
running_ = false;
stopRequested_ = false;
if (fd_ >= 0) {
::close(fd_);
fd_ = -1;
}
}
private:
void startLocked() {
if (running_) {
return;
}
openLocked();
if (fd_ < 0) {
return;
}
stopRequested_ = false;
running_ = true;
worker_ = std::thread([this] { workerLoop(); });
}
void openLocked() {
if (fd_ >= 0) {
return;
}
fd_ = ::open(logFilePath_.c_str(),
O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
}
void workerLoop() {
std::vector<LogRecord> localRecords;
localRecords.reserve(kInitialBufferedRecords);
for (;;) {
uint64_t dropped = 0;
bool shouldStop = false;
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait_for(lock, kFlushInterval,
[this] { return stopRequested_ || !pending_.empty(); });
pending_.swap(localRecords);
dropped = droppedMessages_;
droppedMessages_ = 0;
shouldStop = stopRequested_ && localRecords.empty() && dropped == 0;
}
if (shouldStop) {
return;
}
if (dropped > 0) {
LogRecord dropRecord{};
const int written = std::snprintf(
dropRecord.data, sizeof(dropRecord.data),
"logger dropped %" PRIu64 " messages because the async queue was "
"full\n",
dropped);
if (written > 0) {
dropRecord.size = static_cast<size_t>(written);
writeRecord(dropRecord);
}
}
for (const LogRecord &record : localRecords) {
writeRecord(record);
}
localRecords.clear();
}
}
void directWrite(const LogRecord &record) {
int targetFd = -1;
{
std::lock_guard<std::mutex> lock(mutex_);
openLocked();
targetFd = fd_;
}
if (targetFd >= 0) {
writeAll(targetFd, record.data, record.size);
return;
}
writeAll(STDERR_FILENO, record.data, record.size);
}
void writeRecord(const LogRecord &record) {
int targetFd = -1;
{
std::lock_guard<std::mutex> lock(mutex_);
targetFd = fd_;
}
if (targetFd >= 0) {
writeAll(targetFd, record.data, record.size);
return;
}
writeAll(STDERR_FILENO, record.data, record.size);
}
std::mutex mutex_;
std::condition_variable cv_;
std::vector<LogRecord> pending_;
std::string logFilePath_;
int fd_;
std::thread worker_;
bool running_;
bool stopRequested_;
uint64_t droppedMessages_;
};
AsyncFileLogger &GetLogger() {
static AsyncFileLogger logger;
return logger;
}
} // namespace
void SetAsyncLogFile(std::string_view path) { GetLogger().setLogFile(path); }
void ShutdownAsyncLogger() { GetLogger().shutdown(); }
namespace detail {
void Log(LogLevel level, const char *file, int line, int savedErrno,
const char *format, ...) {
va_list args;
va_start(args, format);
GetLogger().append(level, file, line, savedErrno, format, args);
va_end(args);
}
} // namespace detail
} // namespace kback