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