1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "dfx_logger.h"
16
17 #include <cstdio>
18 #include <securec.h>
19 #include <unistd.h>
20 #include "dfx_define.h"
21 #include "dfx_log.h"
22 #include "faultloggerd_client.h"
23
24 static const int WRITE_LOG_BUF_LEN = 2048;
25 static const int32_t INVALID_FD = -1;
26 static int32_t g_DebugLogFilleDes = INVALID_FD;
27 #ifndef DFX_LOG_USE_HILOG_BASE
28 static int32_t g_StdErrFilleDes = INVALID_FD;
29 #endif
30
WriteLog(int32_t fd,const char * format,...)31 int WriteLog(int32_t fd, const char *format, ...)
32 {
33 int ret;
34 char buf[WRITE_LOG_BUF_LEN] = {0};
35 va_list args;
36 va_start(args, format);
37 ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args);
38 if (ret == -1) {
39 DfxLogWarn("WriteLog: vsnprintf_s fail");
40 }
41 va_end(args);
42
43 if (g_DebugLogFilleDes != INVALID_FD) {
44 fprintf(stderr, "%s", buf);
45 }
46
47 if (fd >= 0) {
48 ret = dprintf(fd, "%s", buf);
49 if (ret < 0) {
50 DfxLogError("WriteLog :: write msg(%s) to fd(%d) failed, ret(%d).", buf, fd, ret);
51 }
52 } else if (fd == INVALID_FD) {
53 ret = DfxLogWarn(buf);
54 } else {
55 ret = DfxLogDebug(buf);
56 }
57
58 return ret;
59 }
60
DfxLogToSocket(const char * msg)61 void DfxLogToSocket(const char *msg)
62 {
63 if (CheckDebugLevel()) {
64 return;
65 }
66
67 size_t length = strlen(msg);
68 if (length >= LOG_BUF_LEN) {
69 return;
70 }
71 RequestPrintTHilog(msg, length);
72 }
73
InitDebugLog(int type,int pid,int tid,unsigned int uid)74 void InitDebugLog(int type, int pid, int tid, unsigned int uid)
75 {
76 DfxLogInfo("InitDebugLog :: type(%d), pid(%d), tid(%d), uid(%d).", type, pid, tid, uid);
77 if (g_DebugLogFilleDes != INVALID_FD) {
78 return;
79 }
80
81 struct FaultLoggerdRequest faultloggerdRequest;
82 if (memset_s(&faultloggerdRequest, sizeof(faultloggerdRequest), 0, sizeof(struct FaultLoggerdRequest)) != 0) {
83 return;
84 }
85 faultloggerdRequest.type = (int)type;
86 faultloggerdRequest.pid = pid;
87 faultloggerdRequest.tid = tid;
88 faultloggerdRequest.uid = uid;
89
90 g_DebugLogFilleDes = RequestLogFileDescriptor(&faultloggerdRequest);
91 if (g_DebugLogFilleDes <= 0) {
92 DfxLogError("InitDebugLog :: RequestLogFileDescriptor failed.");
93 g_DebugLogFilleDes = INVALID_FD;
94 } else {
95 g_StdErrFilleDes = dup(STDERR_FILENO);
96
97 if (dup2(g_DebugLogFilleDes, STDERR_FILENO) == -1) {
98 DfxLogError("InitDebugLog :: dup2 failed.");
99 close(g_DebugLogFilleDes);
100 g_DebugLogFilleDes = INVALID_FD;
101 g_StdErrFilleDes = INVALID_FD;
102 }
103 }
104
105 InitDebugFd(g_DebugLogFilleDes);
106 }
107
CloseDebugLog()108 void CloseDebugLog()
109 {
110 if (g_DebugLogFilleDes == INVALID_FD) {
111 return;
112 }
113
114 if (g_StdErrFilleDes != INVALID_FD) {
115 dup2(g_StdErrFilleDes, STDERR_FILENO);
116 }
117 close(g_DebugLogFilleDes);
118 g_DebugLogFilleDes = INVALID_FD;
119 g_StdErrFilleDes = INVALID_FD;
120
121 InitDebugFd(g_DebugLogFilleDes);
122 }