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