1 /*
2 * Copyright (c) 2021-2023 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
16 #include "dfx_log.h"
17
18 #ifdef DFX_LOG_USE_HILOG_BASE
19 #include <hilog_base/log_base.h>
20 #else
21 #include <hilog/log.h>
22 #endif
23 #include <cstdarg>
24 #include <cstdio>
25 #include <securec.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include "dfx_define.h"
29
30 static int g_debugFd = INVALID_FD;
31 static const Level CURRENT_LOG_LEVEL = Level::INFO;
32 #ifdef DFX_LOG_USE_DMESG
33 static int g_dmesgFd = INVALID_FD;
34 #endif
35
36 #ifdef DFX_LOG_USE_DMESG
LogToDmesg(Level logLevel,const char * tag,const char * info)37 static void LogToDmesg(Level logLevel, const char *tag, const char *info)
38 {
39 static const char *LOG_LEVEL_STR[] = { "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" };
40 static const char *LOG_KLEVEL_STR[] = { "<7>", "<6>", "<4>", "<3>", "<3>" };
41
42 if (UNLIKELY(g_dmesgFd < 0)) {
43 g_dmesgFd = open("/dev/kmsg", O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
44 }
45 char buf[LOG_BUF_LEN] = {0};
46 if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "%s[pid=%d %d][%s][%s]%s",
47 LOG_KLEVEL_STR[logLevel], getpid(), getppid(), tag, LOG_LEVEL_STR[logLevel], info) == -1) {
48 close(g_dmesgFd);
49 g_dmesgFd = -1;
50 return;
51 }
52 if (write(g_dmesgFd, buf, strlen(buf)) < 0) {
53 close(g_dmesgFd);
54 g_dmesgFd = -1;
55 }
56 }
57 #endif
58
InitDebugFd(int fd)59 void InitDebugFd(int fd)
60 {
61 g_debugFd = fd;
62 }
63
CheckDebugLevel(void)64 bool CheckDebugLevel(void)
65 {
66 return Level::DEBUG >= CURRENT_LOG_LEVEL ? true : false;
67 }
68
DfxLog(const Level logLevel,const unsigned int domain,const char * tag,const char * fmt,...)69 int DfxLog(const Level logLevel, const unsigned int domain, const char* tag, const char *fmt, ...)
70 {
71 if (logLevel < CURRENT_LOG_LEVEL) {
72 return -1;
73 }
74
75 int ret;
76 char buf[LOG_BUF_LEN] = {0};
77 va_list args;
78 va_start(args, fmt);
79 ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, fmt, args);
80 va_end(args);
81 #ifdef DFX_LOG_USE_HILOG_BASE
82 static const LogLevel LOG_LEVEL[] = { LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
83 if ((logLevel < Level::DEBUG) || (logLevel > Level::FATAL)) {
84 return -1;
85 }
86 HiLogBasePrint(LOG_CORE, LOG_LEVEL[logLevel], domain, tag, "%{public}s", buf);
87 #else
88 OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, domain, tag};
89 switch (static_cast<int>(logLevel)) {
90 case static_cast<int>(DEBUG):
91 OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, "%{public}s", buf);
92 break;
93 case static_cast<int>(INFO):
94 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s", buf);
95 break;
96 case static_cast<int>(WARN):
97 OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, "%{public}s", buf);
98 break;
99 case static_cast<int>(ERROR):
100 OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s", buf);
101 break;
102 case static_cast<int>(FATAL):
103 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "%{public}s", buf);
104 break;
105 default:
106 break;
107 }
108 #endif
109
110 #ifdef DFX_LOG_USE_DMESG
111 LogToDmesg(logLevel, tag, buf);
112 #endif
113 if (g_debugFd != INVALID_FD) {
114 fprintf(stderr, "%s\n", buf);
115 }
116 return ret;
117 }
118