1 /*
2 * Copyright (c) 2022-2024 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 #ifndef is_ohos_lite
26 static int32_t g_DebugLogFd = INVALID_FD;
27 static int32_t g_StdErrFd = INVALID_FD;
28 #endif
29
WriteLog(int32_t fd,const char * format,...)30 int WriteLog(int32_t fd, const char *format, ...)
31 {
32 int ret = -1;
33 char buf[WRITE_LOG_BUF_LEN] = {0};
34 va_list args;
35 va_start(args, format);
36 ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args);
37 if (ret == -1) {
38 DFXLOG_WARN("%s", "WriteLog: vsnprintf_s fail");
39 }
40 va_end(args);
41
42 #ifndef is_ohos_lite
43 if (g_DebugLogFd != INVALID_FD) {
44 fprintf(stderr, "%s", buf);
45 }
46 #endif
47
48 if (fd >= 0) {
49 ret = dprintf(fd, "%s", buf);
50 if (ret < 0) {
51 DFXLOG_ERROR("WriteLog :: write msg(%s) to fd(%d) failed, ret(%d).", buf, fd, ret);
52 }
53 } else if (fd == INVALID_FD) {
54 DFXLOG_WARN("%s", buf);
55 } else {
56 DFXLOG_DEBUG("%s", buf);
57 }
58
59 return ret;
60 }
61
DfxLogToSocket(const char * msg)62 void DfxLogToSocket(const char *msg)
63 {
64 if (CheckDebugLevel()) {
65 return;
66 }
67
68 size_t length = strlen(msg);
69 if (length >= LOG_BUF_LEN) {
70 return;
71 }
72 int ret = RequestPrintTHilog(msg, length);
73 if (ret < 0) {
74 DFXLOG_ERROR("DfxLogToSocket :: request print msg(%s) failed, ret(%d).", msg, ret);
75 }
76 }
77
InitDebugLog(int type,int pid,int tid,unsigned int uid)78 void InitDebugLog(int type, int pid, int tid, unsigned int uid)
79 {
80 #ifndef is_ohos_lite
81 DFXLOG_INFO("InitDebugLog :: type(%d), pid(%d), tid(%d), uid(%d).", type, pid, tid, uid);
82 if (g_DebugLogFd != INVALID_FD) {
83 return;
84 }
85
86 struct FaultLoggerdRequest faultloggerdRequest;
87 (void)memset_s(&faultloggerdRequest, sizeof(faultloggerdRequest), 0, sizeof(struct FaultLoggerdRequest));
88 faultloggerdRequest.type = (int)type;
89 faultloggerdRequest.pid = pid;
90 faultloggerdRequest.tid = tid;
91 faultloggerdRequest.uid = uid;
92
93 g_DebugLogFd = RequestLogFileDescriptor(&faultloggerdRequest);
94 if (g_DebugLogFd <= 0) {
95 DFXLOG_ERROR("%s", "InitDebugLog :: RequestLogFileDescriptor failed.");
96 g_DebugLogFd = INVALID_FD;
97 } else {
98 g_StdErrFd = dup(STDERR_FILENO);
99
100 if (dup2(g_DebugLogFd, STDERR_FILENO) == -1) {
101 DFXLOG_ERROR("%s", "InitDebugLog :: dup2 failed.");
102 close(g_DebugLogFd);
103 g_DebugLogFd = INVALID_FD;
104 g_StdErrFd = INVALID_FD;
105 }
106 }
107
108 InitDebugFd(g_DebugLogFd);
109 #endif
110 }
111
CloseDebugLog()112 void CloseDebugLog()
113 {
114 #ifndef is_ohos_lite
115 if (g_DebugLogFd == INVALID_FD) {
116 return;
117 }
118
119 if (g_StdErrFd != INVALID_FD) {
120 dup2(g_StdErrFd, STDERR_FILENO);
121 }
122 close(g_DebugLogFd);
123 g_DebugLogFd = INVALID_FD;
124 g_StdErrFd = INVALID_FD;
125
126 InitDebugFd(g_DebugLogFd);
127 #endif
128 }
129