diff --git a/command/BUILD.gn b/command/BUILD.gn index 4beddd431dc1c04a5510fe8d358b801c95688b27..1fec2c5f00ac0d463f263e516f5a91b23578989d 100755 --- a/command/BUILD.gn +++ b/command/BUILD.gn @@ -11,13 +11,42 @@ # See the License for the specific language governing permissions and # limitations under the License. -static_library("hilog_lite_command") { +import("//build/lite/config/component/lite_component.gni") + +config("hilog_command_config"){ + include_dirs = [ + "//base/hiviewdfx/hilog_lite/command" + ] +} + +lite_library("hilog_command_static") { + target_type = "static_library" sources = [ - "hilog_lite_command.c", + "hilog_command.c", ] cflags = [ "-Wall" ] - include_dirs = [ - "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", - "//base/hiviewdfx/hiview_lite", + public_configs = [ + ":hilog_command_config", + "//base/hiviewdfx/hilog_lite/services/apphilogcat:apphilogcat_config" + ] + deps = [ + "//third_party/bounds_checking_function:libsec_shared", + "//base//hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" + ] +} + +lite_library("hilog_command_shared") { + target_type = "shared_library" + sources = [ + "hilog_command.c", + ] + cflags = [ "-Wall" ] + public_configs = [ + ":hilog_command_config", + "//base/hiviewdfx/hilog_lite/services/apphilogcat:apphilogcat_config" + ] + deps = [ + "//third_party/bounds_checking_function:libsec_shared", + "//base//hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" ] } diff --git a/command/hilog_command.c b/command/hilog_command.c new file mode 100644 index 0000000000000000000000000000000000000000..4a727d0bfe76923c8ee4ab2ba4c2933327623567 --- /dev/null +++ b/command/hilog_command.c @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hilog_command.h" + +#include +#include +#include +#include +#include +#include + +#define APPHILOGCAT_OFF 0 + +char g_logLevelInfo[HILOG_LEVEL_MAX] = { + 'N', // "NONE" + 'N', // "NONE" + 'N', // "NONE" + 'D', // "DEBUG" + 'I', // "INFO" + 'W', // "WARN" + 'E', // "ERROR" + '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, +}; + +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"); +} + +void ListLevelInfo(void) +{ + printf("======Level Information======\n"); + printf(" 3 - DEBUG\n"); + printf(" 4 - INFO\n"); + printf(" 5 - WARN\n"); + printf(" 6 - ERROR\n"); + 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) +{ + if (cmd == NULL) { + ListLevelInfo(); + ListModuleInfo(); + } else if (strncmp(cmd, PARA_LEVEL, PARA_LEVEL_LEN) == 0) { + ListLevelInfo(); + } else if (strncmp(cmd, PARA_MODULE, PARA_MODULE_LEN) == 0) { + ListModuleInfo(); + } +} + +bool SetLogLevel(unsigned char level) +{ + if (level >= LOG_DEBUG && level < HILOG_LEVEL_MAX) { + g_hiviewConfig.level = level; + return true; + } + return false; +} + +void SetLogOutputModule(unsigned char mod) +{ + g_hiviewConfig.logOutputModule = mod; +} + +void SetOutputModule(const char *cmd) +{ + char *endPtr = NULL; + int mod = strtol(cmd, &endPtr, 0); + SetLogOutputModule((unsigned char)mod); +} + +void SetOutputLevel(const char *cmd) +{ + char *endPtr = NULL; + int level; + if (cmd != NULL) { + level = strtol(cmd, &endPtr, 0); + } else { + level = LOG_DEBUG; + } + if (SetLogLevel((unsigned char)level) == true) { + printf("Set the log output level success.\n"); + return; + } + printf("Set the log output level failure level=%d.\n", level); +} + +int HilogSetProc(const char *option, const char *attr) +{ + 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 +#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 +#endif + } + printf("Applogcat On \n"); + return 1; +} + +int HilogCmdProc(const char* tag, int argc, const char **argv) +{ +#define ARG2 1 +#define ARG3 2 +#define ARG4 3 + + int i = 0; + + if (argv[ARG2][i++] == OPTION_TAG) { + switch (argv[ARG2][i++]) { + case OPTION_HELP: + HilogHelpProc(tag); + return 0; + case OPTION_LIST: + HilogListProc(&argv[ARG3][0]); + return 0; + case OPTION_SET: + return HilogSetProc(&argv[ARG3][0], &argv[ARG4][0]); + case OPTION_2_FILE: + return 0; + default: + printf("Invalid command.\n"); + return 0; + } + } + printf("Invalid command.\n"); + return 0; +} + +bool FilterLevelLog(unsigned char setLevel, unsigned char logLevel) +{ + int logMinLevel; + + for (logMinLevel = LOG_DEBUG; logMinLevel < HILOG_LEVEL_MAX; logMinLevel++) { + if (logLevel == g_logLevelInfo[logMinLevel]) { + break; + } + } + // Loglevel >= set level, may print log + if (logMinLevel >= setLevel) { + return true; + } + return false; +} + +bool FilterModuleLog(unsigned char setModule, const char *logModule) +{ + if (setModule == HILOG_MODULE_MAX_NUM) { + return true; + } + int ret = strncmp(logModule, g_logModuleInfo[setModule], DOMAIN_LENGTH); + // If module = setmodule, may print log + if (ret == 0) { + return true; + } + return false; +} \ No newline at end of file diff --git a/command/hilog_lite_command.h b/command/hilog_command.h old mode 100755 new mode 100644 similarity index 32% rename from command/hilog_lite_command.h rename to command/hilog_command.h index c5acce63aa49fe56ba66d5a6eb8be4c80d167ed9..db5b8120891da5fd7ef749abb1160e4aafa4b3a6 --- a/command/hilog_lite_command.h +++ b/command/hilog_command.h @@ -16,7 +16,9 @@ #ifndef HOS_LITE_HIVIEW_COMMAND_H #define HOS_LITE_HIVIEW_COMMAND_H -#include "ohos_types.h" +#include + +#include "log.h" #ifdef __cplusplus #if __cplusplus @@ -24,42 +26,59 @@ extern "C" { #endif #endif /* End of #ifdef __cplusplus */ -void HilogCmdProc(const char *cmd); -void HieventCmdProc(const char *cmd); -void DumpCmdProc(const char *cmd); -/** - * Dynamically adjust the Log Output Level. - * @param level log level. Logs of this level or higher will be generated. - * @return TRUE/FALSE - **/ -boolean SetLogLevel(uint8 level); +#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 + +typedef struct { + const unsigned char outputOption : 4; /* Control log output mode. Cannot be modified during running. */ + unsigned char hiviewInited : 1; /* Indicates whether the hiview service is inited. */ + unsigned char level : 3; /* Control log output level. HILOG_LV_XXX */ + 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 short writeFailureCount; +} HiviewConfig; + +typedef enum { + OUTPUT_OPTION_DEBUG = 0, /* Output to the UART without buffer. Commercial versions are forbidden. */ + OUTPUT_OPTION_FLOW, /* Output to UART via SAMR */ + OUTPUT_OPTION_TEXT_FILE, + OUTPUT_OPTION_BIN_FILE, + OUTPUT_OPTION_MAX +} HiviewOutputOption; -/** - * Enable or disable the log function. - * @param flag HIVIEW_FEATURE_ON/HIVIEW_FEATURE_OFF - **/ -void SwitchLog(uint8 flag); +#define HIVIEW_FEATURE_ON 1 +#define HIVIEW_FEATURE_OFF 0 +#define HILOG_MODULE_ALL 0xff -/** - * Enable the log output of a specified module. - * @param mod module id. - * @return TRUE/FALSE - **/ -boolean OpenLogOutputModule(uint8 mod); +#define HILOG_MODULE_MAX_NUM 50 +#define DOMIAN_ID_LENTH 6 -/** - * Disable the log output of a specified module. - * @param mod module id. - * @return TRUE/FALSE - **/ -boolean CloseLogOutputModule(uint8 mod); +extern HiviewConfig g_hiviewConfig; -/** - * Enable the log output of a specified module and disable the log output of other modules. - * @param mod module id. If the value of the module id is HILOG_MODULE_MAX, the log output of all modules is enabled. - * @return TRUE/FALSE - **/ -boolean SetLogOutputModule(uint8 mod); +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); #ifdef __cplusplus #if __cplusplus diff --git a/command/hilog_lite_command.c b/command/hilog_lite_command.c deleted file mode 100755 index 060d521d1f5f12dcd2d4fedeb35271cfd14b31a6..0000000000000000000000000000000000000000 --- a/command/hilog_lite_command.c +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright (c) 2020 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "hilog_lite_command.h" - -#include - -#include "securec.h" -#include "ohos_types.h" -#include "hiview_util.h" -#include "hiview_config.h" -#include "hiview_log.h" - -#define CMD_MIN_LEN 2 -#define CMD_MAX_LEN 32 -#define CMD_HILOGCAT "hilog" -#define OPTION_TAG '-' -#define OPTION_LIST 'l' -#define OPTION_SET 'c' -#define OPTION_HELP 'h' -#define PARA_LEVEL " level" -#define PARA_LEVEL_LEN 6 -#define PARA_MODULE " mod" -#define PARA_MODULE_LEN 4 -#define OP_ASSIGN '=' -#define STR_MAX_LEN 128 - -static boolean CheckCmdStr(const char *cmd); -static void HilogHelpProc(void); -static void HilogListProc(const char *cmd); -static void HilogSetProc(const char *cmd); -static void ListLevelInfo(void); -static void ListModuleInfo(void); -static void SetOutputLevel(const char *cmd); -static void SetOutputModule(const char *cmd); - -/* Command does not contain the "hilogcat". */ -void HilogCmdProc(const char *cmd) -{ - if (cmd == NULL) { - return; - } - - int32 len = strnlen(cmd, CMD_MAX_LEN + 1); - if (len < CMD_MIN_LEN || len > CMD_MAX_LEN || (CheckCmdStr(cmd) == FALSE)) { - HIVIEW_UartPrint("Invalid command.\n"); - return; - } - - if (*cmd != OPTION_TAG) { - HIVIEW_UartPrint("Invalid command.\n"); - return; - } - - switch (*(++cmd)) { - case OPTION_HELP: - HilogHelpProc(); - return; - case OPTION_LIST: - HilogListProc(++cmd); - return; - case OPTION_SET: - HilogSetProc(++cmd); - return; - default: - HIVIEW_UartPrint("Invalid command.\n"); - return; - } -} - -static void HilogHelpProc(void) -{ - HIVIEW_UartPrint("hilog [-h] [-l level/mod] [-c level=<1>] [-c mod=<3>]\n"); - HIVIEW_UartPrint(" -h Help\n"); - HIVIEW_UartPrint(" -l Query the level and module definition information\n"); - HIVIEW_UartPrint(" -l level Query the level definition information\n"); - HIVIEW_UartPrint(" -l mod Query the level definition information\n"); - HIVIEW_UartPrint(" -c Enable all level logs of all modules\n"); - HIVIEW_UartPrint(" -c level= Set the lowest log level\n"); - HIVIEW_UartPrint(" -c mod= Enable the logs of a specified module and disable other modules\n"); -} - -static void HilogListProc(const char *cmd) -{ - if (*cmd == '\0') { - ListLevelInfo(); - ListModuleInfo(); - } else if (strncmp(cmd, PARA_LEVEL, PARA_LEVEL_LEN) == 0) { - ListLevelInfo(); - } else if (strncmp(cmd, PARA_MODULE, PARA_MODULE_LEN) == 0) { - ListModuleInfo(); - } -} - -static void HilogSetProc(const char *cmd) -{ - if (*cmd == '\0') { - SetLogLevel(HILOG_LV_DEBUG); - SetLogOutputModule(HILOG_MODULE_MAX); - } else if (strncmp(cmd, PARA_LEVEL, PARA_LEVEL_LEN) == 0) { - SetOutputLevel(cmd + PARA_LEVEL_LEN); - } else if (strncmp(cmd, PARA_MODULE, PARA_MODULE_LEN) == 0) { - SetOutputModule(cmd + PARA_MODULE_LEN); - } -} - -static void ListLevelInfo(void) -{ - HIVIEW_UartPrint("======Level Information======\n"); - HIVIEW_UartPrint(" 1 - DEBUG\n"); - HIVIEW_UartPrint(" 2 - INFO\n"); - HIVIEW_UartPrint(" 3 - WARN\n"); - HIVIEW_UartPrint(" 4 - ERROR\n"); - HIVIEW_UartPrint(" 5 - FATAL\n"); - HIVIEW_UartPrint(" 6 - CLOSE LOG\n"); -} - -static void ListModuleInfo(void) -{ - char modInfo[STR_MAX_LEN] = { '\0' }; - HIVIEW_UartPrint("======Module Information======\n"); - for (int i = 0; i < HILOG_MODULE_MAX; i++) { - int ret = snprintf_s(modInfo, sizeof(modInfo), sizeof(modInfo) - 1, - " %d - %s\n", i, HiLogGetModuleName(i)); - if (ret > 0) { - modInfo[ret] = '\0'; - HIVIEW_UartPrint((const char *) modInfo); - } - } -} - -static void SetOutputLevel(const char *cmd) -{ - if (*cmd != OP_ASSIGN) { - HIVIEW_UartPrint("Set the log output level failure.\n"); - return; - } - - char *endPtr = NULL; - errno = 0; - int32 level = strtol(++cmd, &endPtr, 0); - if ((endPtr == NULL) || (cmd == endPtr) || (*endPtr != 0) || (errno == ERANGE)) { - HIVIEW_UartPrint("Set the log output level call strtol failure!\n"); - return; - } - if (SetLogLevel((uint8)level) == TRUE) { - HIVIEW_UartPrint("Set the log output level success.\n"); - return; - } -} - -static void SetOutputModule(const char *cmd) -{ - if (*cmd != OP_ASSIGN) { - HIVIEW_UartPrint("Set the log output module failure.\n"); - return; - } - - char *endPtr = NULL; - errno = 0; - int32 mod = strtol(++cmd, &endPtr, 0); - if ((endPtr == NULL) || (cmd == endPtr) || (*endPtr != 0) || (errno == ERANGE)) { - HIVIEW_UartPrint("Set the log output module call strtol failure!\n"); - return; - } - if (SetLogOutputModule((uint8)mod) == TRUE) { - HIVIEW_UartPrint("Set the log output module success.\n"); - return; - } -} - -static boolean CheckCmdStr(const char *cmd) -{ - while (*cmd != '\0') { - if (!(isalnum(*cmd) || (*cmd == ' ') || (*cmd == '\n') || - (*cmd == '=') || (*cmd == '-'))) { - return FALSE; - } - cmd++; - } - return TRUE; -} - -boolean SetLogLevel(uint8 level) -{ - if (level >= HILOG_LV_DEBUG && level < HILOG_MODULE_MAX) { - g_hiviewConfig.level = level; - return TRUE; - } - return FALSE; -} - -void SwitchLog(uint8 flag) -{ - (flag == HIVIEW_FEATURE_ON) ? (g_hiviewConfig.logSwitch = HIVIEW_FEATURE_ON) : - (g_hiviewConfig.logSwitch = HIVIEW_FEATURE_OFF); -} - -boolean OpenLogOutputModule(uint8 mod) -{ - if (mod < HILOG_MODULE_MAX) { - g_hiviewConfig.logOutputModule |= (1 << mod); - return TRUE; - } - return FALSE; -} - -boolean CloseLogOutputModule(uint8 mod) -{ - if (mod < HILOG_MODULE_MAX) { - g_hiviewConfig.logOutputModule &= (~(1 << mod)); - return TRUE; - } - return FALSE; -} - -boolean SetLogOutputModule(uint8 mod) -{ - if (mod < HILOG_MODULE_MAX) { - g_hiviewConfig.logOutputModule &= (1 << mod); - return TRUE; - } else if (mod == HILOG_MODULE_MAX) { - g_hiviewConfig.logOutputModule = UINT64_MAX; - return TRUE; - } else { - return FALSE; - } -} diff --git a/services/apphilogcat/BUILD.gn b/services/apphilogcat/BUILD.gn index 62de6c1edbdadd090e6580ebd72bfb48043647b1..6a998990f8ced3566aeac5c9db846829e3db8583 100755 --- a/services/apphilogcat/BUILD.gn +++ b/services/apphilogcat/BUILD.gn @@ -14,6 +14,32 @@ 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_debug = true + ohos_hiviewdfx_apphilogcat_log_level_release = 5 + ohos_hiviewdfx_apphilogcat_log_level_debug = 3 + apphilogcat_on = 1 + apphilogcat_off = 0 +} + +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_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_DEBUG = $ohos_hiviewdfx_apphilogcat_log_level_debug"] } static_library("apphilogcat_static") { @@ -25,9 +51,10 @@ static_library("apphilogcat_static") { "//third_party/bounds_checking_function/include", ] deps = [ - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//base/hiviewdfx/hilog_lite/command:hilog_command_static" ] - defines = [ "HILOG_MAX_FILELEN = $ohos_hiviewdfx_hilog_file_size" ] + public_configs = [":apphilogcat_config"] } lite_component("apphilogcat") { diff --git a/services/apphilogcat/hiview_applogcat.c b/services/apphilogcat/hiview_applogcat.c index 34d59e32842c50a567c1bb1a1e247af511346656..963e6fec2508e1854611bd0290ec32cd0401b9eb 100755 --- a/services/apphilogcat/hiview_applogcat.c +++ b/services/apphilogcat/hiview_applogcat.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -23,13 +24,17 @@ #include #include #include -#include "log.h" +#include "hilog_command.h" +#include "log.h" #define HILOG_LOGBUFFER 2048 #define HILOG_PATH1 "/storage/data/log/hilog1.txt" #define HILOG_PATH2 "/storage/data/log/hilog2.txt" +#undef LOG_TAG +#define LOG_TAG "apphilogcat" + static int FileSize(const char *filename) { FILE *fp = fopen(filename, "r"); @@ -94,15 +99,15 @@ int main(int argc, const char **argv) int fd; int ret; FILE *fpWrite = NULL; - if (argc == 1) { -#ifdef OHOS_RELEASE - return 0; -#endif - } - if (argc == HILOG_TEST_ARGC) { - HILOG_ERROR(LOG_CORE, "TEST = %d,%s,%d\n", argc, "hilog test", argc); - return 0; + bool printFlag = true; + + if (argc > 1) { + ret = HilogCmdProc(LOG_TAG, argc, argv); + if (ret == 0) { + return 0; + } } + fd = open(HILOG_DRIVER, O_RDONLY); if (fd < 0) { printf("hilog fd failed fd=%d\n", fd); @@ -143,6 +148,17 @@ int main(int argc, const char **argv) rawtime = (time_t)sec; /* Get GMT time */ info = gmtime(&rawtime); + + printFlag = FilterLevelLog(g_hiviewConfig.level, *(head->msg)); + if (!printFlag) { + continue; + } +#define MODULE_OFFSET 2 + printFlag = FilterModuleLog(g_hiviewConfig.logOutputModule, (head->msg) + MODULE_OFFSET); + if (!printFlag) { + continue; + } + if (info == NULL) { continue; } diff --git a/services/hilogcat/BUILD.gn b/services/hilogcat/BUILD.gn index d0e3ee0dc15f8d5cc624c39e68e589f2e9862d6e..29640b578c787a74dd07a15b98704c40ec851d9d 100755 --- a/services/hilogcat/BUILD.gn +++ b/services/hilogcat/BUILD.gn @@ -24,6 +24,7 @@ static_library("hilogcat_static") { deps = [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//third_party/bounds_checking_function:libsec_shared", + "//base/hiviewdfx/hilog_lite/command:hilog_command_static" ] } diff --git a/services/hilogcat/hiview_logcat.c b/services/hilogcat/hiview_logcat.c index 3130b2b9840360371b81f6178330dca877ec0903..c30383c916640ec520658f1c422ede8221d3216a 100755 --- a/services/hilogcat/hiview_logcat.c +++ b/services/hilogcat/hiview_logcat.c @@ -20,266 +20,23 @@ #include #include #include + +#include "hilog_command.h" #include "log.h" #include "securec.h" - #define HILOG_DRIVER "/dev/hilog" #define HILOG_LOGBUFFER 1024 -#undef LOG_DOMAIN -#undef LOG_TAG -#define LOG_DOMAIN 0xD002D00 +#undef LOG_TAG #define LOG_TAG "hilogcat" -#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 OP_ASSIGN '=' -#define STR_MAX_LEN 128 - -typedef struct { - const unsigned char outputOption : 4; /* Control log output mode. Cannot be modified during running. */ - unsigned char hiviewInited : 1; /* Indicates whether the hiview service is inited. */ - unsigned char level : 3; /* Control log output level. HILOG_LV_XXX */ - 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 short writeFailureCount; -} HiviewConfig; - -typedef enum { - OUTPUT_OPTION_DEBUG = 0, /* Output to the UART without buffer. Commercial versions are forbidden. */ - OUTPUT_OPTION_FLOW, /* Output to UART via SAMR */ - OUTPUT_OPTION_TEXT_FILE, - OUTPUT_OPTION_BIN_FILE, - OUTPUT_OPTION_MAX -} HiviewOutputOption; - -#define HIVIEW_FEATURE_ON 1 -#define HIVIEW_FEATURE_OFF 0 -#define HILOG_MODULE_ALL 0xff - -static char g_logLevelInfo[HILOG_LEVEL_MAX] = { - 'N', // "NONE" - 'N', // "NONE" - 'N', // "NONE" - 'D', // "DEBUG" - 'I', // "INFO" - 'W', // "WARN" - 'E', // "ERROR" - 'F' // "FATAL" -}; - -#define HILOG_MODULE_MAX_NUM 50 -#define DOMIAN_ID_LENTH 6 -static 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, -}; - -static void HilogHelpProc(void) -{ - printf("hilog [-h] [-l level/mod] [-C level <1>] [-C mod <3>]\n"); - 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(" -f Enable the logs to a specified file\n"); -} - -static void ListLevelInfo(void) -{ - printf("======Level Information======\n"); - printf(" 3 - DEBUG\n"); - printf(" 4 - INFO\n"); - printf(" 5 - WARN\n"); - printf(" 6 - ERROR\n"); - printf(" 7 - FATAL\n"); -} - -static 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); - } - } -} - -static void HilogListProc(const char *cmd) -{ - if (cmd == NULL) { - ListLevelInfo(); - ListModuleInfo(); - } else if (strncmp(cmd, PARA_LEVEL, PARA_LEVEL_LEN) == 0) { - ListLevelInfo(); - } else if (strncmp(cmd, PARA_MODULE, PARA_MODULE_LEN) == 0) { - ListModuleInfo(); - } -} - -static bool SetLogLevel(unsigned char level) -{ - if (level >= LOG_DEBUG && level < HILOG_LEVEL_MAX) { - g_hiviewConfig.level = level; - return true; - } - return false; -} - -static void SetLogOutputModule(unsigned char mod) -{ - g_hiviewConfig.logOutputModule = mod; -} - -static void SetOutputModule(const char *cmd) -{ - char *endPtr = NULL; - int mod = strtol(cmd, &endPtr, 0); - SetLogOutputModule((unsigned char)mod); -} - -static void SetOutputLevel(const char *cmd) -{ - char *endPtr = NULL; - int level; - if (cmd != NULL) { - level = strtol(cmd, &endPtr, 0); - } else { - level = LOG_DEBUG; - } - if (SetLogLevel((unsigned char)level) == true) { - printf("Set the log output level success.\n"); - return; - } - printf("Set the log output level failure level=%d.\n", level); -} - - -static void HilogSetProc(const char *option, const char *attr) -{ - 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); - } -} - -static int HilogCmdProc(int argc, const char **argv) -{ -#define ARG2 1 -#define ARG3 2 -#define ARG4 3 - - int i = 0; - - if (argv[ARG2][i++] == OPTION_TAG) { - switch (argv[ARG2][i++]) { - case OPTION_HELP: - HilogHelpProc(); - return 0; - case OPTION_LIST: - HilogListProc(&argv[ARG3][0]); - return 0; - case OPTION_SET: - HilogSetProc(&argv[ARG3][0], &argv[ARG4][0]); - return 1; - case OPTION_2_FILE: - return 0; - default: - printf("Invalid command.\n"); - return 0; - } - } - printf("Invalid command.\n"); - return 0; -} - -static bool FilterLevelLog(unsigned char setLevel, unsigned char logLevel) -{ - int logMinLevel; - - for (logMinLevel = LOG_DEBUG; logMinLevel < HILOG_LEVEL_MAX; logMinLevel++) { - if (logLevel == g_logLevelInfo[logMinLevel]) { - break; - } - } - // Loglevel >= set level, may print log - if (logMinLevel >= setLevel) { - return true; - } - return false; -} - -static bool FilterModuleLog(unsigned char setModule, const char *logModule) -{ - if (setModule == HILOG_MODULE_MAX_NUM) { - return true; - } - int ret = strncmp(logModule, g_logModuleInfo[setModule], DOMAIN_LENGTH); - // If module = setmodule, may print log - if (ret == 0) { - return true; - } - return false; -} - int main(int argc, const char **argv) { int ret; bool printFlag = true; if (argc > 1) { - ret = HilogCmdProc(argc, argv); + ret = HilogCmdProc(LOG_TAG, argc, argv); if (ret == 0) { return 0; }