From d3f70487423cebc3ba79132bb3729309037c4406 Mon Sep 17 00:00:00 2001 From: huxia Date: Fri, 9 Apr 2021 10:46:57 +0800 Subject: [PATCH] Optimize hilog command Change-Id: Ifa791db6bec386540d0481607b26d6fd4805e37a --- command/hilog_command.c | 221 ++++++++++++++++++---------------- command/hilog_command.h | 33 +---- services/apphilogcat/BUILD.gn | 2 +- 3 files changed, 121 insertions(+), 135 deletions(-) diff --git a/command/hilog_command.c b/command/hilog_command.c index 4a727d0..4ec4686 100644 --- a/command/hilog_command.c +++ b/command/hilog_command.c @@ -22,8 +22,36 @@ #include #include +#define OPTION_TAG '-' +#define OPTION_LIST 'l' +#define OPTION_SET 'C' +#define OPTION_HELP 'h' + +#define PARA_LEVEL "level" +#define PARA_LEVEL_LEN 5 +#define PARA_MODULE "mod" +#define PARA_MODULE_LEN 3 +#define PARA_AUTO "auto" +#define PARA_AUTO_LEN 4 + #define APPHILOGCAT_OFF 0 +#define HIVIEW_FEATURE_ON 1 +#define HIVIEW_FEATURE_OFF 0 + +#define ARG2 1 +#define ARG3 2 +#define ARG4 3 +#define ARG5 4 +#define ARG6 5 + +#define NUMBER_ARG_2 2 +#define NUMBER_ARG_3 3 +#define NUMBER_ARG_4 4 +#define NUMBER_ARG_6 6 + +#define HILOG_MODULE_MAX "FFFFF" + char g_logLevelInfo[HILOG_LEVEL_MAX] = { 'N', // "NONE" 'N', // "NONE" @@ -35,42 +63,26 @@ char g_logLevelInfo[HILOG_LEVEL_MAX] = { 'F' // "FATAL" }; -char g_logModuleInfo[HILOG_MODULE_MAX_NUM][DOMIAN_ID_LENTH] = { - "00000", - "01100", // "NONE" - "01200", // "NONE" - "01300", // "NONE" - "01400", // "NONE" - "01500", // "NONE" - "01600", // "NONE" - "01700", // "NONE" - "01800", // "NONE" - "01900", // "NONE" - "02500", // "NONE" - "02600", // "NONE" - "02D00", // "NONE" -}; - HiviewConfig g_hiviewConfig = { .outputOption = OUTPUT_OPTION_FLOW, .level = LOG_DEBUG, .logSwitch = HIVIEW_FEATURE_ON, .dumpSwitch = HIVIEW_FEATURE_OFF, .eventSwitch = HIVIEW_FEATURE_OFF, + .logOutputModule = HILOG_MODULE_MAX, }; void HilogHelpProc(const char* tag) { printf("%s [-h] [-l level/mod] [-C level <1>] [-C mod <3>]\n", tag); - printf(" -h Help\n"); - printf(" -l Query the level and module definition information\n"); - printf(" -l level Query the level definition information\n"); - printf(" -l mod Query the level definition information\n"); - printf(" -C Enable all level logs of all modules\n"); - printf(" -C level Set the lowest log level\n"); - printf(" -C mod Enable the logs of a specified module and disable other modules\n"); - printf(" -C auto Set log level with predefined macro\n"); - printf(" -f Enable the logs to a specified file\n"); + printf(" -h Help\n"); + printf(" -l Query the level definition information\n"); + printf(" -C Enable all level logs of all modules\n"); + printf(" -C auto Set log level with predefined macro\n"); + printf(" -C level Set the lowest log level\n"); + printf(" -C mod Enable the logs of a specified module and disable other modules\n"); + printf(" -C mod level Set the lowest log level and enable specified module\n"); + printf(" -C level mod Set the lowest log level and enable specified module\n"); } void ListLevelInfo(void) @@ -83,32 +95,12 @@ void ListLevelInfo(void) printf(" 7 - FATAL\n"); } -void ListModuleInfo(void) -{ - int i, ret; - char modInfo[STR_MAX_LEN] = { '\0' }; - printf("======Module Information======\n"); - for (i = 0; i < HILOG_MODULE_MAX_NUM; i++) { - if (g_logModuleInfo[i][0] == 0) { - break; - } - ret = snprintf_s(modInfo, sizeof(modInfo), sizeof(modInfo) - 1, " %d - %s\n", i, g_logModuleInfo[i]); - if (ret > 0) { - modInfo[ret] = '\0'; - printf((const char *)modInfo); - } - } -} - -void HilogListProc(const char *cmd) +void HilogListProc(int argc, const char **argv) { - if (cmd == NULL) { + if (argc == 2) { ListLevelInfo(); - ListModuleInfo(); - } else if (strncmp(cmd, PARA_LEVEL, PARA_LEVEL_LEN) == 0) { - ListLevelInfo(); - } else if (strncmp(cmd, PARA_MODULE, PARA_MODULE_LEN) == 0) { - ListModuleInfo(); + } else { + printf("Invalid command.\n"); } } @@ -116,21 +108,19 @@ bool SetLogLevel(unsigned char level) { if (level >= LOG_DEBUG && level < HILOG_LEVEL_MAX) { g_hiviewConfig.level = level; + printf("Set log level : %d \n", level); return true; } return false; } -void SetLogOutputModule(unsigned char mod) +void SetOutputModule(const char *mod) { - g_hiviewConfig.logOutputModule = mod; -} - -void SetOutputModule(const char *cmd) -{ - char *endPtr = NULL; - int mod = strtol(cmd, &endPtr, 0); - SetLogOutputModule((unsigned char)mod); + if ((mod == NULL) || (strlen(mod) != (DOMAIN_ID_LENGTH - 1))) { + printf("Invalid command.\n"); + return; + } + strncpy_s(g_hiviewConfig.logOutputModule, mod, DOMAIN_ID_LENGTH); } void SetOutputLevel(const char *cmd) @@ -149,65 +139,93 @@ void SetOutputLevel(const char *cmd) printf("Set the log output level failure level=%d.\n", level); } -int HilogSetProc(const char *option, const char *attr) +int ProcAutoSet(void) { - if (option == NULL) { - SetLogLevel(LOG_DEBUG); - SetLogOutputModule(HILOG_MODULE_MAX_NUM); - } else if (strncmp(option, PARA_LEVEL, PARA_LEVEL_LEN) == 0) { - SetOutputLevel(attr); - } else if (strncmp(option, PARA_MODULE, PARA_MODULE_LEN) == 0) { - SetOutputModule(attr); - } else if (strncmp(option, PARA_AUTO, PARA_AUTO_LEN) == 0) { #ifdef OHOS_RELEASE - #if APPHILOGCAT_STATUS_RELEASE == APPHILOGCAT_OFF - printf("Applogcat Off \n"); - return 0; - #else - SetLogLevel(CONFIG_LOG_LEVEL_RELEASE); - printf("Default log level: %d \n", CONFIG_LOG_LEVEL_RELEASE); - #endif +#if APPHILOGCAT_STATUS_RELEASE == APPHILOGCAT_OFF + return 0; +#else + SetLogLevel(CONFIG_LOG_LEVEL_RELEASE); + printf("Default log level: %d \n", CONFIG_LOG_LEVEL_RELEASE); +#endif #else // OHOS_DEBUG - #if APPHILOGCAT_STATUS_DEBUG == APPHILOGCAT_OFF - printf("Applogcat Off \n"); - return 0; - #else - SetLogLevel(CONFIG_LOG_LEVEL_DEBUG); - printf("Default log level: %d \n", CONFIG_LOG_LEVEL_DEBUG); - #endif +#if APPHILOGCAT_STATUS_DEBUG == APPHILOGCAT_OFF + return 0; +#else + SetLogLevel(CONFIG_LOG_LEVEL_DEBUG); + printf("Default log level: %d \n", CONFIG_LOG_LEVEL_DEBUG); +#endif #endif - } - printf("Applogcat On \n"); return 1; } +int HilogSetProc(int argc, const char **argv) +{ + switch (argc) { + case NUMBER_ARG_2: // XXhilogcat -C + SetLogLevel(LOG_DEBUG); + SetOutputModule(HILOG_MODULE_MAX); + return 1; + case NUMBER_ARG_3: // XXhilogcat -C auto + if (strncmp(&argv[ARG3][0], PARA_AUTO, PARA_AUTO_LEN) == 0) { + return ProcAutoSet(); + } + break; + case NUMBER_ARG_6: // XXhilogcat -C mod level or XXhilogcat -C level mod + if (strncmp(&argv[ARG5][0], PARA_LEVEL, PARA_LEVEL_LEN) == 0) { + SetOutputLevel(&argv[ARG6][0]); + } else if (strncmp(&argv[ARG5][0], PARA_MODULE, PARA_MODULE_LEN) == 0) { + SetOutputModule(&argv[ARG6][0]); + } else { + printf("Invalid command.\n"); + return 0; + } + // fall through + case NUMBER_ARG_4: // XXhilogcat -C level or XXhilogcat -C mod + if (strncmp(&argv[ARG3][0], PARA_LEVEL, PARA_LEVEL_LEN) == 0) { + SetOutputLevel(&argv[ARG4][0]); + } else if (strncmp(&argv[ARG3][0], PARA_MODULE, PARA_MODULE_LEN) == 0) { + SetOutputModule(&argv[ARG4][0]); + } else { + printf("Invalid command.\n"); + return 0; + } + return 1; + default: + break; + } + printf("Invalid command.\n"); + return 0; +} + int HilogCmdProc(const char* tag, int argc, const char **argv) { -#define ARG2 1 -#define ARG3 2 -#define ARG4 3 + if (argc <= 1) { + printf("Invalid command.\n"); + return 0; + } int i = 0; - + int ret = 0; if (argv[ARG2][i++] == OPTION_TAG) { switch (argv[ARG2][i++]) { case OPTION_HELP: HilogHelpProc(tag); - return 0; + break; case OPTION_LIST: - HilogListProc(&argv[ARG3][0]); - return 0; + HilogListProc(argc, argv); + break; case OPTION_SET: - return HilogSetProc(&argv[ARG3][0], &argv[ARG4][0]); - case OPTION_2_FILE: - return 0; + ret = HilogSetProc(argc, argv); + break; default: printf("Invalid command.\n"); - return 0; + break; } + } else { + printf("Invalid command.\n"); } - printf("Invalid command.\n"); - return 0; + return ret; } bool FilterLevelLog(unsigned char setLevel, unsigned char logLevel) @@ -226,15 +244,10 @@ bool FilterLevelLog(unsigned char setLevel, unsigned char logLevel) return false; } -bool FilterModuleLog(unsigned char setModule, const char *logModule) +bool FilterModuleLog(unsigned char *setModule, const char *logModule) { - if (setModule == HILOG_MODULE_MAX_NUM) { + if (strncmp(setModule, HILOG_MODULE_MAX, DOMAIN_ID_LENGTH) == 0) { return true; } - int ret = strncmp(logModule, g_logModuleInfo[setModule], DOMAIN_LENGTH); - // If module = setmodule, may print log - if (ret == 0) { - return true; - } - return false; + return strncmp(logModule, g_hiviewConfig.logOutputModule, DOMAIN_ID_LENGTH) == 0; } \ No newline at end of file diff --git a/command/hilog_command.h b/command/hilog_command.h index db5b812..493819a 100644 --- a/command/hilog_command.h +++ b/command/hilog_command.h @@ -26,27 +26,7 @@ extern "C" { #endif #endif /* End of #ifdef __cplusplus */ -#define CMD_MIN_LEN 2 -#define CMD_MAX_LEN 32 -#define CMD_HILOGCAT "hilog" -#define CMD_HIEVENT "hievent" -#define OPTION_TAG '-' -#define OPTION_LIST 'l' -#define OPTION_SET 'C' -#define OPTION_SIMULATE 's' -#define OPTION_HELP 'h' -#define OPTION_2_FILE 'f' -#define OPTION_START 't' -#define OPTION_UART 'R' - -#define PARA_LEVEL "level" -#define PARA_LEVEL_LEN 5 -#define PARA_MODULE "mod" -#define PARA_MODULE_LEN 3 -#define PARA_AUTO "auto" -#define PARA_AUTO_LEN 4 -#define OP_ASSIGN '=' -#define STR_MAX_LEN 128 +#define DOMAIN_ID_LENGTH 6 typedef struct { const unsigned char outputOption : 4; /* Control log output mode. Cannot be modified during running. */ @@ -55,7 +35,7 @@ typedef struct { unsigned char logSwitch : 1; /* Indicates whether to enable the log component. */ unsigned char eventSwitch : 1; /* Indicates whether to enable the event component. */ unsigned char dumpSwitch : 1; /* Indicates whether to enable the dump component. */ - unsigned char logOutputModule; /* Control log output module. */ + unsigned char logOutputModule[DOMAIN_ID_LENGTH]; /* Control log output module. */ unsigned short writeFailureCount; } HiviewConfig; @@ -67,18 +47,11 @@ typedef enum { OUTPUT_OPTION_MAX } HiviewOutputOption; -#define HIVIEW_FEATURE_ON 1 -#define HIVIEW_FEATURE_OFF 0 -#define HILOG_MODULE_ALL 0xff - -#define HILOG_MODULE_MAX_NUM 50 -#define DOMIAN_ID_LENTH 6 - extern HiviewConfig g_hiviewConfig; int HilogCmdProc(const char* tag, int argc, const char **argv); bool FilterLevelLog(unsigned char setLevel, unsigned char logLevel); -bool FilterModuleLog(unsigned char setModule, const char *logModule); +bool FilterModuleLog(unsigned char* setModule, const char *logModule); #ifdef __cplusplus #if __cplusplus diff --git a/services/apphilogcat/BUILD.gn b/services/apphilogcat/BUILD.gn index 6a99899..0ef4350 100755 --- a/services/apphilogcat/BUILD.gn +++ b/services/apphilogcat/BUILD.gn @@ -14,7 +14,7 @@ import("//build/lite/config/component/lite_component.gni") declare_args() { ohos_hiviewdfx_hilog_file_size = 2048 - enable_ohos_hiviewdfx_apphilogcat_init_release = true + 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 -- Gitee