• 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 "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 
34 static char g_logLevelInfo[HILOG_LV_MAX] = {
35     'N', // "NONE"
36     'D', // "DEBUG"
37     'I', // "INFO"
38     'W', // "WARN"
39     'E', // "ERROR"
40     'F'  // "FATAL"
41 };
42 
43 #ifndef DISABLE_HILOG_CACHE
44 static uint8 g_logCacheBuffer[LOG_STATIC_CACHE_SIZE];
45 #endif
46 static HiviewCache g_logCache = {
47     .size = 0,
48     .buffer = NULL,
49 };
50 static HiviewFile g_logFile = {
51     .path = HIVIEW_FILE_PATH_LOG,
52     .outPath = HIVIEW_FILE_OUT_PATH_LOG,
53     .pFunc = NULL,
54     .mutex = NULL,
55     .fhandle = -1,
56     .configSize = 0,
57 };
58 
59 typedef struct LogFlushInfo LogFlushInfo;
60 struct LogFlushInfo {
61     HiviewMutexId_t mutex;
62 };
63 static LogFlushInfo g_logFlushInfo;
64 static HilogProc g_hilogOutputProc = NULL;
65 
66 typedef struct OutputLogInfo OutputLogInfo;
67 struct OutputLogInfo {
68     HiviewMutexId_t mutex;
69 };
70 static OutputLogInfo g_outputLogInfo;
71 
72 static int32 g_retryInitCount = 0;
73 #define MAX_RETRY_COUNT 100
74 
75 /* Output the log to UART using plaintext. */
76 static void OutputLogRealtime(const Request *req);
77 /* Output the log to FLASH using text. */
78 static void OutputLog2TextFile(const Request *req);
79 /* Output the log to FLASH using binary. */
80 static void OutputLog2BinFile(const Request *req);
81 static int32 LogCommonFmt(char *outStr, int32 outStrlen, const HiLogCommon *commonContentPtr);
82 static int32 LogValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr);
83 static int32 LogDebugValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr);
84 static int32 LogValuesFmtHash(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr);
85 
InitCoreLogOutput(void)86 void InitCoreLogOutput(void)
87 {
88     g_logFlushInfo.mutex = HIVIEW_MutexInit();
89     g_outputLogInfo.mutex = HIVIEW_MutexInit();
90 #ifndef DISABLE_HILOG_CACHE
91     InitHiviewStaticCache(&g_logCache, LOG_CACHE, g_logCacheBuffer, sizeof(g_logCacheBuffer));
92 #endif
93     HiviewRegisterMsgHandle(HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, OutputLog2TextFile);
94     HiviewRegisterMsgHandle(HIVIEW_MSG_OUTPUT_LOG_BIN_FILE, OutputLog2BinFile);
95     HiviewRegisterMsgHandle(HIVIEW_MSG_OUTPUT_LOG_FLOW, OutputLogRealtime);
96 }
97 
InitLogOutput(void)98 void InitLogOutput(void)
99 {
100     int8 opt = GETOPTION(g_hiviewConfig.outputOption);
101     if (opt == OUTPUT_OPTION_DEBUG || opt == OUTPUT_OPTION_FLOW) {
102         return;
103     }
104     HiviewFileType type = HIVIEW_LOG_TEXT_FILE;
105     if (opt == OUTPUT_OPTION_BIN_FILE) {
106         type = HIVIEW_LOG_BIN_FILE;
107     }
108     if (InitHiviewFile(&g_logFile, type,
109         (HIVIEW_LOG_FILE_SIZE / sizeof(HiLogContent)) * sizeof(HiLogContent)) == FALSE) {
110         HIVIEW_UartPrint("Open file[HIVIEW_LOG_BIN_FILE] failed.");
111     }
112     g_logFile.mutex = g_outputLogInfo.mutex;
113 }
114 
ClearLogOutput(void)115 void ClearLogOutput(void)
116 {
117     int8 opt = GETOPTION(g_hiviewConfig.outputOption);
118     if (g_logCache.usedSize > 0) {
119         if (opt == OUTPUT_OPTION_TEXT_FILE) {
120             OutputLog2TextFile(NULL);
121         } else if (opt == OUTPUT_OPTION_BIN_FILE) {
122             OutputLog2BinFile(NULL);
123         }
124     }
125     CloseHiviewFile(&g_logFile);
126 }
127 
OutputLog(const uint8 * data,uint32 len)128 void OutputLog(const uint8 *data, uint32 len)
129 {
130     if (data == NULL) {
131         return;
132     }
133 
134     HiLogContent *hiLogContent = (HiLogContent *)data;
135     if (g_hilogOutputProc != NULL) {
136         if (g_hilogOutputProc(hiLogContent, len) == TRUE) {
137             return;
138         }
139     }
140 
141 #ifdef DISABLE_HILOG_CACHE
142     boolean isDisableCache = TRUE;
143 #else
144     boolean isDisableCache = FALSE;
145 #endif
146 
147 #ifdef DISABLE_HILOG_LITE_PRINT_LIMIT
148     boolean isDisablePrintLimited = TRUE;
149 #else
150     boolean isDisablePrintLimited = FALSE;
151 #endif
152     boolean isLogLimited = LogIsLimited(hiLogContent->commonContent.module);
153 
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     uint32 month, day, hour, min, sec;
403     uint8_t level;
404     struct tm nowTime = {0};
405 
406     time = commonContentPtr->time;
407     localtime_r(&time, &nowTime);
408     month = nowTime.tm_mon + 1;
409     day = nowTime.tm_mday;
410     hour = nowTime.tm_hour;
411     min = nowTime.tm_min;
412     sec = nowTime.tm_sec;
413     level = CLEAR_HASH_FLAG(commonContentPtr->level);
414     if (level >= HILOG_LV_MAX) {
415         level = 0;
416     }
417     ret = snprintf_s(outStr, outStrLen, outStrLen - 1, "%02d-%02d %02d:%02d:%02d.%03d 0 %d %c %d/%s: ",
418         month, day, hour, min, sec, commonContentPtr->milli, commonContentPtr->task, g_logLevelInfo[level],
419         commonContentPtr->module, HiLogGetModuleName(commonContentPtr->module));
420 
421     return ret;
422 }
423 
LogValuesFmt(char * desStrPtr,int32 desLen,const HiLogContent * logContentPtr)424 static int32 LogValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr)
425 {
426     int32 i;
427     int32 outLen = 0;
428     int32 len;
429     char fmtStr[SINGLE_FMT_MAX_LEN];
430     uint32 valNum = logContentPtr->commonContent.valueNumber;
431     const char *fmt = logContentPtr->commonContent.fmt;
432     uint32 valueIndex = 0;
433     for (i = 0; fmt[i] != 0 && outLen < desLen;) {
434         if (fmt[i] != '%') {
435             desStrPtr[outLen++] = fmt[i++];
436             continue;
437         }
438         if (fmt[i + 1] == '%') {
439             desStrPtr[outLen++] = fmt[i++];
440             desStrPtr[outLen++] = fmt[i++];
441             continue;
442         }
443         fmtStr[0] = fmt[i++];
444         uint32 t = 1;
445         while (fmt[i] != 0 && t < sizeof(fmtStr) - 1) {
446             /* %s %ms %-ms %m.ns %-m.ns convert to %p */
447             if ((fmt[i] == 's' || fmt[i] == 'S') &&
448                 (fmt[i - 1] == '%' || (fmt[i - 1] >= '0' && fmt[i - 1] <= '9'))) {
449                 fmtStr[1] = 'p';
450                 fmtStr[FMT_CONVERT_TRMINATOR] = 0;
451                 i++;
452                 break;
453             }
454             if ((fmt[i] >= 'a' && fmt[i] <= 'z') || (fmt[i] >= 'A' && fmt[i] <= 'Z')) {
455                 fmtStr[t++] = fmt[i++];
456                 fmtStr[t] = 0;
457                 break;
458             }
459             fmtStr[t++] = fmt[i++];
460         }
461         if (valueIndex < valNum) {
462             len = snprintf_s(&desStrPtr[outLen], desLen - outLen, desLen - outLen - 1,
463                 fmtStr, logContentPtr->values[valueIndex]);
464             if (len < 0) {
465                 break;
466             }
467             outLen += len;
468             valueIndex++;
469         }
470     }
471 
472     return outLen;
473 }
474 
LogDebugValuesFmt(char * desStrPtr,int32 desLen,const HiLogContent * logContentPtr)475 static int32 LogDebugValuesFmt(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr)
476 {
477     int32 ret = 0;
478     switch (logContentPtr->commonContent.valueNumber) {
479         case LOG_MULTI_PARA_0:
480             ret = strncpy_s(desStrPtr, desLen, logContentPtr->commonContent.fmt, desLen - 1);
481             if (ret != EOK) {
482                 ret = -1;
483             }
484             break;
485         case LOG_MULTI_PARA_1:
486             ret = snprintf_s(desStrPtr, desLen, desLen - 1, logContentPtr->commonContent.fmt,
487                 logContentPtr->values[0]);
488             break;
489         case LOG_MULTI_PARA_2:
490             ret = snprintf_s(desStrPtr, desLen, desLen - 1, logContentPtr->commonContent.fmt,
491                 logContentPtr->values[0], logContentPtr->values[1]);
492             break;
493         case LOG_MULTI_PARA_3:
494             ret = snprintf_s(desStrPtr, desLen, desLen - 1, logContentPtr->commonContent.fmt,
495                 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2]);
496             break;
497         case LOG_MULTI_PARA_4:
498             ret = snprintf_s(desStrPtr, desLen, desLen - 1, logContentPtr->commonContent.fmt,
499                 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2],
500                 logContentPtr->values[LOG_MULTI_PARA_3]);
501             break;
502         case LOG_MULTI_PARA_5:
503             ret = snprintf_s(desStrPtr, desLen, desLen - 1, logContentPtr->commonContent.fmt,
504                 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2],
505                 logContentPtr->values[LOG_MULTI_PARA_3], logContentPtr->values[LOG_MULTI_PARA_4]);
506             break;
507         case LOG_MULTI_PARA_MAX:
508             ret = snprintf_s(desStrPtr, desLen, desLen - 1, logContentPtr->commonContent.fmt,
509                 logContentPtr->values[0], logContentPtr->values[1], logContentPtr->values[LOG_MULTI_PARA_2],
510                 logContentPtr->values[LOG_MULTI_PARA_3], logContentPtr->values[LOG_MULTI_PARA_4],
511                 logContentPtr->values[LOG_MULTI_PARA_5]);
512             break;
513         default:
514             break;
515     }
516 
517     return (ret < 0) ? 0 : ret;
518 }
519 
LogValuesFmtHash(char * desStrPtr,int32 desLen,const HiLogContent * logContentPtr)520 static int32 LogValuesFmtHash(char *desStrPtr, int32 desLen, const HiLogContent *logContentPtr)
521 {
522     int32 outLen = 0;
523     uint32 paraNum = logContentPtr->commonContent.valueNumber;
524     int32 len = snprintf_s(&desStrPtr[outLen], desLen - outLen, desLen - outLen - 1,
525         "hash:%u para:", (uint32)logContentPtr->commonContent.fmt);
526     if (len < 0) {
527         return len;
528     }
529     outLen += len;
530 
531     for (uint32 i = 0; i < paraNum && i < LOG_MULTI_PARA_MAX; i++) {
532         len = snprintf_s(&desStrPtr[outLen], desLen - outLen, desLen - outLen - 1,
533             "%d ", (int32)logContentPtr->values[i]);
534         if (len < 0) {
535             return len;
536         }
537         outLen += len;
538     }
539     return outLen;
540 }
541 
FlushLog(boolean syncFlag)542 void FlushLog(boolean syncFlag)
543 {
544     int8 opt = GETOPTION(g_hiviewConfig.outputOption);
545     if (g_logCache.usedSize > 0) {
546         if (syncFlag == FALSE) {
547             switch (opt) {
548                 case OUTPUT_OPTION_TEXT_FILE:
549                     HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, SYNC_FILE);
550                     break;
551                 case OUTPUT_OPTION_BIN_FILE:
552                     HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_BIN_FILE, SYNC_FILE);
553                     break;
554                 case OUTPUT_OPTION_FLOW:
555                     HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_FLOW, SYNC_FILE);
556                     break;
557                 default:
558                     break;
559             }
560         } else {
561             Request request = {0};
562             request.msgValue = SYNC_FILE;
563             switch (opt) {
564                 case OUTPUT_OPTION_TEXT_FILE:
565                     OutputLog2TextFile(&request);
566                     break;
567                 case OUTPUT_OPTION_BIN_FILE:
568                     OutputLog2BinFile(&request);
569                     break;
570                 case OUTPUT_OPTION_FLOW:
571                     OutputLogRealtime(NULL);
572                     break;
573                 default:
574                     break;
575             }
576         }
577     }
578 }
579 
HiviewRegisterHilogProc(HilogProc func)580 void HiviewRegisterHilogProc(HilogProc func)
581 {
582     g_hilogOutputProc = func;
583 }
584 
HiviewGetConfigOption(void)585 uint32 HiviewGetConfigOption(void)
586 {
587     return GETOPTION(g_hiviewConfig.outputOption);
588 }
589 
HiviewUnRegisterHilogProc(HilogProc func)590 void HiviewUnRegisterHilogProc(HilogProc func)
591 {
592     (void)func;
593     if (g_hilogOutputProc != NULL) {
594         g_hilogOutputProc = NULL;
595     }
596 }
597 
HiviewRegisterHiLogFileWatcher(FileProc func,const char * path)598 void HiviewRegisterHiLogFileWatcher(FileProc func, const char *path)
599 {
600     if (func == NULL) {
601         return;
602     }
603     RegisterFileWatcher(&g_logFile, func, path);
604 }
605 
HiviewUnRegisterHiLogFileWatcher(FileProc func)606 void HiviewUnRegisterHiLogFileWatcher(FileProc func)
607 {
608     if (func == NULL) {
609         return;
610     }
611     UnRegisterFileWatcher(&g_logFile, func);
612 }
613 
HiLogFileProcImp(const char * dest,uint8 mode)614 int HiLogFileProcImp(const char* dest, uint8 mode)
615 {
616     FlushLog(TRUE);
617     HIVIEW_MutexLock(g_logFlushInfo.mutex);
618     int ret = ProcFile(&g_logFile, dest, (FileProcMode)mode);
619     HIVIEW_MutexUnlock(g_logFlushInfo.mutex);
620     return ret;
621 }
622 
HiLogOutputFileLockImp(void)623 void HiLogOutputFileLockImp(void)
624 {
625     HIVIEW_MutexLock(g_outputLogInfo.mutex);
626 }
627 
HiLogOutputFileUnLockImp(void)628 void HiLogOutputFileUnLockImp(void)
629 {
630     HIVIEW_MutexUnlock(g_outputLogInfo.mutex);
631 }
632