From 2fc7281f0775ecc9d55eadf9792df49e126800df Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Tue, 24 Aug 2021 10:03:04 +0000 Subject: [PATCH 1/4] cmd optimization Signed-off-by: youyouyuai --- frameworks/native/include/hilog_common.h | 2 +- services/hilogtool/log_controller.cpp | 6 +++++- services/hilogtool/main.cpp | 27 ++++++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index e87596b..e920f5a 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -99,7 +99,6 @@ typedef enum { ERR_QUERY_TAG_INVALID = -6, ERR_QUERY_PID_INVALID = -7, ERR_QUERY_TYPE_INVALID = -8, - ERR_BUFF_SIZE_INVALID = -8, ERR_BUFF_SIZE_EXP = -9, ERR_LOG_CONTENT_NULL = -10, ERR_LOG_PERSIST_FILE_SIZE_INVALID = -11, @@ -121,5 +120,6 @@ typedef enum { ERR_FLOWCONTROL_CONF_OPEN_FAIL = -27, ERR_LOG_PERSIST_JOBID_INVALID = -28, ERR_FLOWCTRL_SWITCH_VALUE_INVALID = -29, + ERR_BUFF_SIZE_INVALID = -30, } ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 1da81ed..cae486b 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -22,7 +22,7 @@ #include #include #include - +#include #include "hilog/log.h" #include "hilog_common.h" #include "hilogtool_msg.h" @@ -92,6 +92,10 @@ uint64_t GetBuffSize(const string& buffSizeStr) { uint64_t index = buffSizeStr.size() - 1; uint64_t buffSize; + std::regex reg_buff_size("[0-9]+[bBkKmMgGtT]?"); + if (!std::regex_match(buffSizeStr, reg_buff_size)) { + return 0; + } if (buffSizeStr[index] == 'b' || buffSizeStr[index] == 'B') { buffSize = stol(buffSizeStr.substr(0, index)); } else if (buffSizeStr[index] == 'k' || buffSizeStr[index] == 'K') { diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 1bf7d2c..780d356 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -175,6 +175,7 @@ int HilogEntry(int argc, char* argv[]) context.noBlockMode = 0; int32_t ret = 0; HilogShowFormat showFormat = OFF_SHOWFORMAT; + int controlCount = 0; for (int argsCount = 0; argsCount < argc; argsCount++) { args.push_back(argv[argsCount]); } @@ -299,14 +300,17 @@ int HilogEntry(int argc, char* argv[]) case 'g': context.buffSizeArgs = "query"; noLogOption = true; + controlCount++; break; case 'G': context.buffSizeArgs = optarg; noLogOption = true; + controlCount++; break; case 'w': context.logFileCtrlArgs = optarg; noLogOption = true; + controlCount++; break; case 'l': context.fileSizeArgs = optarg; @@ -323,10 +327,12 @@ int HilogEntry(int argc, char* argv[]) case 'p': context.personalArgs = optarg; noLogOption = true; + controlCount++; break; case 'r': context.logClearArgs = "clear"; noLogOption = true; + controlCount++; break; case 'D': indexDomain = optind - 1; @@ -370,10 +376,12 @@ int HilogEntry(int argc, char* argv[]) case 's': context.statisticArgs = "query"; noLogOption = true; + controlCount++; break; case 'S': context.statisticArgs = "clear"; noLogOption = true; + controlCount++; break; case 'T': indexTag = optind - 1; @@ -411,10 +419,12 @@ int HilogEntry(int argc, char* argv[]) case 'b': context.logLevelArgs = optarg; noLogOption = true; + controlCount++; break; case 'Q': context.flowSwitchArgs = optarg; noLogOption = true; + controlCount++; break; case 'P': indexPid = optind - 1; @@ -472,6 +482,9 @@ int HilogEntry(int argc, char* argv[]) context.levels = DEFAULT_LOG_LEVEL; } if (noLogOption) { + if (controlCount != 1) { + exit(-1); + } if (context.buffSizeArgs != "") { if (context.buffSizeArgs == "query") { ret = BufferSizeOp(controller, MC_REQ_BUFFER_SIZE, context.logTypeArgs, ""); @@ -492,13 +505,13 @@ int HilogEntry(int argc, char* argv[]) logPersistParam.jobIdStr = context.jobIdArgs; if (context.logFileCtrlArgs == "start") { ret = LogPersistOp(controller, MC_REQ_LOG_PERSIST_START, &logPersistParam); - } - if (context.logFileCtrlArgs == "stop") { + } else if (context.logFileCtrlArgs == "stop") { ret = LogPersistOp(controller, MC_REQ_LOG_PERSIST_STOP, &logPersistParam); - } - if (context.logFileCtrlArgs == "query") { + } else if (context.logFileCtrlArgs == "query") { ret = LogPersistOp(controller, MC_REQ_LOG_PERSIST_QUERY, &logPersistParam); - } + } else { + exit(-1); + } if (ret == RET_FAIL) { cout << "log file task operation error!" << endl; exit(-1); @@ -551,7 +564,9 @@ int HilogEntry(int argc, char* argv[]) exit(-1); } exit(0); - } + } else { + exit(-1); + } } else { LogQueryRequestOp(controller, &context); context.nDomain = 0; -- Gitee From ba0e320cbb3cea0b8ee73198fc61953eca02507a Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Wed, 25 Aug 2021 02:14:25 +0000 Subject: [PATCH 2/4] codeing style Signed-off-by: youyouyuai --- services/hilogtool/log_controller.cpp | 4 ++-- services/hilogtool/main.cpp | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index cae486b..07a9c29 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -92,8 +92,8 @@ uint64_t GetBuffSize(const string& buffSizeStr) { uint64_t index = buffSizeStr.size() - 1; uint64_t buffSize; - std::regex reg_buff_size("[0-9]+[bBkKmMgGtT]?"); - if (!std::regex_match(buffSizeStr, reg_buff_size)) { + std::regex reg("[0-9]+[bBkKmMgGtT]?"); + if (!std::regex_match(buffSizeStr, reg)) { return 0; } if (buffSizeStr[index] == 'b' || buffSizeStr[index] == 'B') { diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 780d356..ec13ac0 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -300,17 +300,17 @@ int HilogEntry(int argc, char* argv[]) case 'g': context.buffSizeArgs = "query"; noLogOption = true; - controlCount++; + controlCount++; break; case 'G': context.buffSizeArgs = optarg; noLogOption = true; - controlCount++; + controlCount++; break; case 'w': context.logFileCtrlArgs = optarg; noLogOption = true; - controlCount++; + controlCount++; break; case 'l': context.fileSizeArgs = optarg; @@ -327,12 +327,12 @@ int HilogEntry(int argc, char* argv[]) case 'p': context.personalArgs = optarg; noLogOption = true; - controlCount++; + controlCount++; break; case 'r': context.logClearArgs = "clear"; noLogOption = true; - controlCount++; + controlCount++; break; case 'D': indexDomain = optind - 1; @@ -376,12 +376,12 @@ int HilogEntry(int argc, char* argv[]) case 's': context.statisticArgs = "query"; noLogOption = true; - controlCount++; + controlCount++; break; case 'S': context.statisticArgs = "clear"; noLogOption = true; - controlCount++; + controlCount++; break; case 'T': indexTag = optind - 1; @@ -419,12 +419,12 @@ int HilogEntry(int argc, char* argv[]) case 'b': context.logLevelArgs = optarg; noLogOption = true; - controlCount++; + controlCount++; break; case 'Q': context.flowSwitchArgs = optarg; noLogOption = true; - controlCount++; + controlCount++; break; case 'P': indexPid = optind - 1; @@ -482,7 +482,7 @@ int HilogEntry(int argc, char* argv[]) context.levels = DEFAULT_LOG_LEVEL; } if (noLogOption) { - if (controlCount != 1) { + if (controlCount != 1) { exit(-1); } if (context.buffSizeArgs != "") { @@ -511,7 +511,7 @@ int HilogEntry(int argc, char* argv[]) ret = LogPersistOp(controller, MC_REQ_LOG_PERSIST_QUERY, &logPersistParam); } else { exit(-1); - } + } if (ret == RET_FAIL) { cout << "log file task operation error!" << endl; exit(-1); @@ -566,7 +566,7 @@ int HilogEntry(int argc, char* argv[]) exit(0); } else { exit(-1); - } + } } else { LogQueryRequestOp(controller, &context); context.nDomain = 0; -- Gitee From 172f7f4e1aadaa38663d822ba53ab214d1a87f47 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Fri, 27 Aug 2021 02:08:26 +0000 Subject: [PATCH 3/4] func param reference Signed-off-by: youyouyuai --- services/hilogtool/include/log_controller.h | 6 +++--- services/hilogtool/log_controller.cpp | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/services/hilogtool/include/log_controller.h b/services/hilogtool/include/log_controller.h index e097aae..97701ac 100644 --- a/services/hilogtool/include/log_controller.h +++ b/services/hilogtool/include/log_controller.h @@ -35,10 +35,10 @@ void LogQueryRequestOp(SeqPacketSocketClient& controller, const HilogArgs* conte void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uint32_t bufLen, HilogArgs* context, HilogShowFormat format); int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, - std::string logTypeStr, std::string buffSizeStr); + const std::string& logTypeStr, const std::string& buffSizeStr); int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, - std::string logTypeStr, std::string domainStr); -int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::string logTypeStr); + const std::string& logTypeStr, const std::string& domainStr); +int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr); int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersistParam* logPersistParam); int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType, SetPropertyParam* propertyParm); } // namespace HiviewDFX diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 07a9c29..1fc6c3b 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -258,7 +258,8 @@ void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uin } } } -int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::string logTypeStr, std::string buffSizeStr) +int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr, + const std::string& buffSizeStr) { char msgToSend[MSG_MAX_LEN] = {0}; vector vecLogType; @@ -314,7 +315,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::str } int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, - std::string logTypeStr, std::string domainStr) + const std::string& logTypeStr, const std::string& domainStr) { if ((logTypeStr != "" && domainStr != "") || (logTypeStr == "" && domainStr == "")) { return RET_FAIL; @@ -357,7 +358,7 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, return RET_SUCCESS; } -int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::string logTypeStr) +int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, const std::string& logTypeStr) { char msgToSend[MSG_MAX_LEN] = {0}; vector vecLogType; -- Gitee From 729018ea018fcbf020217acddbfb75f0d4d92909 Mon Sep 17 00:00:00 2001 From: youyouyuai Date: Mon, 30 Aug 2021 08:58:13 +0000 Subject: [PATCH 4/4] add error msg Signed-off-by: youyouyuai --- frameworks/native/include/hilog_common.h | 1 + services/hilogtool/log_controller.cpp | 3 ++- services/hilogtool/log_display.cpp | 3 ++- services/hilogtool/main.cpp | 1 + 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index e920f5a..6fceb85 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -121,5 +121,6 @@ typedef enum { ERR_LOG_PERSIST_JOBID_INVALID = -28, ERR_FLOWCTRL_SWITCH_VALUE_INVALID = -29, ERR_BUFF_SIZE_INVALID = -30, + ERR_COMMAND_INVALID = -31, } ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 1fc6c3b..7cd93d3 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -94,7 +94,8 @@ uint64_t GetBuffSize(const string& buffSizeStr) uint64_t buffSize; std::regex reg("[0-9]+[bBkKmMgGtT]?"); if (!std::regex_match(buffSizeStr, reg)) { - return 0; + std::cout << ParseErrorCode(ERR_BUFF_SIZE_INVALID) << std::endl; + exit(-1); } if (buffSizeStr[index] == 'b' || buffSizeStr[index] == 'B') { buffSize = stol(buffSizeStr.substr(0, index)); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index d48b96a..6d08019 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -66,7 +66,8 @@ unordered_map errorMsg {ERR_LOG_PERSIST_JOBID_INVALID, "Invalid jobid, jobid should be more than 0"}, {ERR_LOG_CONTENT_NULL, "Log content NULL"}, {ERR_COMMAND_NOT_FOUND, "Command not found"}, - {ERR_FORMAT_INVALID, "Invalid format parameter"} + {ERR_FORMAT_INVALID, "Invalid format parameter"}, + {ERR_COMMAND_INVALID, "Invalid command, only one control command can be executed eatch time"} }; string ParseErrorCode(ErrorCode errorCode) diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index ec13ac0..90dbc17 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -483,6 +483,7 @@ int HilogEntry(int argc, char* argv[]) } if (noLogOption) { if (controlCount != 1) { + std::cout << ParseErrorCode(ERR_COMMAND_INVALID) << std::endl; exit(-1); } if (context.buffSizeArgs != "") { -- Gitee