forked from Tencent/Pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
383 lines (320 loc) · 9.23 KB
/
Copy pathlog.cpp
File metadata and controls
383 lines (320 loc) · 9.23 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
374
375
376
377
378
379
380
381
382
/*
* Tencent is pleased to support the open source community by making Pebble available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
#include <errno.h>
#include <execinfo.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sstream>
#include <string>
#include "common/file_util.h"
#include "common/log.h"
#include "common/string_utility.h"
#include "common/time_utility.h"
namespace pebble {
#define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
static const char* g_device_str[] = { "STDOUT", "FILE" };
static const char* g_priority_str[] = { "TRACE", "DEBUG", "INFO", "ERROR", "FATAL" };
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 信号处理部分:
static const struct {
int number;
const char* name;
} g_failure_signals[] = {
{ SIGSEGV, "SIGSEGV" },
{ SIGILL, "SIGILL" },
{ SIGFPE, "SIGFPE" },
{ SIGABRT, "SIGABRT" },
{ SIGBUS, "SIGBUS" },
{ SIGTERM, "SIGTERM" }
};
static struct sigaction g_sigaction_bak[ARRAYSIZE(g_failure_signals)];
// 获取调用栈
int GetStackTrace(void** result, int max_depth, int skip_num) {
static const int stack_len = 64;
void * stack[stack_len] = {0};
int size = backtrace(stack, stack_len);
int remain = size - (++skip_num);
if (remain < 0) {
remain = 0;
}
if (remain > max_depth) {
remain = max_depth;
}
for (int i = 0; i < remain; i++) {
result[i] = stack[i + skip_num];
}
return remain;
}
// 信号处理
void SignalHandler(int signum, siginfo_t* siginfo, void* ucontext)
{
// 获取时间
std::ostringstream oss;
oss << "[" << TimeUtility::GetStringTimeDetail() << "]";
// 获取信号名
const char* signame = "";
uint32_t i = 0;
for (; i < ARRAYSIZE(g_failure_signals); i++) {
if (g_failure_signals[i].number == signum) {
signame = g_failure_signals[i].name;
break;
}
}
oss << "[(SIGNAL)(" << signame << ")][FATAL]";
// 获取栈
void* stack[32] = {0};
int depth = GetStackTrace(stack, ARRAYSIZE(stack), 1);
char** symbols = backtrace_symbols(stack, depth);
oss << "\nstack trace:\n";
for (int i = 0; i < depth; i++) {
oss << "#" << i << " " << symbols[i] << "\n";
}
oss << "\n";
// 输出
PLOG_FATAL("%s", oss.str().c_str());
// 恢复默认处理
sigaction(signum, &g_sigaction_bak[i], NULL);
kill(getpid(), signum);
}
// 注册信号处理
void InstallSignalHandler()
{
static bool _installed = false;
if (_installed) {
return;
}
_installed = true;
struct sigaction sig_action;
memset(&sig_action, 0, sizeof(sig_action));
sigemptyset(&sig_action.sa_mask);
sig_action.sa_flags |= SA_SIGINFO;
sig_action.sa_sigaction = &SignalHandler;
for (uint32_t i = 0; i < ARRAYSIZE(g_failure_signals); i++) {
sigaction(g_failure_signals[i].number, &sig_action, &g_sigaction_bak[i]);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// log实现部分:
Log::Log() {
m_device_type = DEV_FILE;
m_log_priority = LOG_PRIORITY_INFO;
std::string self_name = GetSelfName();
m_log_array[kLOG_LOG] = new RollUtil("./log", self_name + ".log");
m_log_array[kLOG_ERROR] = new RollUtil("./log", self_name + ".error");
m_log_array[kLOG_STAT] = new RollUtil("./log", self_name + ".stat");
m_isset_time = false;
m_current_time = TimeUtility::GetCurrentUS();
}
Log::Log(const Log& rhs) {
m_device_type = DEV_FILE;
m_log_priority = LOG_PRIORITY_INFO;
for (int i = 0; i < kLOG_BUTT; i++) {
m_log_array[i] = NULL;
}
m_isset_time = false;
m_current_time = 0;
}
Log::~Log() {
for (int i = 0; i < kLOG_BUTT; i++) {
delete m_log_array[i];
m_log_array[i] = NULL;
}
}
void Log::Write(LOG_PRIORITY pri, const char* file, uint32_t line,
const char* function, const char* fmt, ...)
{
// 优先级以PLOG为准,避免多做格式化
if (pri < m_log_priority) {
return;
}
static char buff[4096] = {0};
// log前缀,接入其他log时不用组装
int pre_len = 0;
if (!m_log_write_func) {
pre_len = snprintf(buff, ARRAYSIZE(buff), "[%s][%d][(%s:%d)(%s)][%s] ",
TimeUtility::GetStringTimeDetail(),
getpid(),
file,
line,
function,
g_priority_str[pri]
);
if (pre_len < 0) {
pre_len = 0;
}
}
va_list ap;
va_start(ap, fmt);
int len = vsnprintf(buff + pre_len, ARRAYSIZE(buff) - pre_len, fmt, ap);
va_end(ap);
if (len < 0) {
len = 0;
}
// 输出到其他log
if (m_log_write_func) {
m_log_write_func(pri, file, line, function, buff);
return;
}
// 其他log以有'\n',PLOG需要再补换行
uint32_t tail = len + pre_len;
if (tail > (ARRAYSIZE(buff) - 2)) {
tail = ARRAYSIZE(buff) - 2;
}
buff[tail++] = '\n';
buff[tail] = '\0';
// 输出到stdout
if (DEV_STDOUT == m_device_type) {
fprintf(stdout, "%s", buff);
return;
}
// 输出到ERROR文件
if (pri >= LOG_PRIORITY_ERROR && m_log_array[kLOG_ERROR] != NULL) {
FILE* error = m_log_array[kLOG_ERROR]->GetFile();
if (error != NULL) {
fwrite(buff, tail, 1, error);
}
}
// 输出到log文件
if (m_log_array[kLOG_LOG] != NULL) {
FILE* log = m_log_array[kLOG_LOG]->GetFile();
if (log != NULL) {
fwrite(buff, tail, 1, log);
}
}
}
void Log::Write(const char* data)
{
if (m_log_array[kLOG_STAT] == NULL) {
return;
}
FILE* stat = m_log_array[kLOG_STAT]->GetFile();
if (stat != NULL) {
char buff[64] = {0};
int len = snprintf(buff, ARRAYSIZE(buff), "[%s] ", TimeUtility::GetStringTimeDetail());
fwrite(buff, len, 1, stat);
fwrite(data, strlen(data), 1, stat);
}
}
void Log::Close()
{
for (int i = 0; i < kLOG_BUTT; i++) {
if (m_log_array[i]) {
m_log_array[i]->Close();
}
}
}
void Log::RegisterLogWriteFunc(const LogWriteFunc& log_write_func)
{
m_log_write_func = log_write_func;
}
void Log::EnableCrashRecord()
{
InstallSignalHandler();
}
void Log::Flush()
{
for (int i = 0; i < kLOG_BUTT; i++) {
if (m_log_array[i]) {
m_log_array[i]->Flush();
}
}
}
int Log::SetOutputDevice(DEVICE_TYPE device)
{
if (device < DEV_STDOUT || device > DEV_FILE) {
return -1;
}
m_device_type = device;
return 0;
}
int Log::SetOutputDevice(const std::string& device)
{
std::string tmp(device);
StringUtility::ToUpper(&tmp);
for (uint32_t i = 0; i < ARRAYSIZE(g_device_str); i++) {
if (g_device_str[i] == tmp) {
m_device_type = static_cast<DEVICE_TYPE>(i);
return 0;
}
}
return -1;
}
int Log::SetLogPriority(LOG_PRIORITY priority)
{
if (priority < LOG_PRIORITY_TRACE || priority > LOG_PRIORITY_FATAL) {
return -1;
}
m_log_priority = priority;
return 0;
}
int Log::SetLogPriority(const std::string& priority)
{
std::string tmp(priority);
StringUtility::ToUpper(&tmp);
for (uint32_t i = 0; i < ARRAYSIZE(g_priority_str); i++) {
if (g_priority_str[i] == tmp) {
m_log_priority = static_cast<LOG_PRIORITY>(i);
return 0;
}
}
return -1;
}
void Log::SetMaxFileSize(uint32_t max_size_Mbytes)
{
uint32_t file_size = max_size_Mbytes;
if (max_size_Mbytes < 1) {
file_size = 1;
}
file_size = file_size * 1024 * 1024;
for (int i = 0; i < kLOG_BUTT; i++) {
if (m_log_array[i]) {
m_log_array[i]->SetFileSize(file_size);
}
}
}
void Log::SetMaxRollNum(uint32_t num)
{
for (int i = 0; i < kLOG_BUTT; i++) {
if (m_log_array[i]) {
m_log_array[i]->SetRollNum(num);
}
}
}
void Log::SetFilePath(const std::string& file_path)
{
for (int i = 0; i < kLOG_BUTT; i++) {
if (m_log_array[i]) {
m_log_array[i]->SetFilePath(file_path);
}
}
// 更改路径后log立即输出在新的路径下面
Close();
}
void Log::SetCurrentTime(int64_t timestamp) {
m_current_time = timestamp;
m_isset_time = true;
}
int64_t Log::GetCurrentTime() {
if (m_isset_time) {
return m_current_time;
}
return TimeUtility::GetCurrentUS();
}
} // namespace pebble