From 1a1fa59ee88136d0d9bc4a9f4fbf2f593ce4c4ea Mon Sep 17 00:00:00 2001 From: stesen Date: Sat, 11 Sep 2021 01:41:42 +0000 Subject: [PATCH 1/6] fix pointer logging Signed-off-by: stesen --- services/apphilogcat/hiview_applogcat.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index 80854d6..696d422 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -15,6 +15,7 @@ #include #include +#include #include "hilog_command.h" @@ -138,22 +139,22 @@ int main(int argc, const char **argv) FILE *fp1 = fopen(HILOG_PATH1, "at"); if (fp1 == NULL) { + printf("open err fp1 %s\n", strerror(errno)); close(fd); - printf("open err fp1=%p\n", fp1); return 0; } FILE *fp2 = fopen(HILOG_PATH2, "at"); if (fp2 == NULL) { + printf("open err fp2 %s\n", strerror(errno)); fclose(fp1); close(fd); - printf("open err fp2=%p\n", fp2); return 0; } // First select fpWrite = SelectWriteFile(&fp1, fp2); if (fpWrite == NULL) { - printf("SelectWriteFile open err fp1=%p\n", fp1); + printf("SelectWriteFile open err\n"); return 0; } while (1) { @@ -166,7 +167,7 @@ int main(int argc, const char **argv) if (NeedFlush(head->msg)) { if (FlushAndSync(fpWrite) != 0) { - printf("flush and sync file err, fpWrite=%p\n", fpWrite); + printf("flush and sync file err\n"); } continue; } @@ -202,7 +203,7 @@ int main(int argc, const char **argv) 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); + printf("[FATAL]File can't write fpWrite %s\n", strerror(errno)); return 0; } if (fpWrite == fp1) { @@ -213,7 +214,7 @@ int main(int argc, const char **argv) // select file, if file1 is full, record file2, file2 is full, record file1 fpWrite = SwitchWriteFile(&fp1, &fp2, fpWrite); if (fpWrite == NULL) { - printf("[FATAL]File can't open fp1=%p, fp2=%p\n", fp1, fp2); + printf("[FATAL]SwitchWriteFile failed\n"); return 0; } } -- Gitee From e01bd3ce9e9fc63e098f0505711b3398a760ef0c Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Sat, 25 Sep 2021 18:46:57 +0800 Subject: [PATCH 2/6] Description:[feature] support customize hilog dir Change-Id: Ic53f6ee330827af6fbcdfd40629e3e6ece9edb34 Signed-off-by: shenchenkai --- services/apphilogcat/BUILD.gn | 82 ++++++++++++------------- services/apphilogcat/hiview_applogcat.c | 7 ++- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/services/apphilogcat/BUILD.gn b/services/apphilogcat/BUILD.gn index 0d471d4..e722652 100755 --- a/services/apphilogcat/BUILD.gn +++ b/services/apphilogcat/BUILD.gn @@ -13,59 +13,59 @@ import("//build/lite/config/component/lite_component.gni") declare_args() { - ohos_hiviewdfx_hilog_file_size = 2048 - enable_ohos_hiviewdfx_apphilogcat_init_release = false - enable_ohos_hiviewdfx_apphilogcat_init_debug = true - ohos_hiviewdfx_apphilogcat_log_level_release = 5 - ohos_hiviewdfx_apphilogcat_log_level_debug = 3 - apphilogcat_on = 1 - apphilogcat_off = 0 - enable_ohos_hiviewdfx_hilogcat_build = true + ohos_hiviewdfx_hilog_file_size = 2048 + enable_ohos_hiviewdfx_apphilogcat_init_release = false + enable_ohos_hiviewdfx_apphilogcat_init_debug = true + ohos_hiviewdfx_apphilogcat_log_level_release = 5 + ohos_hiviewdfx_apphilogcat_log_level_debug = 3 + apphilogcat_on = 1 + apphilogcat_off = 0 + enable_ohos_hiviewdfx_hilogcat_build = true + ohos_hiviewdfx_apphilogcat_log_dir = "/storage/data/log" } -config("apphilogcat_config"){ - defines = [ "HILOG_MAX_FILELEN = $ohos_hiviewdfx_hilog_file_size" ] +config("apphilogcat_config") { + defines = [ "HILOG_MAX_FILELEN = $ohos_hiviewdfx_hilog_file_size" ] - if(enable_ohos_hiviewdfx_apphilogcat_init_release){ - defines += ["APPHILOGCAT_STATUS_RELEASE = $apphilogcat_on"] - }else{ - defines += ["APPHILOGCAT_STATUS_RELEASE = $apphilogcat_off"] - } + if (enable_ohos_hiviewdfx_apphilogcat_init_release) { + defines += [ "APPHILOGCAT_STATUS_RELEASE = $apphilogcat_on" ] + } else { + defines += [ "APPHILOGCAT_STATUS_RELEASE = $apphilogcat_off" ] + } - if(enable_ohos_hiviewdfx_apphilogcat_init_debug){ - defines += ["APPHILOGCAT_STATUS_DEBUG = $apphilogcat_on"] - }else{ - defines += ["APPHILOGCAT_STATUS_DEBUG = $apphilogcat_off"] - } + if (enable_ohos_hiviewdfx_apphilogcat_init_debug) { + defines += [ "APPHILOGCAT_STATUS_DEBUG = $apphilogcat_on" ] + } else { + defines += [ "APPHILOGCAT_STATUS_DEBUG = $apphilogcat_off" ] + } - defines += ["CONFIG_LOG_LEVEL_RELEASE = $ohos_hiviewdfx_apphilogcat_log_level_release"] + defines += [ + "CONFIG_LOG_LEVEL_RELEASE = $ohos_hiviewdfx_apphilogcat_log_level_release", + ] - defines += ["CONFIG_LOG_LEVEL_DEBUG = $ohos_hiviewdfx_apphilogcat_log_level_debug"] + defines += + [ "CONFIG_LOG_LEVEL_DEBUG = $ohos_hiviewdfx_apphilogcat_log_level_debug" ] + + defines += [ "HILOG_DIR = \"$ohos_hiviewdfx_apphilogcat_log_dir\"" ] } static_library("apphilogcat_static") { - sources = [ - "hiview_applogcat.c", - ] - cflags = [ "-Wall" ] - include_dirs = [ - "//third_party/bounds_checking_function/include", - ] - deps = [ - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", - "//base/hiviewdfx/hilog_lite/command:hilog_command_static" - ] - public_configs = [":apphilogcat_config"] + sources = [ "hiview_applogcat.c" ] + cflags = [ "-Wall" ] + include_dirs = [ "//third_party/bounds_checking_function/include" ] + deps = [ + "//base/hiviewdfx/hilog_lite/command:hilog_command_static", + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + ] + public_configs = [ ":apphilogcat_config" ] - if(enable_ohos_hiviewdfx_hilogcat_build) { - deps += [ "//base/hiviewdfx/hilog_lite/services/hilogcat:hilogcat" ] - } + if (enable_ohos_hiviewdfx_hilogcat_build) { + deps += [ "//base/hiviewdfx/hilog_lite/services/hilogcat:hilogcat" ] + } } lite_component("apphilogcat") { - target_type = "executable" + target_type = "executable" - features = [ - ":apphilogcat_static", - ] + features = [ ":apphilogcat_static" ] } diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index 696d422..3bf2ff9 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -20,8 +20,11 @@ #include "hilog_command.h" #define HILOG_LOGBUFFER 2048 -#define HILOG_PATH1 "/storage/data/log/hilog1.txt" -#define HILOG_PATH2 "/storage/data/log/hilog2.txt" +#ifndef HILOG_DIR +#define HILOG_DIR "/storage/data/log" +#endif +#define HILOG_PATH1 HILOG_DIR "/hilog1.txt" +#define HILOG_PATH2 HILOG_DIR "/hilog2.txt" #undef LOG_TAG #define LOG_TAG "apphilogcat" -- Gitee From 8b92329c8770e194158a845e2342776712737e25 Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Sun, 26 Sep 2021 10:19:21 +0800 Subject: [PATCH 3/6] Description:[feature] support output negative integer in hash print Change-Id: I7986aa7d8866123b36e2992f886f2ce82af16195 Signed-off-by: shenchenkai --- frameworks/mini/hiview_output_log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/mini/hiview_output_log.c b/frameworks/mini/hiview_output_log.c index d557ae1..b1924fc 100755 --- a/frameworks/mini/hiview_output_log.c +++ b/frameworks/mini/hiview_output_log.c @@ -497,7 +497,7 @@ static int32 LogValuesFmtHash(char *desStrPtr, int32 desLen, const HiLogContent for (uint32 i = 0; i < paraNum && i < LOG_MULTI_PARA_MAX; i++) { len = snprintf_s(&desStrPtr[outLen], desLen - outLen, desLen - outLen - 1, - "%u ", logContentPtr->values[i]); + "%d ", (int32)logContentPtr->values[i]); if (len < 0) { return len; } -- Gitee From 55995bd33e1a581abe73d6b02608d01c76717c28 Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Sun, 26 Sep 2021 11:11:29 +0800 Subject: [PATCH 4/6] Description:[fix] hilog file permission change to 750 Change-Id: I7fbdded6ce72631491b29a6ace1b0a1ee202babe Signed-off-by: shenchenkai --- services/apphilogcat/hiview_applogcat.c | 34 ++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index 3bf2ff9..43b171c 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -13,9 +13,13 @@ * limitations under the License. */ +#include #include +#include +#include +#include #include -#include +#include #include "hilog_command.h" @@ -32,6 +36,8 @@ static int file1Size = 0; static int file2Size = 0; +int FlushAndSync(FILE *fp); + static int FileSize(const char *filename) { FILE *fp = fopen(filename, "r"); @@ -92,7 +98,7 @@ FILE *SwitchWriteFile(FILE **fp1, FILE **fp2, FILE *curFp) } } -int FlushAndSync(FILE* fp) +int FlushAndSync(FILE *fp) { if (fp == NULL) { return 0; @@ -107,7 +113,7 @@ int FlushAndSync(FILE* fp) return 0; } -bool NeedFlush(const char* buf) +bool NeedFlush(const char *buf) { #define FLUSH_LOG_ARG_0 0 #define FLUSH_LOG_ARG_1 1 @@ -118,10 +124,16 @@ bool NeedFlush(const char* buf) return false; } +static void FileClose(FILE *file) +{ + if (file != NULL) { + fclose(file); + } +} + int main(int argc, const char **argv) { -#define HILOG_PERMMISION 0700 -#define HILOG_TEST_ARGC 2 +#define HILOG_UMASK 007 int fd; int ret; FILE *fpWrite = NULL; @@ -140,6 +152,7 @@ int main(int argc, const char **argv) return 0; } + umask(HILOG_UMASK); FILE *fp1 = fopen(HILOG_PATH1, "at"); if (fp1 == NULL) { printf("open err fp1 %s\n", strerror(errno)); @@ -150,7 +163,7 @@ int main(int argc, const char **argv) FILE *fp2 = fopen(HILOG_PATH2, "at"); if (fp2 == NULL) { printf("open err fp2 %s\n", strerror(errno)); - fclose(fp1); + FileClose(fp1); close(fd); return 0; } @@ -158,6 +171,9 @@ int main(int argc, const char **argv) fpWrite = SelectWriteFile(&fp1, fp2); if (fpWrite == NULL) { printf("SelectWriteFile open err\n"); + close(fd); + FileClose(fp1); + FileClose(fp2); return 0; } while (1) { @@ -207,6 +223,9 @@ int main(int argc, const char **argv) 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 %s\n", strerror(errno)); + close(fd); + FileClose(fp1); + FileClose(fp2); return 0; } if (fpWrite == fp1) { @@ -218,6 +237,9 @@ int main(int argc, const char **argv) fpWrite = SwitchWriteFile(&fp1, &fp2, fpWrite); if (fpWrite == NULL) { printf("[FATAL]SwitchWriteFile failed\n"); + close(fd); + FileClose(fp1); + FileClose(fp2); return 0; } } -- Gitee From b3c0945068aeec3e0310600f8798653861d20278 Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Thu, 28 Oct 2021 03:46:37 +0000 Subject: [PATCH 5/6] Description:[fix] change default file permission to 640 Signed-off-by: shenchenkai --- services/apphilogcat/hiview_applogcat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index 43b171c..6f4c26e 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -133,7 +133,7 @@ static void FileClose(FILE *file) int main(int argc, const char **argv) { -#define HILOG_UMASK 007 +#define HILOG_UMASK 0027 int fd; int ret; FILE *fpWrite = NULL; -- Gitee From 3a2d56ff13dc4ab45cf58927d24ac72d85739526 Mon Sep 17 00:00:00 2001 From: shenchenkai Date: Wed, 27 Oct 2021 09:17:39 +0000 Subject: [PATCH 6/6] support output millisecond in hilog Signed-off-by: shenchenkai Change-Id: If14a0b62f581bca3404829636a0ab61cdab4d591 --- frameworks/mini/hiview_log.c | 8 ++++++-- frameworks/mini/hiview_output_log.c | 7 +++---- interfaces/native/kits/hilog_lite/hiview_log.h | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frameworks/mini/hiview_log.c b/frameworks/mini/hiview_log.c index 57894c5..76c8927 100755 --- a/frameworks/mini/hiview_log.c +++ b/frameworks/mini/hiview_log.c @@ -124,7 +124,9 @@ void HiLogPrintf(uint8 module, uint8 level, const char *nums, const char *fmt, . pCommon->fmt = fmt; pCommon->valueNumber = (uint8)argsNum; pCommon->task = (uint8)HIVIEW_GetTaskId(); - pCommon->time = (uint32)(HIVIEW_GetCurrentTime() / MS_PER_SECOND); + uint64 cur = HIVIEW_GetCurrentTime(); + pCommon->time = (uint32)(cur / MS_PER_SECOND); + pCommon->milli = (uint16)(cur % MS_PER_SECOND); uint8 i = 0; va_list args; @@ -157,7 +159,9 @@ void HILOG_HashPrintf(uint8 module, uint8 level, const char *nums, uint32 hash, pCommon->fmt = (const char*)hash; pCommon->valueNumber = (uint8)argsNum; pCommon->task = (uint8)HIVIEW_GetTaskId(); - pCommon->time = (uint32)(HIVIEW_GetCurrentTime() / MS_PER_SECOND); + uint64 cur = HIVIEW_GetCurrentTime(); + pCommon->time = (uint32)(cur / MS_PER_SECOND); + pCommon->milli = (uint16)(cur % MS_PER_SECOND); uint8 i = 0; va_list args; diff --git a/frameworks/mini/hiview_output_log.c b/frameworks/mini/hiview_output_log.c index b1924fc..3735b7d 100755 --- a/frameworks/mini/hiview_output_log.c +++ b/frameworks/mini/hiview_output_log.c @@ -368,21 +368,20 @@ static int32 LogCommonFmt(char *outStr, int32 outStrLen, const HiLogCommon *comm { int32 ret; time_t time; - uint32 year, month, day, hour, min, sec; + uint32 month, day, hour, min, sec; uint8_t level; struct tm nowTime = {0}; time = commonContentPtr->time; localtime_r(&time, &nowTime); - year = nowTime.tm_year + 1900; month = nowTime.tm_mon + 1; day = nowTime.tm_mday; hour = nowTime.tm_hour; min = nowTime.tm_min; sec = nowTime.tm_sec; level = CLEAR_HASH_FLAG(commonContentPtr->level); - ret = snprintf_s(outStr, outStrLen, outStrLen - 1, "%04d-%02d-%02d %02d:%02d:%02d 0 %d %c %d/%s: ", - year, month, day, hour, min, sec, commonContentPtr->task, g_logLevelInfo[level], + ret = snprintf_s(outStr, outStrLen, outStrLen - 1, "%02d-%02d %02d:%02d:%02d.%03d 0 %d %c %d/%s: ", + month, day, hour, min, sec, commonContentPtr->milli, commonContentPtr->task, g_logLevelInfo[level], commonContentPtr->module, HiLogGetModuleName(commonContentPtr->module)); return ret; diff --git a/interfaces/native/kits/hilog_lite/hiview_log.h b/interfaces/native/kits/hilog_lite/hiview_log.h index 90892e2..7dc7798 100755 --- a/interfaces/native/kits/hilog_lite/hiview_log.h +++ b/interfaces/native/kits/hilog_lite/hiview_log.h @@ -140,6 +140,7 @@ typedef struct { uint8 valueNumber : 4; uint8 task; uint32 time; /* seconds */ + uint16 milli; /* millisecond, 0-999 */ const char *fmt; } HiLogCommon; -- Gitee