• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static 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 CORE_INIT_PRI(HiLogInit, 0);
66 
CheckParameters(uint8 module,uint8 level)67 static boolean CheckParameters(uint8 module, uint8 level)
68 {
69     if ((level < g_hiviewConfig.level) ||  (level < HILOG_COMPILE_LEVEL) ||
70         (module >= HILOG_MODULE_MAX) || (g_logModuleInfo[module].name == NULL)) {
71         return FALSE;
72     }
73 
74     return TRUE;
75 }
76 
HiLogRegisterModule(uint16 id,const char * name)77 boolean HiLogRegisterModule(uint16 id, const char *name)
78 {
79     if ((id >= HILOG_MODULE_MAX) || name == NULL || g_logModuleInfo[id].name != NULL) {
80         return FALSE;
81     }
82 
83     uint32 len = (uint32)strnlen(name, LOG_MODULE_NAME_LEN + 1);
84     if (len >= LOG_MODULE_NAME_LEN - 1) {
85         return FALSE;
86     }
87     uint32 i = 0;
88     while (i < len && name[i] != 0) {
89         if (!((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z'))) {
90             return FALSE;
91         }
92         i++;
93     }
94 
95     g_logModuleInfo[id].name = name;
96     g_logModuleInfo[id].id = id;
97     return TRUE;
98 }
99 
HiLogGetModuleName(uint8 id)100 const char *HiLogGetModuleName(uint8 id)
101 {
102     if (id >= HILOG_MODULE_MAX) {
103         return "";
104     }
105     const char *name = g_logModuleInfo[id].name;
106     return (name == NULL) ? "" : name;
107 }
108 
HiLogPrintf(uint8 module,uint8 level,const char * nums,const char * fmt,...)109 void HiLogPrintf(uint8 module, uint8 level, const char *nums, const char *fmt, ...)
110 {
111     static char newFmt[] = "The number of parameters is invalid.";
112     HiLogContent logContent = { 0 };
113     int32 argsNum = (nums - FUN_ARG_S);
114     if (argsNum < 0 || argsNum > LOG_MULTI_PARA_MAX) {
115         fmt = newFmt;
116         argsNum = 0;
117     }
118 
119     if (g_hiviewConfig.logSwitch == HIVIEW_FEATURE_OFF || !CheckParameters(module, level) ||
120         !LOG_IS_OUTPUT(module)) {
121         return;
122     }
123 
124     HiLogCommon *pCommon = &(logContent.commonContent);
125     pCommon->head = LOG_INFO_HEAD;
126     pCommon->module = module;
127     pCommon->level = level;
128     pCommon->fmt = fmt;
129     pCommon->valueNumber = (uint8)argsNum;
130     pCommon->task = (uint8)HIVIEW_GetTaskId();
131     uint64 cur = HIVIEW_GetCurrentTime();
132     pCommon->time = (uint32)(cur / MS_PER_SECOND);
133     pCommon->milli = (uint16)(cur % MS_PER_SECOND);
134 
135     uint8 i = 0;
136     va_list args;
137     va_start(args, fmt);
138     while (i < argsNum) {
139         logContent.values[i++] = va_arg(args, uint32);
140     }
141     va_end(args);
142 
143     OutputLog((uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum);
144 }
145 
HILOG_HashPrintf(uint8 module,uint8 level,const char * nums,uint32 hash,...)146 void HILOG_HashPrintf(uint8 module, uint8 level, const char *nums, uint32 hash, ...)
147 {
148     HiLogContent logContent = {0};
149     int32 argsNum = (nums - FUN_ARG_S);
150     if (argsNum < 0 || argsNum > LOG_MULTI_PARA_MAX) {
151         argsNum = 0;
152     }
153 
154     if (g_hiviewConfig.logSwitch == HIVIEW_FEATURE_OFF || !CheckParameters(module, level) ||
155         !LOG_IS_OUTPUT(module)) {
156         return;
157     }
158 
159     HiLogCommon *pCommon = &(logContent.commonContent);
160     pCommon->head = LOG_INFO_HEAD;
161     pCommon->module = module;
162     pCommon->level = SET_HASH_FLAG(level);
163     pCommon->fmt = (const char*)hash;
164     pCommon->valueNumber = (uint8)argsNum;
165     pCommon->task = (uint8)HIVIEW_GetTaskId();
166     uint64 cur = HIVIEW_GetCurrentTime();
167     pCommon->time = (uint32)(cur / MS_PER_SECOND);
168     pCommon->milli = (uint16)(cur % MS_PER_SECOND);
169 
170     uint8 i = 0;
171     va_list args;
172     va_start(args, hash);
173     while (i < argsNum) {
174         logContent.values[i++] = va_arg(args, uint32);
175     }
176     va_end(args);
177 
178     OutputLog((uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum);
179 }
180 
HiLogFlush(boolean syncFlag)181 void HiLogFlush(boolean syncFlag)
182 {
183     FlushLog(syncFlag);
184 }
185 
HiLogGetConfigOption(void)186 uint32 HiLogGetConfigOption(void)
187 {
188     return HiviewGetConfigOption();
189 }
190 
HiLogRegisterProc(HilogProc func)191 void HiLogRegisterProc(HilogProc func)
192 {
193     HiviewRegisterHilogProc(func);
194 }
195 
HiLogUnRegisterProc(HilogProc func)196 void HiLogUnRegisterProc(HilogProc func)
197 {
198     HiviewUnRegisterHilogProc(func);
199 }
200 
HiLogFileAddWatcher(FileProc func,const char * path)201 void HiLogFileAddWatcher(FileProc func, const char *path)
202 {
203     HiviewRegisterHiLogFileWatcher(func, path);
204 }
205 
HiLogFileRemoveWatcher(FileProc func)206 void HiLogFileRemoveWatcher(FileProc func)
207 {
208     HiviewUnRegisterHiLogFileWatcher(func);
209 }
210 
HiLogFileProc(const char * dest,uint8 mode)211 int HiLogFileProc(const char *dest, uint8 mode)
212 {
213     return HiLogFileProcImp(dest, mode);
214 }
215 
HiLogOutputFileLock(void)216 void HiLogOutputFileLock(void)
217 {
218     HiLogOutputFileLockImp();
219 }
220 
HiLogOutputFileUnLock(void)221 void HiLogOutputFileUnLock(void)
222 {
223     HiLogOutputFileUnLockImp();
224 }
225 
HiLogGetLogLevel(void)226 uint32 HiLogGetLogLevel(void)
227 {
228     return g_hiviewConfig.level;
229 }
230 
HiLogSetLogLevel(uint8 level)231 boolean HiLogSetLogLevel(uint8 level)
232 {
233     if (level >= HILOG_LV_DEBUG && level < HILOG_LV_MAX) {
234         g_hiviewConfig.level = level;
235         printf("Set log level: %d\n", level);
236         return TRUE;
237     }
238     return FALSE;
239 }
240