@@ -63,25 +63,31 @@ void AsyncSink::onLogBackEndReadPipe(const void *data_ptr, size_t data_size)
63
63
64
64
bool is_need_flush = false ;
65
65
while (buffer_.readableSize () >= sizeof (LogContent)) {
66
- auto content = reinterpret_cast <LogContent*>(buffer_.readableBegin ());
67
- auto frame_size = sizeof (LogContent) + content->text_len ;
66
+ LogContent content;
67
+ ::memcpy (&content, buffer_.readableBegin(), sizeof(content));
68
+
69
+ auto frame_size = sizeof (LogContent) + content.text_len ;
68
70
if (frame_size > buffer_.readableSize ()) // ! 总结长度不够
69
71
break ;
70
- content->text_ptr = reinterpret_cast <const char *>(content + 1 );
72
+
73
+ buffer_.hasRead (sizeof (content));
74
+
75
+ content.text_ptr = reinterpret_cast <const char *>(buffer_.readableBegin ());
71
76
onLogBackEnd (content);
77
+
72
78
is_need_flush = true ;
73
- buffer_.hasRead (frame_size );
79
+ buffer_.hasRead (content. text_len );
74
80
}
75
81
76
82
if (is_need_flush)
77
83
flushLog ();
78
84
}
79
85
80
- void AsyncSink::onLogBackEnd (const LogContent * content)
86
+ void AsyncSink::onLogBackEnd (const LogContent & content)
81
87
{
82
88
size_t buff_size = 1024 ; // ! 初始大小,可应对绝大数情况
83
89
84
- udpateTimestampStr (content-> timestamp .sec );
90
+ udpateTimestampStr (content. timestamp .sec );
85
91
86
92
// ! 加循环为了应对缓冲不够的情况
87
93
for (;;) {
@@ -95,34 +101,34 @@ void AsyncSink::onLogBackEnd(const LogContent *content)
95
101
96
102
// ! 开启色彩,显示日志等级
97
103
if (enable_color_) {
98
- len = snprintf (WRITE_PTR, REMAIN_SIZE, " \033 [%sm" , LOG_LEVEL_COLOR_CODE[content-> level ]);
104
+ len = snprintf (WRITE_PTR, REMAIN_SIZE, " \033 [%sm" , LOG_LEVEL_COLOR_CODE[content. level ]);
99
105
pos += len;
100
106
}
101
107
102
108
// ! 打印等级、时间戳、线程号、模块名
103
109
len = snprintf (WRITE_PTR, REMAIN_SIZE, " %c %s.%06u %ld %s " ,
104
- LOG_LEVEL_LEVEL_CODE[content-> level ],
105
- timestamp_str_, content-> timestamp .usec ,
106
- content-> thread_id , content-> module_id );
110
+ LOG_LEVEL_LEVEL_CODE[content. level ],
111
+ timestamp_str_, content. timestamp .usec ,
112
+ content. thread_id , content. module_id );
107
113
pos += len;
108
114
109
- if (content-> func_name != nullptr ) {
110
- len = snprintf (WRITE_PTR, REMAIN_SIZE, " %s() " , content-> func_name );
115
+ if (content. func_name != nullptr ) {
116
+ len = snprintf (WRITE_PTR, REMAIN_SIZE, " %s() " , content. func_name );
111
117
pos += len;
112
118
}
113
119
114
- if (content-> text_len > 0 ) {
115
- if (REMAIN_SIZE >= content-> text_len )
116
- memcpy (WRITE_PTR, content-> text_ptr , content-> text_len );
117
- pos += content-> text_len ;
120
+ if (content. text_len > 0 ) {
121
+ if (REMAIN_SIZE >= content. text_len )
122
+ memcpy (WRITE_PTR, content. text_ptr , content. text_len );
123
+ pos += content. text_len ;
118
124
119
125
if (REMAIN_SIZE >= 1 ) // ! 追加一个空格
120
126
*WRITE_PTR = ' ' ;
121
127
++pos;
122
128
}
123
129
124
- if (content-> file_name != nullptr ) {
125
- len = snprintf (WRITE_PTR, REMAIN_SIZE, " -- %s:%d" , content-> file_name , content-> line );
130
+ if (content. file_name != nullptr ) {
131
+ len = snprintf (WRITE_PTR, REMAIN_SIZE, " -- %s:%d" , content. file_name , content. line );
126
132
pos += len;
127
133
}
128
134
0 commit comments