1 /*
2 * Copyright (c) 2020 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 <string.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include "ohos_init.h"
20 #include "hiview_def.h"
21 #include "hiview_util.h"
22 #include "hiview_config.h"
23 #include "hiview_service.h"
24 #include "hiview_log.h"
25 #include "hiview_log_limit.h"
26 #include "hiview_output_log.h"
27
28 #define LOG_IS_OUTPUT(mod) (g_hiviewConfig.logOutputModule & (((uint64_t)1) << (mod)))
29
30 static HiLogModuleInfo g_logModuleInfo[HILOG_MODULE_MAX];
31 const char * const FUN_ARG_S = "0123456I";
32
33 static boolean CheckParameters(uint8 module, uint8 level);
34
35 /* The first step does not involve memory allocation. */
HiLogInit(void)36 void HiLogInit(void)
37 {
38 HIVIEW_UartPrint("hilog will init.\n");
39 InitCoreLogOutput();
40
41 /* The module that is not registered cannot print the log. */
42 if (HiLogRegisterModule(HILOG_MODULE_HIVIEW, "HIVIEW") == FALSE ||
43 HiLogRegisterModule(HILOG_MODULE_SAMGR, "SAMGR") == FALSE ||
44 HiLogRegisterModule(HILOG_MODULE_UPDATE, "UPDATE") == FALSE ||
45 HiLogRegisterModule(HILOG_MODULE_ACE, "ACE") == FALSE ||
46 HiLogRegisterModule(HILOG_MODULE_AAFWK, "AAFWK") == FALSE ||
47 HiLogRegisterModule(HILOG_MODULE_APP, "APP") == FALSE ||
48 HiLogRegisterModule(HILOG_MODULE_GRAPHIC, "GRAPHIC") == FALSE ||
49 HiLogRegisterModule(HILOG_MODULE_MEDIA, "MEDIA") == FALSE ||
50 HiLogRegisterModule(HILOG_MODULE_DMS, "DMS") == FALSE ||
51 HiLogRegisterModule(HILOG_MODULE_SEN, "SEN") == FALSE ||
52 HiLogRegisterModule(HILOG_MODULE_SCY, "SCY") == FALSE ||
53 HiLogRegisterModule(HILOG_MODULE_SOFTBUS, "SOFTBUS") == FALSE ||
54 HiLogRegisterModule(HILOG_MODULE_POWERMGR, "POWERMGR") == FALSE ||
55 HiLogRegisterModule(HILOG_MODULE_UIKIT, "UIKIT") == FALSE ||
56 HiLogRegisterModule(HILOG_MODULE_GLOBAL, "GLOBAL") == FALSE ||
57 HiLogRegisterModule(HILOG_MODULE_DATAMGR, "DATAMGR") == FALSE) {
58 return;
59 }
60
61 HiviewRegisterInitFunc(HIVIEW_CMP_TYPE_LOG, InitLogOutput);
62 HiviewRegisterInitFunc(HIVIEW_CMP_TYPE_LOG_LIMIT, InitLogLimit);
63 HILOG_DEBUG(HILOG_MODULE_HIVIEW, "hilog init success.");
64 }
65 #ifndef DISABLE_HILOG_LITE_CORE_INIT
66 CORE_INIT_PRI(HiLogInit, 0);
67 #endif
68
CheckParameters(uint8 module,uint8 level)69 static boolean CheckParameters(uint8 module, uint8 level)
70 {
71 if ((level < g_hiviewConfig.level) || (level < HILOG_COMPILE_LEVEL) ||
72 (module >= HILOG_MODULE_MAX) || (g_logModuleInfo[module].name == NULL)) {
73 return FALSE;
74 }
75
76 return TRUE;
77 }
78
HiLogRegisterModule(uint16 id,const char * name)79 boolean HiLogRegisterModule(uint16 id, const char *name)
80 {
81 if ((id >= HILOG_MODULE_MAX) || name == NULL || g_logModuleInfo[id].name != NULL) {
82 return FALSE;
83 }
84
85 uint32 len = (uint32)strnlen(name, LOG_MODULE_NAME_LEN + 1);
86 if (len >= LOG_MODULE_NAME_LEN - 1) {
87 return FALSE;
88 }
89 uint32 i = 0;
90 while (i < len && name[i] != 0) {
91 if (!((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z'))) {
92 return FALSE;
93 }
94 i++;
95 }
96
97 g_logModuleInfo[id].name = name;
98 g_logModuleInfo[id].id = id;
99 return TRUE;
100 }
101
HiLogGetModuleName(uint8 id)102 const char *HiLogGetModuleName(uint8 id)
103 {
104 if (id >= HILOG_MODULE_MAX) {
105 return "";
106 }
107 const char *name = g_logModuleInfo[id].name;
108 return (name == NULL) ? "" : name;
109 }
110
HiLogPrintf(uint8 module,uint8 level,const char * nums,const char * fmt,...)111 void HiLogPrintf(uint8 module, uint8 level, const char *nums, const char *fmt, ...)
112 {
113 static char newFmt[] = "The number of parameters is invalid.";
114 HiLogContent logContent = { 0 };
115 int32 argsNum = (nums - FUN_ARG_S);
116 if (argsNum < 0 || argsNum > LOG_MULTI_PARA_MAX) {
117 fmt = newFmt;
118 argsNum = 0;
119 }
120
121 if (g_hiviewConfig.logSwitch == HIVIEW_FEATURE_OFF || !CheckParameters(module, level) ||
122 !LOG_IS_OUTPUT(module)) {
123 return;
124 }
125
126 HiLogCommon *pCommon = &(logContent.commonContent);
127 pCommon->head = LOG_INFO_HEAD;
128 pCommon->module = module;
129 pCommon->level = level;
130 pCommon->fmt = fmt;
131 pCommon->valueNumber = (uint8)argsNum;
132 pCommon->task = (uint8)HIVIEW_GetTaskId();
133 uint64 cur = HIVIEW_GetCurrentTime();
134 pCommon->time = (uint32)(cur / MS_PER_SECOND);
135 pCommon->milli = (uint16)(cur % MS_PER_SECOND);
136
137 uint8 i = 0;
138 va_list args;
139 va_start(args, fmt);
140 while (i < argsNum) {
141 logContent.values[i++] = va_arg(args, uint32);
142 }
143 va_end(args);
144
145 OutputLog((uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum);
146 }
147
HILOG_HashPrintf(uint8 module,uint8 level,const char * nums,uint32 hash,...)148 void HILOG_HashPrintf(uint8 module, uint8 level, const char *nums, uint32 hash, ...)
149 {
150 HiLogContent logContent = {0};
151 int32 argsNum = (nums - FUN_ARG_S);
152 if (argsNum < 0 || argsNum > LOG_MULTI_PARA_MAX) {
153 argsNum = 0;
154 }
155
156 if (g_hiviewConfig.logSwitch == HIVIEW_FEATURE_OFF || !CheckParameters(module, level) ||
157 !LOG_IS_OUTPUT(module)) {
158 return;
159 }
160
161 HiLogCommon *pCommon = &(logContent.commonContent);
162 pCommon->head = LOG_INFO_HEAD;
163 pCommon->module = module;
164 pCommon->level = SET_HASH_FLAG(level);
165 pCommon->fmt = (const char*)hash;
166 pCommon->valueNumber = (uint8)argsNum;
167 pCommon->task = (uint8)HIVIEW_GetTaskId();
168 uint64 cur = HIVIEW_GetCurrentTime();
169 pCommon->time = (uint32)(cur / MS_PER_SECOND);
170 pCommon->milli = (uint16)(cur % MS_PER_SECOND);
171
172 uint8 i = 0;
173 va_list args;
174 va_start(args, hash);
175 while (i < argsNum) {
176 logContent.values[i++] = va_arg(args, uint32);
177 }
178 va_end(args);
179
180 OutputLog((uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum);
181 }
182
HiLogFlush(boolean syncFlag)183 void HiLogFlush(boolean syncFlag)
184 {
185 FlushLog(syncFlag);
186 }
187
HiLogGetConfigOption(void)188 uint32 HiLogGetConfigOption(void)
189 {
190 return HiviewGetConfigOption();
191 }
192
HiLogRegisterProc(HilogProc func)193 void HiLogRegisterProc(HilogProc func)
194 {
195 HiviewRegisterHilogProc(func);
196 }
197
HiLogUnRegisterProc(HilogProc func)198 void HiLogUnRegisterProc(HilogProc func)
199 {
200 HiviewUnRegisterHilogProc(func);
201 }
202
HiLogFileAddWatcher(FileProc func,const char * path)203 void HiLogFileAddWatcher(FileProc func, const char *path)
204 {
205 HiviewRegisterHiLogFileWatcher(func, path);
206 }
207
HiLogFileRemoveWatcher(FileProc func)208 void HiLogFileRemoveWatcher(FileProc func)
209 {
210 HiviewUnRegisterHiLogFileWatcher(func);
211 }
212
HiLogFileProc(const char * dest,uint8 mode)213 int HiLogFileProc(const char *dest, uint8 mode)
214 {
215 return HiLogFileProcImp(dest, mode);
216 }
217
HiLogOutputFileLock(void)218 void HiLogOutputFileLock(void)
219 {
220 HiLogOutputFileLockImp();
221 }
222
HiLogOutputFileUnLock(void)223 void HiLogOutputFileUnLock(void)
224 {
225 HiLogOutputFileUnLockImp();
226 }
227
HiLogGetLogLevel(void)228 uint32 HiLogGetLogLevel(void)
229 {
230 return g_hiviewConfig.level;
231 }
232
HiLogSetLogLevel(uint8 level)233 boolean HiLogSetLogLevel(uint8 level)
234 {
235 if (level >= HILOG_LV_DEBUG && level < HILOG_LV_MAX) {
236 g_hiviewConfig.level = level;
237 printf("Set log level: %d\n", level);
238 return TRUE;
239 }
240 return FALSE;
241 }
242