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
WriteLog(int32_t fd, const char *format, ...)26 int WriteLog(int32_t fd, const char *format, ...)
27 {
28 int ret = -1;
29 char buf[WRITE_LOG_BUF_LEN] = {0};
30 va_list args;
31 va_start(args, format);
32 ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, args);
33 if (ret == -1) {
34 DFXLOGW("WriteLog: vsnprintf_s fail");
35 }
36 va_end(args);
37
38 if (fd >= 0) {
39 ret = dprintf(fd, "%s", buf);
40 if (ret < 0) {
41 DFXLOGE("WriteLog :: write msg(%{public}s) to fd(%{public}d) failed, ret(%{public}d).", buf, fd, ret);
42 }
43 } else if (fd == INVALID_FD) {
44 DFXLOGW("%{public}s", buf);
45 } else {
46 DFXLOGD("%{public}s", buf);
47 }
48
49 return ret;
50 }
51
DfxLogToSocket(const char *msg)52 void DfxLogToSocket(const char *msg)
53 {
54 size_t length = strlen(msg);
55 if (length >= LOG_BUF_LEN) {
56 return;
57 }
58 int ret = RequestPrintTHilog(msg, length);
59 if (ret < 0) {
60 DFXLOGE("DfxLogToSocket :: request print msg(%{public}s) failed, ret(%{public}d).", msg, ret);
61 }
62 }
63