-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathasync_sink.cpp
151 lines (122 loc) · 3.91 KB
/
async_sink.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "async_sink.h"
#include <cstring>
#include <algorithm>
#include <iostream>
#include <chrono>
namespace tbox {
namespace log {
void AsyncSink::cleanup()
{
if (is_pipe_inited_)
async_pipe_.cleanup();
}
void AsyncSink::onEnable()
{
if (!async_pipe_.initialize(cfg_))
return;
using namespace std::placeholders;
async_pipe_.setCallback(std::bind(&AsyncSink::onLogBackEndReadPipe, this, _1, _2));
is_pipe_inited_ = true;
}
void AsyncSink::onDisable()
{
async_pipe_.cleanup();
is_pipe_inited_ = false;
}
void AsyncSink::onLogFrontEnd(const LogContent *content)
{
async_pipe_.append(content, sizeof(LogContent));
if (content->text_len != 0)
async_pipe_.append(content->text_ptr, content->text_len);
}
void AsyncSink::onLogBackEndReadPipe(const void *data_ptr, size_t data_size)
{
auto start_ts = std::chrono::steady_clock::now();
buffer_.append(data_ptr, data_size);
bool is_need_flush = false;
while (buffer_.readableSize() >= sizeof(LogContent)) {
LogContent content;
::memcpy(&content, buffer_.readableBegin(), sizeof(content));
auto frame_size = sizeof(LogContent) + content.text_len;
if (frame_size > buffer_.readableSize()) //! 总结长度不够
break;
buffer_.hasRead(sizeof(content));
content.text_ptr = reinterpret_cast<const char*>(buffer_.readableBegin());
onLogBackEnd(content);
is_need_flush = true;
buffer_.hasRead(content.text_len);
}
if (is_need_flush)
flush();
auto time_cost = std::chrono::steady_clock::now() - start_ts;
if (time_cost > std::chrono::milliseconds(500))
std::cerr << timestamp_str_ << " NOTICE: log sink cost > 500 ms, " << time_cost.count() / 1000 << " us" << std::endl;
}
void AsyncSink::onLogBackEnd(const LogContent &content)
{
char buff[1024];
size_t len = 0;
updateTimestampStr(content.timestamp.sec);
//! 开启色彩,显示日志等级
if (enable_color_) {
len = snprintf(buff, sizeof(buff), "\033[%sm", LOG_LEVEL_COLOR_CODE[content.level]);
append(buff, len);
}
//! 打印等级、时间戳、线程号、模块名
len = snprintf(buff, sizeof(buff), "%c %s.%06u %ld %s ",
LOG_LEVEL_LEVEL_CODE[content.level],
timestamp_str_, content.timestamp.usec,
content.thread_id, content.module_id);
append(buff, len);
if (content.func_name != nullptr) {
len = snprintf(buff, sizeof(buff), "%s() ", content.func_name);
append(buff, len);
}
if (content.text_len > 0) {
append(content.text_ptr, content.text_len);
append(' '); //! 追加空格
if (content.text_trunc) {
const char *tip = "(TRUNCATED) ";
append(tip, ::strlen(tip));
}
}
if (content.file_name != nullptr) {
len = snprintf(buff, sizeof(buff), "-- %s:%d", content.file_name, content.line);
append(buff, len);
}
if (enable_color_) {
append("\033[0m", 4);
}
endline();
}
void AsyncSink::append(const char *str, size_t len)
{
cache_.reserve(cache_.size() + len);
std::back_insert_iterator<std::vector<char>> back_insert_iter(cache_);
std::copy(str, str + len, back_insert_iter);
}
void AsyncSink::append(char ch)
{
cache_.push_back(ch);
}
}
}