From c98e5eaa7aecb4ff9f3a8df31b027564396a316f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 15 Dec 2021 01:41:00 +0000 Subject: [PATCH 1/2] DEBUG default closed Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/hilog_printf.cpp | 7 +++++++ frameworks/native/include/hilog_common.h | 1 + frameworks/native/include/hilogtool_msg.h | 2 ++ services/hilogtool/include/hilogtool.h | 1 + services/hilogtool/log_controller.cpp | 13 +++++++++++++ services/hilogtool/main.cpp | 18 ++++++++++++++++-- 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 0bed870..5ba125c 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -285,9 +285,16 @@ int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *ta bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level) { +#ifdef DEBUG if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == nullptr) { return false; } +#else + if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == nullptr + || level == LOG_DEBUG) { + return false; + } +#endif if (level < GetFinalLevel(domain, tag)) { return false; } diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index c556374..8991d42 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -133,6 +133,7 @@ typedef enum { ERR_COMMAND_INVALID = -31, ERR_LOG_PERSIST_TASK_FAIL = -32, ERR_KMSG_SWITCH_VALUE_INVALID = -33, + ERR_DEBUG_SWITCH_VALUE_INVALID = -34, } ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 1123a5f..71c723d 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -58,6 +58,7 @@ typedef enum { OT_KMSG_SWITCH, OT_LOG_LEVEL, OT_FLOW_SWITCH, + OT_SINGLE_DEBUG_SWITCH, } OperateType; @@ -317,6 +318,7 @@ typedef struct { std::string domainStr; std::string tagStr; std::string pidStr; + std::string debugStr; } SetPropertyParam; #endif /* HILOGTOOL_MSG_H */ diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index ef74cc9..bf7fdba 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -62,6 +62,7 @@ typedef struct { std::string flowQuotaArgs; std::string pidArgs; std::string algorithmArgs; + std::string debugArgs; } HilogArgs; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index cb27e04..ecc6130 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -538,6 +538,19 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType return RET_FAIL; } break; + case OT_SINGLE_DEBUG_SWITCH: + key = GetPropertyName(PROP_SINGLE_DEBUG); + if (propertyParm->kmsgSwitchStr == "on") { + PropertySet(key.c_str(), "true"); + std::cout << "hilog will print log of Debug level" << std::endl; + } else if (propertyParm->kmsgSwitchStr == "off") { + PropertySet(key.c_str(), "false"); + std::cout << "hilog will not print log of Debug level" << std::endl; + } else { + std::cout << ParseErrorCode(ERR_DEBUG_SWITCH_VALUE_INVALID) << std::endl; + return RET_FAIL; + } + break; case OT_LOG_LEVEL: if (propertyParm->tagStr != "" && propertyParm->domainStr != "") { return RET_FAIL; diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index d35e920..a006399 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -337,6 +337,11 @@ int HilogEntry(int argc, char* argv[]) noLogOption = true; controlCount++; break; + case 'd': + context.debugArgs = optarg; + noLogOption = true; + controlCount++; + break; case 'r': context.logClearArgs = "clear"; noLogOption = true; @@ -539,8 +544,17 @@ int HilogEntry(int argc, char* argv[]) propertyParam.kmsgSwitchStr = context.kmsgArgs; ret = SetPropertiesOp(controller, OT_KMSG_SWITCH, &propertyParam); if (ret == RET_FAIL) { - std::cout << "set kmsg switch operation error!" << std::endl; - exit(-1); + std::cout << "set kmsg switch operation error!" << std::endl; + exit(-1); + } + exit(0); + } else if (context.debugArgs != "") { + SetPropertyParam propertyParam; + propertyParam.debugStr = context.debugArgs; + ret = SetPropertiesOp(controller, OT_SINGLE_DEBUG_SWITCH, &propertyParam); + if (ret == RET_FAIL) { + std::cout << "set debug switch operation error!" << std::endl; + exit(-1); } exit(0); } else if (context.logClearArgs != "") { -- Gitee From ed636cca5b50f6ce1dbd19922a3f7ff547a89f53 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 15 Dec 2021 01:44:57 +0000 Subject: [PATCH 2/2] help add debug Signed-off-by: aajwy <13051180828@163.com> --- README_zh.md | 6 ++++-- frameworks/native/hilog_printf.cpp | 10 ++-------- services/hilogtool/log_display.cpp | 3 ++- services/hilogtool/main.cpp | 8 ++++++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/README_zh.md b/README_zh.md index a989df9..f2bd177 100644 --- a/README_zh.md +++ b/README_zh.md @@ -117,6 +117,7 @@ release模式输出: | | | on | 打开隐私开关,显示\ | | | | off | 关闭隐私开关,显示明文 | | -k | | | Kernel日志读取开关控制 | +| -d | | | debug mode的开关控制 | | | | on | 打开读取kernel日志 | | | | off | 关闭读取kernel日志 | | -s | --statistics | | 查询统计信息,需配合-t或-D使用 | @@ -127,7 +128,7 @@ release模式输出: | | | domainon | domain流控开关打开 | | | | domainoff | domain流控开关关闭 | | -L | --level | \ | 指定级别的日志,示例:-L D/I/W/E/F | -| -t | --type | \ | 指定类型的日志,示例:-t app core init | +| -t | --type | \ | 指定类型的日志,示例:-t app core init kmsg | | -D | --domain | \ | 指定domain | | -T | --Tag | \ | 指定tag | | -a | --head | \ | 只显示前\行日志 | @@ -166,7 +167,8 @@ release模式输出: 解释:执行名字为hilog的落盘任务,10个落盘文件进行轮转,单个文件落盘大小为66k,采用zlib压缩算法。 type、level、domain、tag支持排除查询,排除查询可以使用以"^"开头的参数和分隔符","."来完成 示例:hilog -t ^core,app 排除core和app类型的日志,可以与其他参数一起使用。 -示例:hilog -t app core 打印core和app类型的日志,可以与其他参数一起使用。 +示例:hilog -t app core 打印core和app类型的日志,可以与其他参数一起使用。 +示例:hilog -t kmsg 打印kmsg类型的日志,kmsg类型不可与app/core/init类型日志混合使用。 ``` ## 涉及仓 diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 5ba125c..181bfb7 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -285,16 +285,10 @@ int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *ta bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level) { -#ifdef DEBUG - if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == nullptr) { - return false; - } -#else - if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == nullptr - || level == LOG_DEBUG) { + LogLevel min = IsDebugOn() ? LOG_LEVEL_MIN : LOG_DEBUG; + if((level <= min) || (level >= LOG_LEVEL_MAX) || tag == nullptr) { return false; } -#endif if (level < GetFinalLevel(domain, tag)) { return false; } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 28844ff..b0ae563 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -65,7 +65,8 @@ unordered_map errorMsg {ERR_BUFF_SIZE_INVALID, "Invalid buffer size, buffer size should be more than 0 and less than " + to_string(MAX_BUFFER_SIZE)}, {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed each time"}, - {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"} + {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"}, + {ERR_DEBUG_SWITCH_VALUE_INVALID, "Invalid debug switch value, valid:on/off"} }; string ParseErrorCode(ErrorCode errorCode) diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index a006399..75176bd 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -53,6 +53,10 @@ static void Helper() " store log type kmsg or not\n" " on yes\n" " off no\n" + " -d\n" + " debug mode on or off.\n" + " on yes\n" + " off no\n" " -s, --statistics query hilogd statistic information.\n" " -S clear hilogd statistic information.\n" " -r remove the logs in hilog buffer, use -t to specify log type\n" @@ -213,7 +217,7 @@ int HilogEntry(int argc, char* argv[]) {nullptr, 0, nullptr, 0} }; - int choice = getopt_long(argc, argv, "hxz:grsSa:v:e:t:L:G:f:l:n:j:w:p:k:M:D:T:b:Q:m:P:", + int choice = getopt_long(argc, argv, "hxz:grsSa:v:e:t:L:G:f:l:n:j:w:p:k:M:D:T:b:d:Q:m:P:", longOptions, &optIndex); if (choice == -1) { break; @@ -544,7 +548,7 @@ int HilogEntry(int argc, char* argv[]) propertyParam.kmsgSwitchStr = context.kmsgArgs; ret = SetPropertiesOp(controller, OT_KMSG_SWITCH, &propertyParam); if (ret == RET_FAIL) { - std::cout << "set kmsg switch operation error!" << std::endl; + std::cout << "set switch operation error!" << std::endl; exit(-1); } exit(0); -- Gitee