From 3a713ed0b170a42bdf52d82b42457f746ed019cb Mon Sep 17 00:00:00 2001 From: stesen Date: Thu, 22 Jul 2021 07:37:06 +0000 Subject: [PATCH 1/6] fix memset to memset_s --- command/hilog_command.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/hilog_command.c b/command/hilog_command.c index b321d4f..645819e 100644 --- a/command/hilog_command.c +++ b/command/hilog_command.c @@ -98,7 +98,7 @@ int SetOutputDomain(const char *mod) int len = strlen(mod); int modSize = sizeof(g_hiviewConfig.logOutputModule); - memset(g_hiviewConfig.logOutputModule, '0', modSize); + (void)memset_s(g_hiviewConfig.logOutputModule, modSize, '0', modSize); int destStart = ((DOMAIN_ID_LENGTH - len) > 0) ? (DOMAIN_ID_LENGTH - len) : 0; int sourceStart = ((len - DOMAIN_ID_LENGTH) > 0) ? (len - DOMAIN_ID_LENGTH) : 0; -- Gitee From 629e1d538aaead829c01a130878453a00c7718a3 Mon Sep 17 00:00:00 2001 From: huxia Date: Wed, 28 Jul 2021 15:06:20 +0800 Subject: [PATCH 2/6] add return value check Signed-off-by: huxia Change-Id: I3b234a163193faa7d123b434cd8590a0dad293ac Signed-off-by: huxia --- command/hilog_command.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/command/hilog_command.c b/command/hilog_command.c index 645819e..e117dac 100644 --- a/command/hilog_command.c +++ b/command/hilog_command.c @@ -104,7 +104,10 @@ int SetOutputDomain(const char *mod) int sourceStart = ((len - DOMAIN_ID_LENGTH) > 0) ? (len - DOMAIN_ID_LENGTH) : 0; int copyLen = (len < DOMAIN_ID_LENGTH) ? len : DOMAIN_ID_LENGTH; - strncpy_s(g_hiviewConfig.logOutputModule + destStart, modSize - destStart, mod + sourceStart, copyLen); + if (strncpy_s(g_hiviewConfig.logOutputModule + destStart, modSize - destStart, mod + sourceStart, copyLen) != 0) { + printf("Copy log domain fail : %s \n", mod); + return -1; + } printf("Set log domain : %s \n", g_hiviewConfig.logOutputModule); return 0; -- Gitee From 803c18398512d358e6d5198e8b8888413a89a5fb Mon Sep 17 00:00:00 2001 From: huxia Date: Fri, 30 Jul 2021 16:05:40 +0800 Subject: [PATCH 3/6] fix hilog file flush problem Signed-off-by: huxia Change-Id: I074342e9a01d0ec15cf4951a9c7c32e08df2b33f Signed-off-by: huxia --- frameworks/featured/hiview_log.c | 16 ++++++ .../native/innerkits/hilog/hiview_log.h | 2 + services/apphilogcat/hiview_applogcat.c | 56 +++++++++++++++++-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/frameworks/featured/hiview_log.c b/frameworks/featured/hiview_log.c index a0e4279..b613624 100755 --- a/frameworks/featured/hiview_log.c +++ b/frameworks/featured/hiview_log.c @@ -1558,3 +1558,19 @@ int HiLogPrint(LogType bufID, LogLevel prio, unsigned int domain, const char *ta va_end(ap); return ret; } + +int FlushHilog(void) +{ + int ret; +#define FLUSH_LOG_WRITE_LEN 3 + char buf[FLUSH_LOG_WRITE_LEN] = {0x07, 0x07, '\0'}; +#ifdef LOSCFG_BASE_CORE_HILOG + ret = HiLogWriteInternal(buf, strlen(buf) + 1); +#else + if (g_hilogFd == -1) { + g_hilogFd = open(HILOG_DRIVER, O_WRONLY); + } + ret = write(g_hilogFd, buf, strlen(buf) + 1); +#endif + return ret; +} \ No newline at end of file diff --git a/interfaces/native/innerkits/hilog/hiview_log.h b/interfaces/native/innerkits/hilog/hiview_log.h index 93e82d6..0967ac5 100755 --- a/interfaces/native/innerkits/hilog/hiview_log.h +++ b/interfaces/native/innerkits/hilog/hiview_log.h @@ -310,6 +310,8 @@ int HiLogPrintArgs(LogType bufID, LogLevel prio, */ #define HILOG_FATAL(type, ...) ((void)HiLogPrint(LOG_CORE, LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)) +int FlushHilog(void); + #define HILOG_DRIVER "/dev/hilog" #define NANOSEC_PER_MIRCOSEC 1000000 struct HiLogEntry { diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index ff6a4e9..059aa26 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -25,6 +25,9 @@ #undef LOG_TAG #define LOG_TAG "apphilogcat" +static int file1Size = 0; +static int file2Size = 0; + static int FileSize(const char *filename) { FILE *fp = fopen(filename, "r"); @@ -55,33 +58,62 @@ static FILE *FileClear(FILE **fp, const char *filename) FILE *SelectWriteFile(FILE **fp1, FILE *fp2) { - int file1Size = FileSize(HILOG_PATH1); - int file2Size = FileSize(HILOG_PATH2); + file1Size = FileSize(HILOG_PATH1); + file2Size = FileSize(HILOG_PATH2); if (file1Size < HILOG_MAX_FILELEN) { return *fp1; } else if (file2Size < HILOG_MAX_FILELEN) { return fp2; } else { // clear file1 write file 1 + file1Size = 0; return FileClear(fp1, HILOG_PATH1); } } FILE *SwitchWriteFile(FILE **fp1, FILE **fp2, FILE *curFp) { - int file1Size = FileSize(HILOG_PATH1); - int file2Size = FileSize(HILOG_PATH2); // select file, if file1 is full, record file2, file2 is full, record file1 if (file1Size < HILOG_MAX_FILELEN) { return *fp1; } else if (file2Size < HILOG_MAX_FILELEN) { return *fp2; } else if (curFp == *fp2) { // clear file1 write file 1 + FlushAndSync(*fp2); + file1Size = 0; return FileClear(fp1, HILOG_PATH1); } else { + FlushAndSync(*fp1); + file2Size = 0; return FileClear(fp2, HILOG_PATH2); } } +int FlushAndSync(FILE* fp) +{ + if (fp == NULL) { + return 0; + } + if (fflush(fp) != 0) { + return -1; + } + int fd = fileno(fp); + if (fsync(fd) != 0) { + return -1; + } + return 0; +} + +bool NeedFlush(char* buf) +{ +#define FLUSH_LOG_ARG_0 0 +#define FLUSH_LOG_ARG_1 1 +#define FLUSH_LOG_FLAG 0x07 + if (buf[FLUSH_LOG_ARG_0] == FLUSH_LOG_FLAG && buf[FLUSH_LOG_ARG_1] == FLUSH_LOG_FLAG) { + return true; + } + return false; +} + int main(int argc, const char **argv) { #define HILOG_PERMMISION 0700 @@ -132,6 +164,13 @@ int main(int argc, const char **argv) } struct HiLogEntry *head = (struct HiLogEntry *)buf; + if (NeedFlush(head->msg)) { + if (FlushAndSync(fpWrite) != 0) { + printf("flush and sync file err, fpWrite=%p\n", fpWrite); + } + continue; + } + time_t rawtime; struct tm *info = NULL; unsigned int sec = head->sec; @@ -162,6 +201,15 @@ int main(int argc, const char **argv) ret = fprintf(fpWrite, "%02d-%02d %02d:%02d:%02d.%03d %d %d %s\n", info->tm_mon + 1, info->tm_mday, info->tm_hour, info->tm_min, info->tm_sec, head->nsec / NANOSEC_PER_MIRCOSEC, head->pid, head->taskId, head->msg); + if (ret < 0) { + printf("[FATAL]File can't write fpWrite=%p\n", fpWrite); + return 0; + } + if (fpWrite == fp1) { + file1Size += ret; + } else if (fpWrite == fp2) { + file2Size += ret; + } // select file, if file1 is full, record file2, file2 is full, record file1 fpWrite = SwitchWriteFile(&fp1, &fp2, fpWrite); if (fpWrite == NULL) { -- Gitee From c9cb5095091986241548c3b7870821c007f5b14b Mon Sep 17 00:00:00 2001 From: huxia Date: Wed, 4 Aug 2021 11:55:09 +0800 Subject: [PATCH 4/6] fix codex check Signed-off-by: huxia Change-Id: Ia9b3b8f71473820e546cad5986d9cac0afb40381 Signed-off-by: huxia --- frameworks/featured/hiview_log.c | 6 ++++++ frameworks/mini/hiview_output_log.c | 6 ++++++ services/apphilogcat/hiview_applogcat.c | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/frameworks/featured/hiview_log.c b/frameworks/featured/hiview_log.c index b613624..02095ff 100755 --- a/frameworks/featured/hiview_log.c +++ b/frameworks/featured/hiview_log.c @@ -1544,6 +1544,9 @@ int HiLogPrintArgs(LogType bufID, LogLevel prio, unsigned int domain, const char if (g_hilogFd == -1) { g_hilogFd = open(HILOG_DRIVER, O_WRONLY); } + if (g_hilogFd == -1) { + return 0; + } ret = write(g_hilogFd, buf, strlen(buf) + 1); #endif return ret; @@ -1570,6 +1573,9 @@ int FlushHilog(void) if (g_hilogFd == -1) { g_hilogFd = open(HILOG_DRIVER, O_WRONLY); } + if (g_hilogFd == -1) { + return 0; + } ret = write(g_hilogFd, buf, strlen(buf) + 1); #endif return ret; diff --git a/frameworks/mini/hiview_output_log.c b/frameworks/mini/hiview_output_log.c index b276092..3dae864 100755 --- a/frameworks/mini/hiview_output_log.c +++ b/frameworks/mini/hiview_output_log.c @@ -539,11 +539,17 @@ void HiviewUnRegisterHilogProc(HilogProc func) void HiviewRegisterHiLogFileWatcher(FileProc func, const char *path) { + if (func == NULL || path == NULL) { + return; + } RegisterFileWatcher(&g_logFile, func, path); } void HiviewUnRegisterHiLogFileWatcher(FileProc func) { + if (func == NULL) { + return; + } UnRegisterFileWatcher(&g_logFile, func); } diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index 059aa26..1dba7df 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -103,7 +103,7 @@ int FlushAndSync(FILE* fp) return 0; } -bool NeedFlush(char* buf) +bool NeedFlush(const char* buf) { #define FLUSH_LOG_ARG_0 0 #define FLUSH_LOG_ARG_1 1 @@ -157,7 +157,7 @@ int main(int argc, const char **argv) return 0; } while (1) { - char buf[HILOG_LOGBUFFER] = {0}; + char buf[HILOG_LOGBUFFER + 1] = {0}; ret = read(fd, buf, HILOG_LOGBUFFER); if (ret < sizeof(struct HiLogEntry)) { continue; -- Gitee From 8fcce30a6fba761443b1e7e4495195f37a28f988 Mon Sep 17 00:00:00 2001 From: huxia Date: Thu, 5 Aug 2021 09:33:25 +0800 Subject: [PATCH 5/6] increase the function of writing files and serial port at the same time in hilog_lite Signed-off-by: huxia Change-Id: I3044bf1a7a032f68fc5867ce006e849ce22452fd Signed-off-by: huxia --- frameworks/mini/hiview_log_limit.c | 2 +- frameworks/mini/hiview_output_log.c | 30 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/frameworks/mini/hiview_log_limit.c b/frameworks/mini/hiview_log_limit.c index ca47b9f..8758bbb 100755 --- a/frameworks/mini/hiview_log_limit.c +++ b/frameworks/mini/hiview_log_limit.c @@ -57,7 +57,7 @@ void InitLogLimit(void) boolean LogIsLimited(uint8 module) { - if (g_hiviewConfig.outputOption == OUTPUT_OPTION_DEBUG) { + if (GETOPTION(g_hiviewConfig.outputOption) == OUTPUT_OPTION_DEBUG) { return FALSE; } /* covert ms to sec by dividing 1000, and integer overflow can be accepted */ diff --git a/frameworks/mini/hiview_output_log.c b/frameworks/mini/hiview_output_log.c index 3dae864..2e0b454 100755 --- a/frameworks/mini/hiview_output_log.c +++ b/frameworks/mini/hiview_output_log.c @@ -92,12 +92,12 @@ void InitCoreLogOutput(void) void InitLogOutput(void) { - if (g_hiviewConfig.outputOption == OUTPUT_OPTION_DEBUG || - g_hiviewConfig.outputOption == OUTPUT_OPTION_FLOW) { + int8 opt = GETOPTION(g_hiviewConfig.outputOption); + if (opt == OUTPUT_OPTION_DEBUG || opt == OUTPUT_OPTION_FLOW) { return; } HiviewFileType type = HIVIEW_LOG_TEXT_FILE; - if (g_hiviewConfig.outputOption == OUTPUT_OPTION_BIN_FILE) { + if (opt == OUTPUT_OPTION_BIN_FILE) { type = HIVIEW_LOG_BIN_FILE; } if (InitHiviewFile(&g_logFile, type, @@ -109,10 +109,11 @@ void InitLogOutput(void) void ClearLogOutput(void) { + int8 opt = GETOPTION(g_hiviewConfig.outputOption); if (g_logCache.usedSize > 0) { - if (g_hiviewConfig.outputOption == OUTPUT_OPTION_TEXT_FILE) { + if (opt == OUTPUT_OPTION_TEXT_FILE) { OutputLog2TextFile(NULL); - } else if (g_hiviewConfig.outputOption == OUTPUT_OPTION_BIN_FILE) { + } else if (opt == OUTPUT_OPTION_BIN_FILE) { OutputLog2BinFile(NULL); } } @@ -136,11 +137,17 @@ void OutputLog(const uint8 *data, uint32 len) #else boolean isDisableCache = FALSE; #endif - if (g_hiviewConfig.outputOption == OUTPUT_OPTION_DEBUG || isDisableCache) { + + int8 opt = GETOPTION(g_hiviewConfig.outputOption); + boolean isPrint = g_hiviewConfig.outputOption >= OUTPUT_OPTION_PRINT; + if (opt == OUTPUT_OPTION_DEBUG || isPrint || isDisableCache) { char tempOutStr[LOG_FMT_MAX_LEN] = {0}; if (LogContentFmt(tempOutStr, sizeof(tempOutStr), data) > 0) { HIVIEW_UartPrint(tempOutStr); } + } + + if (opt == OUTPUT_OPTION_DEBUG || isDisableCache) { return; } @@ -158,7 +165,7 @@ void OutputLog(const uint8 *data, uint32 len) writeFail = TRUE; } if (g_logCache.usedSize >= HIVIEW_HILOG_FILE_BUF_SIZE) { - switch (g_hiviewConfig.outputOption) { + switch (opt) { case OUTPUT_OPTION_TEXT_FILE: HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, 0); break; @@ -320,7 +327,7 @@ int32 LogContentFmt(char *outStr, int32 outStrLen, const uint8 *pLogContent) if (len >= 0) { if (isHash) { len += LogValuesFmtHash(outStr + len, outStrLen - len, logContentPtr); - } else if (g_hiviewConfig.outputOption == OUTPUT_OPTION_DEBUG) { + } else if (GETOPTION(g_hiviewConfig.outputOption) == OUTPUT_OPTION_DEBUG) { len += LogDebugValuesFmt(outStr + len, outStrLen - len, logContentPtr); } else { len += LogValuesFmt(outStr + len, outStrLen - len, logContentPtr); @@ -486,9 +493,10 @@ static int32 LogValuesFmtHash(char *desStrPtr, int32 desLen, const HiLogContent void FlushLog(boolean syncFlag) { + int8 opt = GETOPTION(g_hiviewConfig.outputOption); if (g_logCache.usedSize > 0) { if (syncFlag == FALSE) { - switch (g_hiviewConfig.outputOption) { + switch (opt) { case OUTPUT_OPTION_TEXT_FILE: HiviewSendMessage(HIVIEW_SERVICE, HIVIEW_MSG_OUTPUT_LOG_TEXT_FILE, 0); break; @@ -502,7 +510,7 @@ void FlushLog(boolean syncFlag) break; } } else { - switch (g_hiviewConfig.outputOption) { + switch (opt) { case OUTPUT_OPTION_TEXT_FILE: OutputLog2TextFile(NULL); break; @@ -526,7 +534,7 @@ void HiviewRegisterHilogProc(HilogProc func) uint32 HiviewGetConfigOption(void) { - return g_hiviewConfig.outputOption; + return GETOPTION(g_hiviewConfig.outputOption); } void HiviewUnRegisterHilogProc(HilogProc func) -- Gitee From 35da937ad6b6cac2e6f9e18ef404ce89e83a1d59 Mon Sep 17 00:00:00 2001 From: huxia Date: Tue, 10 Aug 2021 15:36:52 +0800 Subject: [PATCH 6/6] Fix the frequent writing of hilog file headers and log current limit issues Signed-off-by: huxia Change-Id: I167f85413a73eb6eaa546060618a85be6dc6b112 Signed-off-by: huxia --- frameworks/mini/BUILD.gn | 4 ++++ frameworks/mini/hiview_log.c | 8 ++++---- frameworks/mini/hiview_output_log.c | 24 +++++++++++++++++++----- frameworks/mini/hiview_output_log.h | 3 ++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/frameworks/mini/BUILD.gn b/frameworks/mini/BUILD.gn index 3b4d3f9..4ac2d4f 100755 --- a/frameworks/mini/BUILD.gn +++ b/frameworks/mini/BUILD.gn @@ -17,6 +17,7 @@ declare_args() { ohos_hiviewdfx_hilog_lite_file_size = 8192 ohos_hiviewdfx_hilog_lite_disable_cache = false ohos_hiviewdfx_hilog_lite_limit_level_default = 30 + ohos_hiviewdfx_hilog_lite_disable_print_limit = false } config("hilog_lite_config") { @@ -42,6 +43,9 @@ static_library("hilog_lite") { if (ohos_hiviewdfx_hilog_lite_disable_cache) { defines += ["DISABLE_HILOG_CACHE"] } + if(ohos_hiviewdfx_hilog_lite_disable_print_limit) { + defines += ["DISABLE_HILOG_LITE_PRINT_LIMIT"] + } public_configs = [ "//base/hiviewdfx/hiview_lite:hiview_lite_config", ":hilog_lite_config" diff --git a/frameworks/mini/hiview_log.c b/frameworks/mini/hiview_log.c index 91ad76d..88c1930 100755 --- a/frameworks/mini/hiview_log.c +++ b/frameworks/mini/hiview_log.c @@ -113,7 +113,7 @@ void HiLogPrintf(uint8 module, uint8 level, const char *nums, const char *fmt, . } if (g_hiviewConfig.logSwitch == HIVIEW_FEATURE_OFF || !CheckParameters(module, level) || - LogIsLimited(module) || !LOG_IS_OUTPUT(module)) { + !LOG_IS_OUTPUT(module)) { return; } @@ -134,7 +134,7 @@ void HiLogPrintf(uint8 module, uint8 level, const char *nums, const char *fmt, . } va_end(args); - OutputLog((uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum); + OutputLog(module, (uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum); } void HILOG_HashPrintf(uint8 module, uint8 level, const char *nums, uint32 hash, ...) @@ -146,7 +146,7 @@ void HILOG_HashPrintf(uint8 module, uint8 level, const char *nums, uint32 hash, } if (g_hiviewConfig.logSwitch == HIVIEW_FEATURE_OFF || !CheckParameters(module, level) || - LogIsLimited(module) || !LOG_IS_OUTPUT(module)) { + !LOG_IS_OUTPUT(module)) { return; } @@ -167,7 +167,7 @@ void HILOG_HashPrintf(uint8 module, uint8 level, const char *nums, uint32 hash, } va_end(args); - OutputLog((uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum); + OutputLog(module, (uint8 *)&logContent, sizeof(HiLogCommon) + sizeof(uint32) * argsNum); } void HiLogFlush(boolean syncFlag) diff --git a/frameworks/mini/hiview_output_log.c b/frameworks/mini/hiview_output_log.c index 2e0b454..3fa39ec 100755 --- a/frameworks/mini/hiview_output_log.c +++ b/frameworks/mini/hiview_output_log.c @@ -14,16 +14,17 @@ */ #include "hiview_output_log.h" -#include "hiview_def.h" -#include "hiview_util.h" #include "hiview_cache.h" #include "hiview_config.h" +#include "hiview_def.h" #include "hiview_file.h" #include "hiview_log.h" +#include "hiview_log_limit.h" #include "hiview_service.h" +#include "hiview_util.h" #include "message.h" -#include "securec.h" #include "ohos_types.h" +#include "securec.h" #include @@ -120,7 +121,7 @@ void ClearLogOutput(void) CloseHiviewFile(&g_logFile); } -void OutputLog(const uint8 *data, uint32 len) +void OutputLog(uint8 module, const uint8 *data, uint32 len) { if (data == NULL) { return; @@ -138,6 +139,19 @@ void OutputLog(const uint8 *data, uint32 len) boolean isDisableCache = FALSE; #endif +#ifdef DISABLE_HILOG_LITE_PRINT_LIMIT + boolean isDisablePrintLimited = TRUE; +#else + boolean isDisablePrintLimited = FALSE; +#endif + boolean isLogLimited = LogIsLimited(module); + + if (!isDisablePrintLimited && isLogLimited) { + // The console output adopts the same restriction strategy as the file output, + // and the log output to the file is restricted. + return; + } + int8 opt = GETOPTION(g_hiviewConfig.outputOption); boolean isPrint = g_hiviewConfig.outputOption >= OUTPUT_OPTION_PRINT; if (opt == OUTPUT_OPTION_DEBUG || isPrint || isDisableCache) { @@ -147,7 +161,7 @@ void OutputLog(const uint8 *data, uint32 len) } } - if (opt == OUTPUT_OPTION_DEBUG || isDisableCache) { + if (opt == OUTPUT_OPTION_DEBUG || isDisableCache || isLogLimited) { return; } diff --git a/frameworks/mini/hiview_output_log.h b/frameworks/mini/hiview_output_log.h index a114f36..ec447d5 100755 --- a/frameworks/mini/hiview_output_log.h +++ b/frameworks/mini/hiview_output_log.h @@ -31,10 +31,11 @@ void ClearLogOutput(void); /** * Output the log to file. + * @param module log module. * @param data HiLogContent pointer. * @param len log data length. **/ -void OutputLog(const uint8 *data, uint32 len); +void OutputLog(uint8 module, const uint8 *data, uint32 len); /** * Get the log file size. -- Gitee