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 "hiview_output_log.h"
17 #include "hiview_cache.h"
18 #include "hiview_config.h"
19 #include "hiview_def.h"
20 #include "hiview_file.h"
21 #include "hiview_log.h"
22 #include "hiview_log_limit.h"
23 #include "hiview_service.h"
24 #include "hiview_util.h"
25 #include "message.h"
26 #include "ohos_types.h"
27 #include "securec.h"
28
29 #include <time.h>
30
31 #define SINGLE_FMT_MAX_LEN 8
32 #define FMT_CONVERT_TRMINATOR 2
33 #define INI_CONVERT_DIVID_NUM 10
34
35 static char g_logLevelInfo[HILOG_LV_MAX] = {
36 'N', // "NONE"
37 'D', // "DEBUG"
38 'I', // "INFO"
39 'W', // "WARN"
40 'E', // "ERROR"
41 'F' // "FATAL"
42 };
43
44 #ifndef DISABLE_HILOG_CACHE
45 static uint8 g_logCacheBuffer[LOG_STATIC_CACHE_SIZE];
46 #endif
47 static HiviewCache g_logCache = {
48 .size = 0,
49 .buffer = NULL,
50 };
51 static HiviewFile g_logFile = {
52 .path = HIVIEW_FILE_PATH_LOG,
53 .outPath = HIVIEW_FILE_OUT_PATH_LOG,
54 .pFunc = NULL,
55 .mutex = NULL,
56 .fhandle = -1,
57 .configSize = 0,
58 };
59
60 typedef struct LogFlushInfo LogFlushInfo;
61 struct LogFlushInfo {
62 HiviewMutexId_t mutex;
63 };
64 static LogFlushInfo g_logFlushInfo;
65 static HilogProc g_hilogOutputProc = NULL;
66
67 typedef struct OutputLogInfo OutputLogInfo;
68 struct OutputLogInfo {
69 HiviewMutexId_t mutex;
70 };
71 static OutputLogInfo g_outputLogInfo;
72
73 static int32 g_retryInitCount = 0;
74 #define MAX_RETRY_COUNT 100
75
76 /* Output the log to UART using plaintext. */
77 static void OutputLogRealtime(const Request *req);
78 /* Output the log to FLASH using text. */
79 static void OutputLog2TextFile(const Request *req);
80 /* Output the log to FLASH using binary. */
81 static void OutputLog2BinFile(const Request *req);
82 static int32 LogCommonFmt(char *outStr, int32 outStrlen, const HiLogCommon *commonContentPtr);
83 static int32 LogValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr);
84 static int32 LogDebugValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr);
85 static int32 LogValuesFmtHash(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr);
86
InitCoreLogOutput(void)87 void InitCoreLogOutput(void)
88 {
89 g_logFlushInfo.mutex = HIVIEW_MutexInit();
90 g_outputLogInfo.mutex = HIVIEW_MutexInit();
91 #ifndef DISABLE_HILOG_CACHE
92 InitHiviewStaticCache(&g_logCache, LOG_CACHE, g_logCacheBuffer, sizeof(g_logCacheBuffer));
93 #endif
94 HiviewRegisterMsgHandle(HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, OutputLog2TextFile);
95 HiviewRegisterMsgHandle(HIVIEW_MSG_OUTPUT_LOG_BIN_FILE, OutputLog2BinFile);
96 HiviewRegisterMsgHandle(HIVIEW_MSG_OUTPUT_LOG_FLOW, OutputLogRealtime);
97 }
98
InitLogOutput(void)99 void InitLogOutput(void)
100 {
101 int8 opt = GETOPTION(g_hiviewConfig.outputOption);
102 if (opt == OUTPUT_OPTION_DEBUG || opt == OUTPUT_OPTION_FLOW) {
103 return;
104 }
105 HiviewFileType type = HIVIEW_LOG_TEXT_FILE;
106 if (opt == OUTPUT_OPTION_BIN_FILE) {
107 type = HIVIEW_LOG_BIN_FILE;
108 }
109 if (InitHiviewFile(&g_logFile, type,
110 (HIVIEW_LOG_FILE_SIZE / sizeof(HiLogContent)) * sizeof(HiLogContent)) == FALSE) {
111 HIVIEW_UartPrint("Open file[HIVIEW_LOG_BIN_FILE] failed.");
112 }
113 g_logFile.mutex = g_outputLogInfo.mutex;
114 }
115
ClearLogOutput(void)116 void ClearLogOutput(void)
117 {
118 int8 opt = GETOPTION(g_hiviewConfig.outputOption);
119 if (g_logCache.usedSize > 0) {
120 if (opt == OUTPUT_OPTION_TEXT_FILE) {
121 OutputLog2TextFile(NULL);
122 } else if (opt == OUTPUT_OPTION_BIN_FILE) {
123 OutputLog2BinFile(NULL);
124 }
125 }
126 CloseHiviewFile(&g_logFile);
127 }
128
OutputLog(const uint8 * data,uint32 len)129 void OutputLog(const uint8 *data, uint32 len)
130 {
131 if (data == NULL) {
132 return;
133 }
134
135 HiLogContent *hiLogContent = (HiLogContent *)data;
136 if (g_hilogOutputProc != NULL) {
137 if (g_hilogOutputProc(hiLogContent, len) == TRUE) {
138 return;
139 }
140 }
141
142 #ifdef DISABLE_HILOG_CACHE
143 boolean isDisableCache = TRUE;
144 #else
145 boolean isDisableCache = FALSE;
146 #endif
147
148 #ifdef DISABLE_HILOG_LITE_PRINT_LIMIT
149 boolean isDisablePrintLimited = TRUE;
150 #else
151 boolean isDisablePrintLimited = FALSE;
152 #endif
153 boolean isLogLimited = LogIsLimited(hiLogContent->commonContent.module);
154 if (!isDisablePrintLimited && isLogLimited) {
155 // The console output adopts the same restriction strategy as the file output,
156 // and the log output to the file is restricted.
157 return;
158 }
159
160 int8 opt = GETOPTION(g_hiviewConfig.outputOption);
161 boolean isPrint = g_hiviewConfig.outputOption >= OUTPUT_OPTION_PRINT;
162 if (opt == OUTPUT_OPTION_DEBUG || isPrint || isDisableCache) {
163 char tempOutStr[LOG_FMT_MAX_LEN] = {0};
164 if (LogContentFmt(tempOutStr, sizeof(tempOutStr), data) > 0) {
165 HIVIEW_UartPrint(tempOutStr);
166 }
167 }
168
169 if (opt == OUTPUT_OPTION_DEBUG || isDisableCache || isLogLimited) {
170 return;
171 }
172
173 /* When the init of kernel is not finished, data is cached in the cache. */
174 if (g_hiviewConfig.hiviewInited == FALSE) {
175 if (WriteToCache(&g_logCache, data, len) != (int32)len) {
176 HIVIEW_UartPrint("Write log to cache failed.");
177 }
178 return;
179 }
180
181 boolean writeFail = FALSE;
182 if (WriteToCache(&g_logCache, (uint8 *)data, len) != (int32)len) {
183 HIVIEW_UartPrint("Hilog writeToCache error!\n");
184 writeFail = TRUE;
185 }
186 if (g_logCache.usedSize >= HIVIEW_HILOG_FILE_BUF_SIZE) {
187 switch (opt) {
188 case OUTPUT_OPTION_TEXT_FILE:
189 HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, 0);
190 break;
191 case OUTPUT_OPTION_BIN_FILE:
192 HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_BIN_FILE, 0);
193 break;
194 case OUTPUT_OPTION_FLOW:
195 HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_FLOW, 0);
196 break;
197 default:
198 break;
199 }
200 }
201
202 /* If the cache fails to be written, write the cache again. */
203 if (writeFail) {
204 WriteToCache(&g_logCache, (uint8 *)data, len);
205 }
206 }
207
OutputLogRealtime(const Request * req)208 static void OutputLogRealtime(const Request *req)
209 {
210 HIVIEW_MutexLock(g_logFlushInfo.mutex);
211 HiLogContent logContent;
212 char tempOutStr[LOG_FMT_MAX_LEN] = {0};
213 int32 len;
214 (void)req;
215
216 while (ReadFromCache(&g_logCache, (uint8 *)&(logContent.commonContent),
217 sizeof(HiLogCommon)) == sizeof(HiLogCommon)) {
218 if (logContent.commonContent.head != LOG_INFO_HEAD) {
219 DiscardCacheData(&g_logCache);
220 HIVIEW_UartPrint("Discard cache[LOG_CACHE] data.");
221 break;
222 }
223 len = logContent.commonContent.valueNumber * sizeof(uint32);
224 if (len > 0 && ReadFromCache(&g_logCache, (uint8 *)&(logContent.values), len) != len) {
225 continue;
226 }
227 len = LogContentFmt(tempOutStr, sizeof(tempOutStr), (uint8 *)&logContent);
228 if (len <= 0) {
229 continue;
230 }
231 HIVIEW_UartPrint(tempOutStr);
232 }
233 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
234 }
235
OutputLog2TextFile(const Request * req)236 static void OutputLog2TextFile(const Request *req)
237 {
238 HIVIEW_MutexLock(g_logFlushInfo.mutex);
239 HiLogContent logContent;
240 char tempOutStr[LOG_FMT_MAX_LEN] = {0};
241
242 if (g_logCache.usedSize < sizeof(HiLogCommon)) {
243 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
244 return;
245 }
246
247 int32 len;
248 while (ReadFromCache(&g_logCache, (uint8 *)&(logContent.commonContent),
249 sizeof(HiLogCommon)) == sizeof(HiLogCommon)) {
250 if (logContent.commonContent.head != LOG_INFO_HEAD) {
251 DiscardCacheData(&g_logCache);
252 HIVIEW_UartPrint("Discard cache[LOG_CACHE] data.");
253 break;
254 }
255 len = logContent.commonContent.valueNumber * sizeof(uint32);
256 if (len > 0 && ReadFromCache(&g_logCache, (uint8 *)&(logContent.values), len) != len) {
257 continue;
258 }
259 len = LogContentFmt(tempOutStr, sizeof(tempOutStr), (uint8 *)&logContent);
260 if (len > 0 && tempOutStr[len - 1] == '\0') {
261 // prevent writing '\0' character to file
262 len--;
263 }
264 if (g_logFile.fhandle < 0) {
265 if (g_retryInitCount < MAX_RETRY_COUNT) {
266 InitLogOutput();
267 }
268 g_retryInitCount++;
269 } else {
270 // once success, clean retry count
271 g_retryInitCount = 0;
272 }
273 if (len > 0 && WriteToFile(&g_logFile, (uint8 *)tempOutStr, len) != len) {
274 g_hiviewConfig.writeFailureCount++;
275 }
276 }
277 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
278 if (req != NULL && req->msgValue == SYNC_FILE) {
279 HIVIEW_FileSync(g_logFile.fhandle);
280 }
281 }
282
OutputLog2BinFile(const Request * req)283 static void OutputLog2BinFile(const Request *req)
284 {
285 HIVIEW_MutexLock(g_logFlushInfo.mutex);
286 HiLogCommon *pCommonContent = NULL;
287 uint16 len = 0;
288 uint16 valueLen;
289 uint8 *tmpBuffer = NULL;
290 uint16 outputSize = g_logCache.usedSize;
291
292 if (outputSize < sizeof(HiLogCommon)) {
293 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
294 return;
295 }
296 tmpBuffer = (uint8 *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, outputSize);
297 if (tmpBuffer == NULL) {
298 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
299 return;
300 }
301 while (g_logCache.usedSize >= sizeof(HiLogCommon) && outputSize > (len + sizeof(HiLogCommon))) {
302 if (ReadFromCache(&g_logCache, tmpBuffer + len, sizeof(HiLogCommon)) != sizeof(HiLogCommon)) {
303 continue;
304 }
305 pCommonContent = (HiLogCommon *)(tmpBuffer + len);
306 len += sizeof(HiLogCommon);
307 if (pCommonContent->head != LOG_INFO_HEAD) {
308 DiscardCacheData(&g_logCache);
309 HIVIEW_UartPrint("Discard cache[LOG_CACHE] data.");
310 break;
311 }
312 valueLen = pCommonContent->valueNumber * sizeof(uint32);
313 if (valueLen > 0) {
314 if ((int32)len + (int32)valueLen > (int32)outputSize) {
315 DiscardCacheData(&g_logCache);
316 HIVIEW_UartPrint("Discard cache[LOG_CACHE] data.");
317 break;
318 }
319 if (ReadFromCache(&g_logCache, tmpBuffer + len, valueLen) != valueLen) {
320 continue;
321 }
322 len += valueLen;
323 }
324 }
325 if (g_logFile.fhandle < 0) {
326 if (g_retryInitCount < MAX_RETRY_COUNT) {
327 InitLogOutput();
328 }
329 g_retryInitCount++;
330 } else {
331 // once success, clean retry count
332 g_retryInitCount = 0;
333 }
334 if (len > 0 && WriteToFile(&g_logFile, tmpBuffer, len) != len) {
335 g_hiviewConfig.writeFailureCount++;
336 HIVIEW_UartPrint("Failed to write log data.");
337 }
338 HIVIEW_MemFree(MEM_POOL_HIVIEW_ID, tmpBuffer);
339 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
340 if (req != NULL && req->msgValue == SYNC_FILE) {
341 HIVIEW_FileSync(g_logFile.fhandle);
342 }
343 }
344
GetLogFileSize(void)345 uint32 GetLogFileSize(void)
346 {
347 return GetFileUsedSize(&g_logFile);
348 }
349
ReadLogFile(uint8 * buf,uint32 len)350 uint32 ReadLogFile(uint8 *buf, uint32 len)
351 {
352 if (buf == NULL) {
353 return 0;
354 }
355 uint32 usedSize = GetFileUsedSize(&g_logFile);
356 if (usedSize < len) {
357 len = usedSize;
358 }
359 if (ReadFromFile(&g_logFile, buf, len) != (int32)len) {
360 return 0;
361 }
362
363 return len;
364 }
365
LogContentFmt(char * outStr,int32 outStrLen,const uint8 * pLogContent)366 int32 LogContentFmt(char *outStr, int32 outStrLen, const uint8 *pLogContent)
367 {
368 int32 len;
369 HiLogContent *logContentPtr = (HiLogContent *)pLogContent;
370
371 len = LogCommonFmt(outStr, outStrLen, &(logContentPtr->commonContent));
372 boolean isHash = CHECK_HASH_FLAG(logContentPtr->commonContent.level);
373 if (len >= 0) {
374 if (isHash) {
375 len += LogValuesFmtHash(outStr + len, outStrLen - len, logContentPtr);
376 } else if (GETOPTION(g_hiviewConfig.outputOption) == OUTPUT_OPTION_DEBUG) {
377 len += LogDebugValuesFmt(outStr + len, outStrLen - len, logContentPtr);
378 } else {
379 len += LogValuesFmt(outStr + len, outStrLen - len, logContentPtr);
380 }
381 }
382
383 if (len < 0) {
384 return len;
385 }
386
387 if (len >= outStrLen - 1) {
388 outStr[outStrLen - TAIL_LINE_BREAK] = '\n';
389 outStr[outStrLen - 1] = '\0';
390 } else {
391 outStr[len++] = '\n';
392 outStr[len++] = '\0';
393 }
394
395 return len;
396 }
397
LogCommonFmt(char * outStr,int32 outStrLen,const HiLogCommon * commonContentPtr)398 static int32 LogCommonFmt(char *outStr, int32 outStrLen, const HiLogCommon *commonContentPtr)
399 {
400 int32 ret;
401 time_t time;
402 uint8_t level;
403 struct tm nowTime = {0};
404
405 time = commonContentPtr->time;
406 localtime_r(&time, &nowTime);
407 int month = nowTime.tm_mon + 1;
408 int day = nowTime.tm_mday;
409 int hour = nowTime.tm_hour;
410 int min = nowTime.tm_min;
411 int sec = nowTime.tm_sec;
412 level = CLEAR_HASH_FLAG(commonContentPtr->level);
413 if (level >= HILOG_LV_MAX) {
414 level = 0;
415 }
416 ret = snprintf_s(outStr, outStrLen, outStrLen - 1, "%02d-%02d %02d:%02d:%02d.%03d 0 %d %c %d/%s: ",
417 month, day, hour, min, sec, commonContentPtr->milli, commonContentPtr->task, g_logLevelInfo[level],
418 commonContentPtr->module, HiLogGetModuleName(commonContentPtr->module));
419
420 return ret;
421 }
422
LogValuesFmt(char * desStrPtr,int32 desLen,const HiLogContent * logContentPtr)423 static int32 LogValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr)
424 {
425 int32 i;
426 int32 outLen = 0;
427 int32 len;
428 char fmtStr[SINGLE_FMT_MAX_LEN];
429 uint32 valNum = logContentPtr->commonContent.valueNumber;
430 const char *fmt = logContentPtr->commonContent.fmt;
431 uint32 valueIndex = 0;
432 for (i = 0; fmt[i] != 0 && outLen < desLen;) {
433 if (fmt[i] != '%') {
434 desStrPtr[outLen++] = fmt[i++];
435 continue;
436 }
437 if (fmt[i + 1] == '%') {
438 desStrPtr[outLen++] = fmt[i++];
439 desStrPtr[outLen++] = fmt[i++];
440 continue;
441 }
442 fmtStr[0] = fmt[i++];
443 uint32 t = 1;
444 while (fmt[i] != 0 && t < sizeof(fmtStr) - 1) {
445 /* %s %ms %-ms %m.ns %-m.ns convert to %p */
446 if ((fmt[i] == 's' || fmt[i] == 'S') &&
447 (fmt[i - 1] == '%' || (fmt[i - 1] >= '0' && fmt[i - 1] <= '9'))) {
448 fmtStr[1] = 'p';
449 fmtStr[FMT_CONVERT_TRMINATOR] = 0;
450 i++;
451 break;
452 }
453 if ((fmt[i] >= 'a' && fmt[i] <= 'z') || (fmt[i] >= 'A' && fmt[i] <= 'Z')) {
454 fmtStr[t++] = fmt[i++];
455 fmtStr[t] = 0;
456 break;
457 }
458 fmtStr[t++] = fmt[i++];
459 }
460 if (valueIndex < valNum) {
461 len = snprintf_s(&desStrPtr[outLen], desLen - outLen, desLen - outLen - 1,
462 fmtStr, logContentPtr->values[valueIndex]);
463 if (len < 0) {
464 break;
465 }
466 outLen += len;
467 valueIndex++;
468 }
469 }
470
471 return outLen;
472 }
473
RemovePrivacyFmt(const char * fmtStr,size_t fmtLen,char * arr,size_t arrLen)474 static void RemovePrivacyFmt(const char* fmtStr, size_t fmtLen, char* arr, size_t arrLen)
475 {
476 if (arrLen == 0) {
477 return;
478 }
479 static const char *publicStr = "{public}";
480 static const char *privateStr = "{private}";
481 static const int publicLen = 8;
482 static const int privateLen = 9;
483 size_t writePos = 0;
484 size_t pos = 0;
485 for (; pos < fmtLen && writePos < arrLen - 1; ++pos) {
486 arr[writePos++] = fmtStr[pos];
487 if (fmtStr[pos] != '%') {
488 continue;
489 }
490 if (pos + 1 + publicLen < fmtLen && strncmp(fmtStr + pos + 1, publicStr, publicLen) == 0) {
491 pos += publicLen;
492 } else if (pos + 1 + privateLen < fmtLen && strncmp(fmtStr + pos + 1, privateStr, privateLen) == 0) {
493 pos += privateLen;
494 }
495 }
496 while (pos < fmtLen && writePos < arrLen - 1) {
497 arr[writePos++] = fmtStr[pos];
498 }
499 arr[writePos] = 0;
500 return;
501 }
502
LogDebugValuesFmt(char * desStrPtr,int32 desLen,const HiLogContent * logContentPtr)503 static int32 LogDebugValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr)
504 {
505 int32 ret = 0;
506 size_t fmtLen = strlen(logContentPtr->commonContent.fmt);
507 char *fmt = (char *)malloc((fmtLen + 1) * sizeof(char)); // 1: for '\0'
508 if (fmt == NULL) {
509 return -1;
510 }
511 memset_s(fmt, fmtLen + 1, 0, fmtLen + 1);
512 RemovePrivacyFmt(logContentPtr->commonContent.fmt, fmtLen, fmt, fmtLen);
513 switch (logContentPtr->commonContent.valueNumber) {
514 case LOG_MULTI_PARA_0:
515 ret = strncpy_s(desStrPtr, desLen, fmt, desLen - 1);
516 if (ret != EOK) {
517 ret = -1;
518 } else {
519 ret = strlen(fmt);
520 }
521 break;
522 case LOG_MULTI_PARA_1:
523 ret = snprintf_s(desStrPtr, desLen, desLen - 1, fmt,
524 logContentPtr->values[0]);
525 break;
526 case LOG_MULTI_PARA_2:
527 ret = snprintf_s(desStrPtr, desLen, desLen - 1, fmt,
528 logContentPtr->values[0], logContentPtr->values[1]);
529 break;
530 case LOG_MULTI_PARA_3:
531 ret = snprintf_s(desStrPtr, desLen, desLen - 1, fmt,
532 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2]);
533 break;
534 case LOG_MULTI_PARA_4:
535 ret = snprintf_s(desStrPtr, desLen, desLen - 1, fmt,
536 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2],
537 logContentPtr->values[LOG_MULTI_PARA_3]);
538 break;
539 case LOG_MULTI_PARA_5:
540 ret = snprintf_s(desStrPtr, desLen, desLen - 1, fmt,
541 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2],
542 logContentPtr->values[LOG_MULTI_PARA_3], logContentPtr->values[LOG_MULTI_PARA_4]);
543 break;
544 case LOG_MULTI_PARA_MAX:
545 ret = snprintf_s(desStrPtr, desLen, desLen - 1, fmt,
546 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2],
547 logContentPtr->values[LOG_MULTI_PARA_3], logContentPtr->values[LOG_MULTI_PARA_4],
548 logContentPtr->values[LOG_MULTI_PARA_5]);
549 break;
550 default:
551 break;
552 }
553 free(fmt);
554 return (ret < 0) ? (desLen - 1) : ret;
555 }
556
IntAppendStr(char * str,int32 num,char end)557 static int32 IntAppendStr(char* str, int32 num, char end)
558 {
559 int32 digits = 0;
560 if (num == 0) {
561 str[0] = '0';
562 digits++;
563 str[1] = end;
564 return digits + 1;
565 }
566 int32 temp = num > 0 ? num : -num;
567 while (temp > 0) {
568 temp /= INI_CONVERT_DIVID_NUM;
569 digits++;
570 }
571 if (num < 0) {
572 str[0] = '-';
573 temp = -num;
574 str++;
575 } else {
576 temp = num;
577 }
578 for (int32 i = digits - 1; i >= 0; i--) {
579 str[i] = temp % INI_CONVERT_DIVID_NUM + '0';
580 temp /= INI_CONVERT_DIVID_NUM;
581 }
582 str[digits] = end;
583 if (num < 0) {
584 digits ++;
585 }
586 return digits + 1;
587 }
588
UIntAppendStr(char * str,uint32 num,char end)589 static int UIntAppendStr(char* str, uint32 num, char end)
590 {
591 int32 digits = 0;
592 if (num == 0) {
593 str[0] = '0';
594 digits++;
595 str[1] = end;
596 return digits + 1;
597 }
598 uint32 temp = num;
599 while (temp > 0) {
600 temp /= INI_CONVERT_DIVID_NUM;
601 digits++;
602 }
603 temp = num;
604 for (int32 i = digits - 1; i >= 0; i--) {
605 str[i] = temp % INI_CONVERT_DIVID_NUM + '0';
606 temp /= INI_CONVERT_DIVID_NUM;
607 }
608 str[digits] = end;
609 return digits + 1;
610 }
611
LogValuesFmtHash(char * desStrPtr,int32 desLen,const HiLogContent * logContentPtr)612 static int32 LogValuesFmtHash(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr)
613 {
614 int32 outLen = 0;
615 uint32 paraNum = logContentPtr->commonContent.valueNumber;
616 int32 ret = strncpy_s(&desStrPtr[outLen], desLen - outLen, "hash:", strlen("hash:"));
617 if (ret != 0) {
618 return -ret;
619 }
620 outLen += strlen("hash:");
621 int32 len = UIntAppendStr(&desStrPtr[outLen], (uint32)logContentPtr->commonContent.fmt, ' ');
622 outLen += len;
623 ret = strncpy_s(&desStrPtr[outLen], desLen - outLen, "para:", strlen("para:"));
624 if (ret != 0) {
625 return -ret;
626 }
627 outLen += strlen("para:");
628 for (uint32 i = 0; i < paraNum && i < LOG_MULTI_PARA_MAX; i++) {
629 len = IntAppendStr(&desStrPtr[outLen], (int32)logContentPtr->values[i], ' ');
630 outLen += len;
631 }
632 return outLen;
633 }
634
FlushLog(boolean syncFlag)635 void FlushLog(boolean syncFlag)
636 {
637 int8 opt = GETOPTION(g_hiviewConfig.outputOption);
638 if (g_logCache.usedSize > 0) {
639 if (syncFlag == FALSE) {
640 switch (opt) {
641 case OUTPUT_OPTION_TEXT_FILE:
642 HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, SYNC_FILE);
643 break;
644 case OUTPUT_OPTION_BIN_FILE:
645 HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_BIN_FILE, SYNC_FILE);
646 break;
647 case OUTPUT_OPTION_FLOW:
648 HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_FLOW, SYNC_FILE);
649 break;
650 default:
651 break;
652 }
653 } else {
654 Request request = {0};
655 request.msgValue = SYNC_FILE;
656 switch (opt) {
657 case OUTPUT_OPTION_TEXT_FILE:
658 OutputLog2TextFile(&request);
659 break;
660 case OUTPUT_OPTION_BIN_FILE:
661 OutputLog2BinFile(&request);
662 break;
663 case OUTPUT_OPTION_FLOW:
664 OutputLogRealtime(NULL);
665 break;
666 default:
667 break;
668 }
669 }
670 }
671 }
672
HiviewRegisterHilogProc(HilogProc func)673 void HiviewRegisterHilogProc(HilogProc func)
674 {
675 g_hilogOutputProc = func;
676 }
677
HiviewGetConfigOption(void)678 uint32 HiviewGetConfigOption(void)
679 {
680 return GETOPTION(g_hiviewConfig.outputOption);
681 }
682
HiviewUnRegisterHilogProc(HilogProc func)683 void HiviewUnRegisterHilogProc(HilogProc func)
684 {
685 (void)func;
686 if (g_hilogOutputProc != NULL) {
687 g_hilogOutputProc = NULL;
688 }
689 }
690
HiviewRegisterHiLogFileWatcher(FileProc func,const char * path)691 void HiviewRegisterHiLogFileWatcher(FileProc func, const char *path)
692 {
693 if (func == NULL) {
694 return;
695 }
696 RegisterFileWatcher(&g_logFile, func, path);
697 }
698
HiviewUnRegisterHiLogFileWatcher(FileProc func)699 void HiviewUnRegisterHiLogFileWatcher(FileProc func)
700 {
701 if (func == NULL) {
702 return;
703 }
704 UnRegisterFileWatcher(&g_logFile, func);
705 }
706
HiLogFileProcImp(const char * dest,uint8 mode)707 int HiLogFileProcImp(const char* dest, uint8 mode)
708 {
709 FlushLog(TRUE);
710 HIVIEW_MutexLock(g_logFlushInfo.mutex);
711 int ret = ProcFile(&g_logFile, dest, (FileProcMode)mode);
712 HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
713 return ret;
714 }
715
HiLogOutputFileLockImp(void)716 void HiLogOutputFileLockImp(void)
717 {
718 HIVIEW_MutexLock(g_outputLogInfo.mutex);
719 }
720
HiLogOutputFileUnLockImp(void)721 void HiLogOutputFileUnLockImp(void)
722 {
723 HIVIEW_MutexUnlock(g_outputLogInfo.mutex);
724 }
725