From d2e3c670618d7f8f77946c48ac33d8f1e196e924 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 11 Aug 2021 06:11:49 +0000 Subject: [PATCH 1/3] error code start Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilog_common.h | 30 ++++++ frameworks/native/include/hilogtool_msg.h | 8 ++ services/hilogd/log_buffer.cpp | 18 ++-- services/hilogd/log_persister.cpp | 14 +-- services/hilogd/log_querier.cpp | 13 +++ services/hilogtool/include/log_controller.h | 1 - services/hilogtool/include/log_display.h | 2 +- services/hilogtool/log_controller.cpp | 15 +++ services/hilogtool/log_display.cpp | 103 ++++++++++++++++++-- services/hilogtool/main.cpp | 20 ++-- 10 files changed, 188 insertions(+), 36 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index f1e8269..5a0c948 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -83,4 +83,34 @@ using HilogShowFormatBuffer = struct { #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) + +/*-********************************************* + * Error codes list + *-********************************************* + * Error codes _values_ are pinned down. + **********************************************/ +typedef enum { + ERR_LOG_LEVEL_INVALID = -1, + ERR_LOG_TYPE_INVALID = -2, + ERR_QUERY_LEVEL_INVALID = -3, + ERR_QUERY_TAG_INVALID = -4, + ERR_QUERY_PID_INVALID = -5, + ERR_QUERY_TYPE_INVALID = -6, + ERR_BUFF_SIZE_INVALID = -7, + ERR_BUFF_SIZE_EXP = -8, + ERR_LOG_PERSIST_FILE_SIZE_INVALID = -9, + ERR_LOG_PERSIST_FILE_NAME_INVALID = -10, + ERR_LOG_PERSIST_FILE_PATH_EXP = -11, + ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -12, + ERR_LOG_PERSIST_FILE_OPEN_FAIL = -13, + ERR_LOG_PERSIST_MMAP_FAIL = -14, + ERR_LOG_PERSIST_JOBID_FAIL = -15, + ERR_DOMAIN_INVALID = -16, + ERR_MEM_ALLOC_FAIL = -17, + ERR_MSG_LEN_INVALID = -18, + ERR_PROPERTY_VALUE_INVALID = -19, + ERR_LOG_CONTENT_NULL = -20, + ERR_COMMAND_NOT_FOUND = -21, + ERR_FORMAT_INVALID = -22 +} ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index bf3b9f9..ba14f5f 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -162,6 +162,7 @@ typedef struct { uint16_t logType; uint64_t buffSize; int32_t result; + int32_t reason; } BuffSizeResult; typedef struct { @@ -183,6 +184,7 @@ typedef struct { uint16_t logType; uint64_t buffSize; int32_t result; + int32_t reason; } BuffResizeResult; typedef struct { @@ -204,6 +206,7 @@ typedef struct { uint64_t printLen; uint64_t cacheLen; int32_t dropped; + int32_t reason; } StatisticInfoQueryResponse; typedef struct { @@ -217,6 +220,7 @@ typedef struct { int32_t result; uint16_t logType; uint32_t domain; + int32_t reason; } StatisticInfoClearResponse; typedef struct { @@ -231,6 +235,7 @@ typedef struct { typedef struct { uint16_t logType; int32_t result; + int32_t reason; } LogClearResult; typedef struct { @@ -262,6 +267,7 @@ typedef struct { typedef struct { int32_t result; uint32_t jobId; + int32_t reason; } LogPersistStartResult; typedef struct { @@ -280,6 +286,7 @@ typedef struct { typedef struct { int32_t result; uint32_t jobId; + int32_t reason; } LogPersistStopResult; typedef struct { MessageHeader msgHeader; @@ -302,6 +309,7 @@ typedef struct { char filePath[FILE_PATH_MAX_LEN]; uint32_t fileSize; uint32_t fileNum; + int32_t reason; } LogPersistQueryResult; typedef struct { MessageHeader msgHeader; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index e3a3e28..88f1b69 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -181,6 +181,9 @@ bool HilogBuffer::Query(std::shared_ptr reader) size_t HilogBuffer::Delete(uint16_t logType) { + if (logType >= LOG_TYPE_MAX) { + return ERR_LOG_TYPE_INVALID; + } size_t sum = 0; hilogBufferMutex.lock(); std::list::iterator it = hilogDataList.begin(); @@ -233,7 +236,7 @@ bool HilogBuffer::Query(LogReader* reader) size_t HilogBuffer::GetBuffLen(uint16_t logType) { if (logType >= LOG_TYPE_MAX) { - return -1; + return ERR_LOG_TYPE_INVALID; } uint64_t buffSize = g_maxBufferSizeByType[logType]; return buffSize; @@ -241,8 +244,11 @@ size_t HilogBuffer::GetBuffLen(uint16_t logType) size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) { - if (logType >= LOG_TYPE_MAX || buffSize <= 0 || buffSize > ONE_GB) { - return -1; + if (logType >= LOG_TYPE_MAX) { + return ERR_LOG_TYPE_INVALID; + } + if (buffSize <= 0 || buffSize > ONE_GB) { + return ERR_BUFF_SIZE_INVALID; } hilogBufferMutex.lock(); if (sizeByType[logType] > buffSize) { @@ -270,7 +276,7 @@ size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) } // Re-confirm if enough elements has been removed if (sizeByType[logType] > (size_t)g_maxBufferSizeByType[logType] || size > (size_t)g_maxBufferSize) { - return -1; + return ERR_BUFF_SIZE_EXP; } g_maxBufferSizeByType[logType] = buffSize; g_maxBufferSize += (buffSize - sizeByType[logType]); @@ -285,7 +291,7 @@ size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) int32_t HilogBuffer::GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped) { if (logType >= LOG_TYPE_MAX) { - return -1; + return ERR_LOG_TYPE_INVALID; } printLen = printLenByType[logType]; cacheLen = cacheLenByType[logType]; @@ -305,7 +311,7 @@ int32_t HilogBuffer::GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLe int32_t HilogBuffer::ClearStatisticInfoByLog(uint16_t logType) { if (logType >= LOG_TYPE_MAX) { - return -1; + return ERR_LOG_TYPE_INVALID; } ClearDroppedByType(); printLenByType[logType] = 0; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index d656e06..87809c1 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -96,7 +96,7 @@ int LogPersister::Init() { int nPos = path.find_last_of('/'); if (nPos == RET_FAIL) { - return RET_FAIL; + return ERR_LOG_PERSIST_FILE_PATH_EXP; } mmapPath = path.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { @@ -113,10 +113,10 @@ int LogPersister::Init() break; } if (hit) { - return RET_FAIL; + return ERR_LOG_PERSIST_FILE_PATH_EXP; } if (InitCompress() == RET_FAIL) { - return RET_FAIL; + return ERR_LOG_PERSIST_COMPRESS_INIT_FAIL; } fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); if (fd <= 0) { @@ -135,7 +135,7 @@ int LogPersister::Init() #ifdef DEBUG cout << "open log file(" << mmapPath << ") failed: " << strerror(errno) << endl; #endif - return RET_FAIL; + return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } fdinfo = fopen((mmapPath + ".info").c_str(), "a+"); if (fdinfo == nullptr) { @@ -143,7 +143,7 @@ int LogPersister::Init() cout << "open loginfo file failed: " << strerror(errno) << endl; #endif close(fd); - return RET_FAIL; + return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } buffer = (LogPersisterBuffer *)mmap(nullptr, sizeof(LogPersisterBuffer), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); @@ -153,7 +153,7 @@ int LogPersister::Init() cout << "mmap file failed: " << strerror(errno) << endl; #endif fclose(fdinfo); - return RET_FAIL; + return ERR_LOG_PERSIST_MMAP_FAIL; } if (restore == true) { #ifdef DEBUG @@ -374,7 +374,7 @@ int LogPersister::Kill(const uint32_t id) ++it; } } - return found ? 0 : -1; + return found ? 0 : ERR_LOG_PERSIST_JOBID_FAIL; } bool LogPersister::isExited() diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 94c4041..94ec02c 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -115,6 +115,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade std::cout << "Persist log file size less than min size" << std::endl; pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = RET_FAIL; + pLogPersistStartRst->reason = ERR_LOG_PERSIST_FILE_SIZE_INVALID; SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; @@ -127,6 +128,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade cout << "FileName is not valid!" << endl; pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = RET_FAIL; + pLogPersistStartRst->reason = ERR_LOG_PERSIST_FILE_NAME_INVALID; SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; @@ -147,6 +149,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartRst->result = persister->Init(); int rotatorRes = rotator->Init(); if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { + pLogPersistStartRst->reason = rotatorRes; cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { @@ -186,6 +189,7 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead if (pLogPersistStopRst) { pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistStopRst->reason = rst; pLogPersistStopRst++; msgNum++; } @@ -195,6 +199,7 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead if (pLogPersistStopRst) { pLogPersistStopRst->jobId = (*it).jobId; pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistStopRst->reason = rst; pLogPersistStopRst++; msgNum++; } @@ -234,6 +239,7 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade for (it = resultList.begin(); it != resultList.end(); ++it) { if (pLogPersistQueryRst) { pLogPersistQueryRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogPersistQueryRst->reason = rst; pLogPersistQueryRst->jobId = (*it).jobId; pLogPersistQueryRst->logType = (*it).logType; pLogPersistQueryRst->compressAlg = (*it).compressAlg; @@ -281,6 +287,7 @@ void HandleBufferResizeRequest(char* reqMsg, std::shared_ptr logReade pBuffResizeRst->logType = pBuffResizeMsg->logType; pBuffResizeRst->buffSize = pBuffResizeMsg->buffSize; pBuffResizeRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pBuffResizeRst->reason = rst; pBuffResizeRst++; } pBuffResizeMsg++; @@ -316,6 +323,7 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, pBuffSizeRst->logType = pBuffSizeMsg->logType; pBuffSizeRst->buffSize = buffLen; pBuffSizeRst->result = (buffLen < 0) ? RET_FAIL : RET_SUCCESS; + pBuffSizeRst->reason = buffLen; pBuffSizeRst++; } recvMsgLen += sizeof(BuffSizeMsg); @@ -341,12 +349,14 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, rst = buffer->GetStatisticInfoByLog(pStatisticInfoQueryReq->logType, pStatisticInfoQueryRsp->printLen, pStatisticInfoQueryRsp->cacheLen, pStatisticInfoQueryRsp->dropped); pStatisticInfoQueryRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pStatisticInfoQueryRsp->reason = rst; } else { pStatisticInfoQueryRsp->logType = pStatisticInfoQueryReq->logType; pStatisticInfoQueryRsp->domain = pStatisticInfoQueryReq->domain; rst = buffer->GetStatisticInfoByDomain(pStatisticInfoQueryReq->domain, pStatisticInfoQueryRsp->printLen, pStatisticInfoQueryRsp->cacheLen, pStatisticInfoQueryRsp->dropped); pStatisticInfoQueryRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pStatisticInfoQueryRsp->reason = rst; } SetMsgHead(&pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sizeof(StatisticInfoQueryResponse) - sizeof(MessageHeader)); @@ -365,11 +375,13 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; rst = buffer->ClearStatisticInfoByLog(pStatisticInfoClearReq->logType); pStatisticInfoClearRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pStatisticInfoClearRsp->reason = rst; } else { pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; rst = buffer->ClearStatisticInfoByDomain(pStatisticInfoClearReq->domain); pStatisticInfoClearRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pStatisticInfoClearRsp->reason = rst; } SetMsgHead(&pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - sizeof(MessageHeader)); @@ -397,6 +409,7 @@ void HandleBufferClearRequest(char* reqMsg, std::shared_ptr logReader if (pLogClearRst) { pLogClearRst->logType = pLogClearMsg->logType; pLogClearRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; + pLogClearRst->reason = rst; pLogClearRst++; } pLogClearMsg++; diff --git a/services/hilogtool/include/log_controller.h b/services/hilogtool/include/log_controller.h index 2030eac..e097aae 100644 --- a/services/hilogtool/include/log_controller.h +++ b/services/hilogtool/include/log_controller.h @@ -27,7 +27,6 @@ namespace OHOS { namespace HiviewDFX { constexpr int RECV_BUF_LEN = MAX_LOG_LEN * 2; - void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen); int MultiQuerySplit(const std::string& src, const char& delim, std::vector& vec); inline void PrintBuffer(void* pBuff, unsigned int nLen); diff --git a/services/hilogtool/include/log_display.h b/services/hilogtool/include/log_display.h index 4d1ffe3..2f0a402 100644 --- a/services/hilogtool/include/log_display.h +++ b/services/hilogtool/include/log_display.h @@ -24,7 +24,7 @@ namespace OHOS { namespace HiviewDFX { using namespace std; int32_t ControlCmdResult(const char* message); - +std::string ParseErrorCode(ErrorCode errorCode); void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* contentOut, HilogArgs* context, vector& tailBuffer); HilogShowFormat HilogFormat (const char* formatArg); diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 6bcd3bc..88f40c9 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -38,6 +38,7 @@ const int MSG_MAX_LEN = 2048; const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB; const int LOG_PERSIST_FILE_NUM = 10; const uint32_t DEFAULT_JOBID = 1; + void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { if (!msgHeader) { @@ -273,6 +274,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::str for (iter = 0; iter < logTypeNum; iter++) { pBuffSizeMsg->logType = GetLogType(vecLogType[iter]); if (pBuffSizeMsg->logType == 0xffff) { + ParseErrorCode(ERR_LOG_TYPE_INVALID); return RET_FAIL; } pBuffSizeMsg++; @@ -291,6 +293,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::str for (iter = 0; iter < logTypeNum; iter++) { pBuffResizeMsg->logType = GetLogType(vecLogType[iter]); if (pBuffResizeMsg->logType == 0xffff) { + ParseErrorCode(ERR_LOG_TYPE_INVALID); return RET_FAIL; } pBuffResizeMsg->buffSize = GetBuffSize(buffSizeStr); @@ -318,12 +321,14 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, if (domainStr == "") { domain = 0xffffffff; if (logType == 0xffff) { + ParseErrorCode(ERR_LOG_TYPE_INVALID); return RET_FAIL; } } else { std::istringstream(domainStr) >> domain; if (domain == 0) { std::cout << "Invalid parameter" << std::endl; + ParseErrorCode(ERR_DOMAIN_INVALID); return RET_FAIL; } } @@ -362,14 +367,17 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::strin LogClearRequest* pLogClearReq = reinterpret_cast(msgToSend); LogClearMsg* pLogClearMsg = reinterpret_cast(&pLogClearReq->logClearMsg); if (!pLogClearMsg) { + ParseErrorCode(ERR_MEM_ALLOC_FAIL); return RET_FAIL; } if (logTypeNum * sizeof(LogClearMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { + ParseErrorCode(ERR_MSG_LEN_INVALID); return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { pLogClearMsg->logType = GetLogType(vecLogType[iter]); if (pLogClearMsg->logType == 0xffff) { + ParseErrorCode(ERR_LOG_TYPE_INVALID); return RET_FAIL; } pLogClearMsg++; @@ -401,11 +409,13 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi LogPersistStartMsg* pLogPersistStartMsg = reinterpret_cast(&pLogPersistStartReq->logPersistStartMsg); if (sizeof(LogPersistStartRequest) > MSG_MAX_LEN) { + ParseErrorCode(ERR_MSG_LEN_INVALID); return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); if (tmpType == 0xffff) { + ParseErrorCode(ERR_LOG_TYPE_INVALID); return RET_FAIL; } pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; @@ -419,6 +429,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { + ParseErrorCode(ERR_LOG_PERSIST_FILE_NAME_INVALID); return RET_FAIL; } if (logPersistParam->fileNameStr != " ") { @@ -440,6 +451,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi break; } if (jobIdNum * sizeof(LogPersistStopMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { + ParseErrorCode(ERR_MSG_LEN_INVALID); return RET_FAIL; } for (iter = 0; iter < jobIdNum; iter++) { @@ -460,6 +472,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); if (tmpType == 0xffff) { + ParseErrorCode(ERR_LOG_TYPE_INVALID); return RET_FAIL; } pLogPersistQueryMsg->logType = (0b01 << tmpType) | pLogPersistQueryMsg->logType; @@ -501,6 +514,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType PropertySet(key.c_str(), "false"); cout << "hilog private formatter is disabled" << endl; } else { + ParseErrorCode(ERR_PROPERTY_VALUE_INVALID); return RET_FAIL; } break; @@ -554,6 +568,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType PropertySet(key.c_str(), "false"); cout << "flow control by domain is disabled" << endl; } else { + ParseErrorCode(ERR_PROPERTY_VALUE_INVALID); return RET_FAIL; } break; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index e79ba70..6635b67 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -35,6 +35,85 @@ using hash_t = std::uint64_t; constexpr hash_t PRIME = 0x100000001B3ull; constexpr hash_t BASIS = 0xCBF29CE484222325ull; +string ParseErrorCode(ErrorCode errorCode) { + string errorMsg; + switch (errorCode) + { + case ERR_LOG_LEVEL_INVALID: + errorMsg = "Invalid log level"; + break; + case ERR_LOG_TYPE_INVALID: + errorMsg = "Invalid log type"; + break; + case ERR_QUERY_TYPE_INVALID: + errorMsg = "Query condition on both types and excluded types is undefined.\n"; + errorMsg += "Please remove types or excluded types condition, and try again."; + break; + case ERR_QUERY_LEVEL_INVALID: + errorMsg = "Query condition on both levels and excluded levels is undefined.\n"; + errorMsg += "Please remove levels or excluded levels condition, and try again."; + break; + case ERR_QUERY_TAG_INVALID: + errorMsg = "Query condition on both tags and excluded tags is undefined.\n"; + errorMsg += "Please remove tags or excluded tags condition, and try again."; + break; + case ERR_QUERY_PID_INVALID: + errorMsg = "Query condition on both pid and excluded pid is undefined.\n"; + errorMsg += "Please remove pid or excluded pid condition, and try again."; + break; + case ERR_BUFF_SIZE_INVALID: + errorMsg = "Invalid buffer size"; + break; + case ERR_BUFF_SIZE_EXP: + errorMsg = "buffer size exception"; + break; + case ERR_LOG_PERSIST_FILE_SIZE_INVALID: + errorMsg = "Invalid log persist file size"; + break; + case ERR_LOG_PERSIST_FILE_NAME_INVALID: + errorMsg = "Invalid log persist file name"; + break; + case ERR_LOG_PERSIST_FILE_PATH_EXP: + errorMsg = "log persist file path exception"; + break; + case ERR_LOG_PERSIST_COMPRESS_INIT_FAIL: + errorMsg = "log persist compress initial failed"; + break; + case ERR_LOG_PERSIST_FILE_OPEN_FAIL: + errorMsg = "log persist open file failed"; + break; + case ERR_LOG_PERSIST_MMAP_FAIL: + errorMsg = "log persist mmap failed"; + break; + case ERR_LOG_PERSIST_JOBID_FAIL: + errorMsg = "log persist jobid not exist"; + break; + case ERR_DOMAIN_INVALID: + errorMsg = "Invalid domain"; + break; + case ERR_MEM_ALLOC_FAIL: + errorMsg = "alloc memory failed"; + break; + case ERR_MSG_LEN_INVALID: + errorMsg = "Invalid message length"; + break; + case ERR_PROPERTY_VALUE_INVALID: + errorMsg = "Invalid property value"; + break; + case ERR_LOG_CONTENT_NULL: + errorMsg = "log content NULL"; + break; + case ERR_COMMAND_NOT_FOUND: + errorMsg = "command not found"; + break; + case ERR_FORMAT_INVALID: + errorMsg = "Invalid format parameter"; + break; + default: + break; + } + return errorMsg; +} hash_t Hash(char const *str) { hash_t ret {BASIS}; @@ -143,7 +222,8 @@ int32_t ControlCmdResult(const char* message) while (pBuffSizeRst && resultLen < msgLen) { if (pBuffSizeRst->result == RET_FAIL) { outputStr += GetLogTypeStr(pBuffSizeRst->logType); - outputStr += " buffer size fail"; + outputStr += " buffer size fail, reason:"; + outputStr += ParseErrorCode((ErrorCode)pBuffSizeRst->reason); outputStr += "\n"; } else { outputStr += GetLogTypeStr(pBuffSizeRst->logType); @@ -165,7 +245,8 @@ int32_t ControlCmdResult(const char* message) while (pBuffResizeRst && resultLen < msgLen) { if (pBuffResizeRst->result == RET_FAIL) { outputStr += GetLogTypeStr(pBuffResizeRst->logType); - outputStr += " buffer resize fail"; + outputStr += " buffer resize fail, reason:"; + outputStr += ParseErrorCode((ErrorCode)pBuffResizeRst->reason); outputStr += "\n"; } else { outputStr += GetLogTypeStr(pBuffResizeRst->logType); @@ -203,7 +284,8 @@ int32_t ControlCmdResult(const char* message) outputStr += GetByteLenStr(staInfoQueryRsp->dropped); } else if (staInfoQueryRsp->result == RET_FAIL) { outputStr += logOrDomain; - outputStr += " statistic info query fail "; + outputStr += " statistic info query fail, reason:"; + outputStr += ParseErrorCode((ErrorCode)staInfoQueryRsp->reason); } break; } @@ -223,7 +305,8 @@ int32_t ControlCmdResult(const char* message) outputStr += " statistic info clear success "; } else if (staInfoClearRsp->result == RET_FAIL) { outputStr += logOrDomain; - outputStr += " statistic info clear fail "; + outputStr += " statistic info clear fail, reason:"; + outputStr += ParseErrorCode((ErrorCode)staInfoClearRsp->reason); } break; } @@ -236,7 +319,8 @@ int32_t ControlCmdResult(const char* message) while (pLogClearRst && resultLen < msgLen) { if (pLogClearRst->result == RET_FAIL) { outputStr += GetLogTypeStr(pLogClearRst->logType); - outputStr += " log clear fail"; + outputStr += " log clear fail, reason:"; + outputStr += ParseErrorCode((ErrorCode)pLogClearRst->reason); outputStr += "\n"; } else { outputStr += GetLogTypeStr(pLogClearRst->logType); @@ -260,6 +344,7 @@ int32_t ControlCmdResult(const char* message) outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); outputStr += "] start failed\n"; + outputStr += ParseErrorCode((ErrorCode)pLogPersistStartRst->reason); } else { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); @@ -281,6 +366,7 @@ int32_t ControlCmdResult(const char* message) outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); outputStr += "] stop failed\n"; + outputStr += ParseErrorCode((ErrorCode)pLogPersistStopRst->reason); } else { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); @@ -303,6 +389,7 @@ int32_t ControlCmdResult(const char* message) outputStr = "Persist task [logtype:"; outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); outputStr += "] query failed\n"; + outputStr += ParseErrorCode((ErrorCode)pLogPersistQueryRst->reason); } else { outputStr += to_string(pLogPersistQueryRst->jobId); outputStr += " "; @@ -362,7 +449,7 @@ HilogShowFormat HilogFormat (const char* formatArg) format = MONOTONIC_SHOWFORMAT; break; default: - cout << "Format Invalid parameter"<length == 0) { -#ifdef DEBUG - cout << "Log content null" << endl; -#endif + std::cout << ParseErrorCode(ERR_LOG_CONTENT_NULL) << endl; return; } diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 1daf778..6e72312 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -128,7 +128,7 @@ static int GetTypes(HilogArgs context, const string& typesArgs, bool exclude = f } else if (typesArgs == "core") { types |= 1<msgType << endl; break; -- Gitee From b12d2c16e743a7037bd80d854846bbbafc74d6c6 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 12 Aug 2021 05:24:00 +0000 Subject: [PATCH 2/3] Hilogd: Update err codes Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilog_common.h | 26 +++++++++++++---------- services/hilogd/flow_control_init.cpp | 2 +- services/hilogd/log_persister.cpp | 6 +++--- services/hilogd/log_persister_rotator.cpp | 6 +++--- services/hilogd/log_querier.cpp | 6 +++--- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index 5a0c948..9966532 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -101,16 +101,20 @@ typedef enum { ERR_LOG_PERSIST_FILE_SIZE_INVALID = -9, ERR_LOG_PERSIST_FILE_NAME_INVALID = -10, ERR_LOG_PERSIST_FILE_PATH_EXP = -11, - ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -12, - ERR_LOG_PERSIST_FILE_OPEN_FAIL = -13, - ERR_LOG_PERSIST_MMAP_FAIL = -14, - ERR_LOG_PERSIST_JOBID_FAIL = -15, - ERR_DOMAIN_INVALID = -16, - ERR_MEM_ALLOC_FAIL = -17, - ERR_MSG_LEN_INVALID = -18, - ERR_PROPERTY_VALUE_INVALID = -19, - ERR_LOG_CONTENT_NULL = -20, - ERR_COMMAND_NOT_FOUND = -21, - ERR_FORMAT_INVALID = -22 + ERR_LOG_PERSIST_DIR_OPEN_FAIL = -12, + ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -13, + ERR_LOG_PERSIST_FILE_OPEN_FAIL = -14, + ERR_LOG_PERSIST_MMAP_FAIL = -15, + ERR_LOG_PERSIST_JOBID_FAIL = -16, + ERR_DOMAIN_INVALID = -17, + ERR_MEM_ALLOC_FAIL = -18, + ERR_MSG_LEN_INVALID = -19, + ERR_PROPERTY_VALUE_INVALID = -20, + ERR_LOG_CONTENT_NULL = -21, + ERR_COMMAND_NOT_FOUND = -22, + ERR_FORMAT_INVALID = -23, + ERR_COMPRESS_FAIL = -24, + ERR_PERSIST_INFO_OPEN_FAIL = -25, + ERR_FLOWCONTROL_CONF_OPEN_FAIL = -26 } ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index 1b77293..5f4f47e 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -145,7 +145,7 @@ int32_t InitDomainFlowCtrl() #ifdef DEBUG std::cout << "open file failed" << std::endl; #endif - return -1; + return ERR_FLOWCONTROL_CONF_OPEN_FAIL; } std::string line; while (!ifs.eof()) { diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 87809c1..aad36c8 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -74,7 +74,7 @@ int LogPersister::InitCompress() { compressBuffer = new LogPersisterBuffer; if (compressBuffer == NULL) { - return RET_FAIL; + return ERR_COMPRESS_FAIL; } switch (compressAlg) { case COMPRESS_TYPE_NONE: @@ -266,7 +266,7 @@ int LogPersister::WriteData(HilogData *data) return 0; if (compressor->Compress(buffer, compressBuffer) != 0) { cout << "COMPRESS Error" << endl; - return -1; + return ERR_COMPRESS_FAIL; }; WriteFile(); return writeUnCompressedBuffer(data) ? 0 : -1; @@ -420,7 +420,7 @@ int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) info.levels = queryCondition.levels; if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { cout << "Failed to save persister file path" << endl; - return RET_FAIL; + return ERR_LOG_PERSIST_FILE_PATH_EXP; } cout << "Saved Path=" << info.msg.filePath << endl; return RET_SUCCESS; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 61db2e2..b7ec080 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -49,7 +49,7 @@ int LogPersisterRotator::Init() } } fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); - if (fdinfo == nullptr) return RET_FAIL; + if (fdinfo == nullptr) return ERR_PERSIST_INFO_OPEN_FAIL; return RET_SUCCESS; } @@ -57,7 +57,7 @@ int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index << " " << length << " need: " << needRotate << endl; - if (length <= 0 || buf == nullptr) return -1; + if (length <= 0 || buf == nullptr) return ERR_LOG_PERSIST_FILE_PATH_EXP; if (needRotate) { output.close(); Rotate(); @@ -127,4 +127,4 @@ void LogPersisterRotator::SetId(uint32_t pId) id = pId; } } // namespace HiviewDFX -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 94ec02c..5544b5d 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -148,7 +148,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = persister->Init(); int rotatorRes = rotator->Init(); - if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { + if (pLogPersistStartRst->result != 0 || saveInfoRes != 0 || rotatorRes != 0) { pLogPersistStartRst->reason = rotatorRes; cout << "LogPersister failed to initialize!" << endl; persister.reset(); @@ -618,7 +618,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) int rotatorRes = rotator->Init(); persister->queryCondition.types = info.types; persister->queryCondition.levels = info.levels; - if (persisterRes == RET_FAIL || rotatorRes == RET_FAIL) { + if (persisterRes != 0 || rotatorRes != 0) { cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { @@ -630,7 +630,7 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) closedir (dir); } else { perror ("Failed to open persister directory!"); - return EXIT_FAILURE; + return ERR_LOG_PERSIST_DIR_OPEN_FAIL; } cout << "Finished restoring persist jobs!" << endl; return EXIT_SUCCESS; -- Gitee From 3eb338f446e029682677fcf9830c087cfa6c0c89 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 12 Aug 2021 05:36:10 +0000 Subject: [PATCH 3/3] Hilogtool: update err codes Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilog_common.h | 67 +++++---- frameworks/native/include/hilogtool_msg.h | 8 - services/hilogd/include/log_compress.h | 2 +- services/hilogd/log_buffer.cpp | 5 +- services/hilogd/log_persister.cpp | 10 +- services/hilogd/log_persister_rotator.cpp | 2 +- services/hilogd/log_querier.cpp | 43 +++--- services/hilogtool/include/hilogtool.h | 2 + services/hilogtool/log_controller.cpp | 36 ++--- services/hilogtool/log_display.cpp | 173 +++++++++------------- services/hilogtool/main.cpp | 13 +- 11 files changed, 165 insertions(+), 196 deletions(-) diff --git a/frameworks/native/include/hilog_common.h b/frameworks/native/include/hilog_common.h index 9966532..e87596b 100644 --- a/frameworks/native/include/hilog_common.h +++ b/frameworks/native/include/hilog_common.h @@ -32,7 +32,6 @@ #define SENDIDN 0 // hilogd: reached end of log; hilogtool: exit log reading #define SENDIDA 1 // hilogd & hilogtool: normal log reading #define SENDIDS 2 // hilogd: notify for new data; hilogtool: block and wait for new data -#define MULARGS 5 #define MAX_LOG_LEN 1024 /* maximum length of a log, include '\0' */ #define MAX_TAG_LEN 32 /* log tag size, include '\0' */ #define MAX_DOMAINS 5 @@ -46,7 +45,9 @@ #define ONE_GB (1UL<<30) #define ONE_TB (1ULL<<40) -#define DOMAIN_NUMBER_BASE (16) +const uint32_t MAX_BUFFER_SIZE = 1UL<<30; +const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; +const int MSG_MAX_LEN = 2048; /* * header of log message from libhilog to hilogd @@ -84,37 +85,41 @@ using HilogShowFormatBuffer = struct { #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) -/*-********************************************* +/* + * ******************************************** * Error codes list - *-********************************************* * Error codes _values_ are pinned down. - **********************************************/ + * ******************************************** +*/ typedef enum { - ERR_LOG_LEVEL_INVALID = -1, - ERR_LOG_TYPE_INVALID = -2, - ERR_QUERY_LEVEL_INVALID = -3, - ERR_QUERY_TAG_INVALID = -4, - ERR_QUERY_PID_INVALID = -5, - ERR_QUERY_TYPE_INVALID = -6, - ERR_BUFF_SIZE_INVALID = -7, - ERR_BUFF_SIZE_EXP = -8, - ERR_LOG_PERSIST_FILE_SIZE_INVALID = -9, - ERR_LOG_PERSIST_FILE_NAME_INVALID = -10, - ERR_LOG_PERSIST_FILE_PATH_EXP = -11, - ERR_LOG_PERSIST_DIR_OPEN_FAIL = -12, - ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -13, - ERR_LOG_PERSIST_FILE_OPEN_FAIL = -14, - ERR_LOG_PERSIST_MMAP_FAIL = -15, - ERR_LOG_PERSIST_JOBID_FAIL = -16, - ERR_DOMAIN_INVALID = -17, - ERR_MEM_ALLOC_FAIL = -18, - ERR_MSG_LEN_INVALID = -19, - ERR_PROPERTY_VALUE_INVALID = -20, - ERR_LOG_CONTENT_NULL = -21, - ERR_COMMAND_NOT_FOUND = -22, - ERR_FORMAT_INVALID = -23, - ERR_COMPRESS_FAIL = -24, - ERR_PERSIST_INFO_OPEN_FAIL = -25, - ERR_FLOWCONTROL_CONF_OPEN_FAIL = -26 + ERR_LOG_LEVEL_INVALID = -2, + ERR_LOG_TYPE_INVALID = -3, + ERR_QUERY_LEVEL_INVALID = -4, + ERR_QUERY_DOMAIN_INVALID = -5, + 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, + ERR_LOG_PERSIST_FILE_NAME_INVALID = -12, + ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP = -13, + ERR_LOG_PERSIST_DIR_OPEN_FAIL = -14, + ERR_LOG_PERSIST_COMPRESS_INIT_FAIL = -15, + ERR_LOG_PERSIST_FILE_OPEN_FAIL = -16, + ERR_LOG_PERSIST_MMAP_FAIL = -17, + ERR_LOG_PERSIST_JOBID_FAIL = -18, + ERR_DOMAIN_INVALID = -19, + ERR_MEM_ALLOC_FAIL = -20, + ERR_MSG_LEN_INVALID = -21, + ERR_PRIVATE_SWITCH_VALUE_INVALID = -22, + ERR_COMMAND_NOT_FOUND = -23, + ERR_FORMAT_INVALID = -24, + ERR_LOG_PERSIST_FILE_PATH_INVALID = -25, + ERR_PERSIST_INFO_OPEN_FAIL = -26, + ERR_FLOWCONTROL_CONF_OPEN_FAIL = -27, + ERR_LOG_PERSIST_JOBID_INVALID = -28, + ERR_FLOWCTRL_SWITCH_VALUE_INVALID = -29, } ErrorCode; #endif /* HILOG_COMMON_H */ diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index ba14f5f..bf3b9f9 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -162,7 +162,6 @@ typedef struct { uint16_t logType; uint64_t buffSize; int32_t result; - int32_t reason; } BuffSizeResult; typedef struct { @@ -184,7 +183,6 @@ typedef struct { uint16_t logType; uint64_t buffSize; int32_t result; - int32_t reason; } BuffResizeResult; typedef struct { @@ -206,7 +204,6 @@ typedef struct { uint64_t printLen; uint64_t cacheLen; int32_t dropped; - int32_t reason; } StatisticInfoQueryResponse; typedef struct { @@ -220,7 +217,6 @@ typedef struct { int32_t result; uint16_t logType; uint32_t domain; - int32_t reason; } StatisticInfoClearResponse; typedef struct { @@ -235,7 +231,6 @@ typedef struct { typedef struct { uint16_t logType; int32_t result; - int32_t reason; } LogClearResult; typedef struct { @@ -267,7 +262,6 @@ typedef struct { typedef struct { int32_t result; uint32_t jobId; - int32_t reason; } LogPersistStartResult; typedef struct { @@ -286,7 +280,6 @@ typedef struct { typedef struct { int32_t result; uint32_t jobId; - int32_t reason; } LogPersistStopResult; typedef struct { MessageHeader msgHeader; @@ -309,7 +302,6 @@ typedef struct { char filePath[FILE_PATH_MAX_LEN]; uint32_t fileSize; uint32_t fileNum; - int32_t reason; } LogPersistQueryResult; typedef struct { MessageHeader msgHeader; diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index eafcce6..b2ed40a 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -15,6 +15,7 @@ #ifndef HILOG_COMPRESS_H #define HILOG_COMPRESS_H +#include "hilog_common.h" #include #ifdef USING_ZSTD_COMPRESS #define ZSTD_STATIC_LINKING_ONLY @@ -24,7 +25,6 @@ #include namespace OHOS { namespace HiviewDFX { -const uint32_t MAX_PERSISTER_BUFFER_SIZE = 64 * 1024; typedef struct { uint32_t offset; char content[MAX_PERSISTER_BUFFER_SIZE]; diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 88f1b69..fd975f0 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -31,7 +31,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -#define MAX_BUFFER_SIZE 4194304 const float DROP_RATIO = 0.05; static int g_maxBufferSize = 4194304; static int g_maxBufferSizeByType[LOG_TYPE_MAX] = {1048576, 1048576, 1048576, 1048576}; @@ -245,9 +244,9 @@ size_t HilogBuffer::GetBuffLen(uint16_t logType) size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) { if (logType >= LOG_TYPE_MAX) { - return ERR_LOG_TYPE_INVALID; + return ERR_LOG_TYPE_INVALID; } - if (buffSize <= 0 || buffSize > ONE_GB) { + if (buffSize <= 0 || buffSize > MAX_BUFFER_SIZE) { return ERR_BUFF_SIZE_INVALID; } hilogBufferMutex.lock(); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index aad36c8..056e64c 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -74,7 +74,7 @@ int LogPersister::InitCompress() { compressBuffer = new LogPersisterBuffer; if (compressBuffer == NULL) { - return ERR_COMPRESS_FAIL; + return RET_FAIL; } switch (compressAlg) { case COMPRESS_TYPE_NONE: @@ -96,7 +96,7 @@ int LogPersister::Init() { int nPos = path.find_last_of('/'); if (nPos == RET_FAIL) { - return ERR_LOG_PERSIST_FILE_PATH_EXP; + return ERR_LOG_PERSIST_FILE_PATH_INVALID; } mmapPath = path.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { @@ -113,7 +113,7 @@ int LogPersister::Init() break; } if (hit) { - return ERR_LOG_PERSIST_FILE_PATH_EXP; + return ERR_LOG_PERSIST_FILE_PATH_INVALID; } if (InitCompress() == RET_FAIL) { return ERR_LOG_PERSIST_COMPRESS_INIT_FAIL; @@ -266,7 +266,7 @@ int LogPersister::WriteData(HilogData *data) return 0; if (compressor->Compress(buffer, compressBuffer) != 0) { cout << "COMPRESS Error" << endl; - return ERR_COMPRESS_FAIL; + return RET_FAIL; }; WriteFile(); return writeUnCompressedBuffer(data) ? 0 : -1; @@ -420,7 +420,7 @@ int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) info.levels = queryCondition.levels; if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { cout << "Failed to save persister file path" << endl; - return ERR_LOG_PERSIST_FILE_PATH_EXP; + return ERR_LOG_PERSIST_FILE_PATH_INVALID; } cout << "Saved Path=" << info.msg.filePath << endl; return RET_SUCCESS; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index b7ec080..48f2df7 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -57,7 +57,7 @@ int LogPersisterRotator::Input(const char *buf, uint32_t length) { cout << __func__ << " " << fileName << " " << index << " " << length << " need: " << needRotate << endl; - if (length <= 0 || buf == nullptr) return ERR_LOG_PERSIST_FILE_PATH_EXP; + if (length <= 0 || buf == nullptr) return ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP; if (needRotate) { output.close(); Rotate(); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 5544b5d..24ec6b7 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -111,11 +111,16 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } + if (pLogPersistStartMsg->jobId <= 0) { + pLogPersistStartRst->result = ERR_LOG_PERSIST_JOBID_INVALID; + SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); + return; + } if (pLogPersistStartMsg->fileSize < MAX_PERSISTER_BUFFER_SIZE) { std::cout << "Persist log file size less than min size" << std::endl; pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - pLogPersistStartRst->result = RET_FAIL; - pLogPersistStartRst->reason = ERR_LOG_PERSIST_FILE_SIZE_INVALID; + pLogPersistStartRst->result = ERR_LOG_PERSIST_FILE_SIZE_INVALID; SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; @@ -127,8 +132,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } else { cout << "FileName is not valid!" << endl; pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - pLogPersistStartRst->result = RET_FAIL; - pLogPersistStartRst->reason = ERR_LOG_PERSIST_FILE_NAME_INVALID; + pLogPersistStartRst->result = ERR_LOG_PERSIST_FILE_NAME_INVALID; SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; @@ -149,7 +153,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartRst->result = persister->Init(); int rotatorRes = rotator->Init(); if (pLogPersistStartRst->result != 0 || saveInfoRes != 0 || rotatorRes != 0) { - pLogPersistStartRst->reason = rotatorRes; cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { @@ -188,8 +191,7 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead rst = LogPersister::Kill(pLogPersistStopMsg->jobId); if (pLogPersistStopRst) { pLogPersistStopRst->jobId = pLogPersistStopMsg->jobId; - pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pLogPersistStopRst->reason = rst; + pLogPersistStopRst->result = (rst < 0) ? rst : RET_SUCCESS; pLogPersistStopRst++; msgNum++; } @@ -198,8 +200,7 @@ void HandlePersistDeleteRequest(char* reqMsg, std::shared_ptr logRead rst = LogPersister::Kill((*it).jobId); if (pLogPersistStopRst) { pLogPersistStopRst->jobId = (*it).jobId; - pLogPersistStopRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pLogPersistStopRst->reason = rst; + pLogPersistStopRst->result = (rst < 0) ? rst : RET_SUCCESS; pLogPersistStopRst++; msgNum++; } @@ -238,8 +239,7 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade rst = LogPersister::Query(pLogPersistQueryMsg->logType, resultList); for (it = resultList.begin(); it != resultList.end(); ++it) { if (pLogPersistQueryRst) { - pLogPersistQueryRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pLogPersistQueryRst->reason = rst; + pLogPersistQueryRst->result = (rst < 0) ? rst : RET_SUCCESS; pLogPersistQueryRst->jobId = (*it).jobId; pLogPersistQueryRst->logType = (*it).logType; pLogPersistQueryRst->compressAlg = (*it).compressAlg; @@ -286,8 +286,7 @@ void HandleBufferResizeRequest(char* reqMsg, std::shared_ptr logReade if (pBuffResizeRst) { pBuffResizeRst->logType = pBuffResizeMsg->logType; pBuffResizeRst->buffSize = pBuffResizeMsg->buffSize; - pBuffResizeRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pBuffResizeRst->reason = rst; + pBuffResizeRst->result = (rst < 0) ? rst : RET_SUCCESS; pBuffResizeRst++; } pBuffResizeMsg++; @@ -322,8 +321,7 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, if (pBuffSizeRst) { pBuffSizeRst->logType = pBuffSizeMsg->logType; pBuffSizeRst->buffSize = buffLen; - pBuffSizeRst->result = (buffLen < 0) ? RET_FAIL : RET_SUCCESS; - pBuffSizeRst->reason = buffLen; + pBuffSizeRst->result = (buffLen < 0) ? buffLen : RET_SUCCESS; pBuffSizeRst++; } recvMsgLen += sizeof(BuffSizeMsg); @@ -348,15 +346,13 @@ void HandleInfoQueryRequest(char* reqMsg, std::shared_ptr logReader, pStatisticInfoQueryRsp->domain = pStatisticInfoQueryReq->domain; rst = buffer->GetStatisticInfoByLog(pStatisticInfoQueryReq->logType, pStatisticInfoQueryRsp->printLen, pStatisticInfoQueryRsp->cacheLen, pStatisticInfoQueryRsp->dropped); - pStatisticInfoQueryRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pStatisticInfoQueryRsp->reason = rst; + pStatisticInfoQueryRsp->result = (rst < 0) ? rst : RET_SUCCESS; } else { pStatisticInfoQueryRsp->logType = pStatisticInfoQueryReq->logType; pStatisticInfoQueryRsp->domain = pStatisticInfoQueryReq->domain; rst = buffer->GetStatisticInfoByDomain(pStatisticInfoQueryReq->domain, pStatisticInfoQueryRsp->printLen, pStatisticInfoQueryRsp->cacheLen, pStatisticInfoQueryRsp->dropped); - pStatisticInfoQueryRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pStatisticInfoQueryRsp->reason = rst; + pStatisticInfoQueryRsp->result = (rst < 0) ? rst : RET_SUCCESS; } SetMsgHead(&pStatisticInfoQueryRsp->msgHeader, MC_RSP_STATISTIC_INFO_QUERY, sizeof(StatisticInfoQueryResponse) - sizeof(MessageHeader)); @@ -374,14 +370,12 @@ void HandleInfoClearRequest(char* reqMsg, std::shared_ptr logReader, pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; rst = buffer->ClearStatisticInfoByLog(pStatisticInfoClearReq->logType); - pStatisticInfoClearRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pStatisticInfoClearRsp->reason = rst; + pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; } else { pStatisticInfoClearRsp->logType = pStatisticInfoClearReq->logType; pStatisticInfoClearRsp->domain = pStatisticInfoClearReq->domain; rst = buffer->ClearStatisticInfoByDomain(pStatisticInfoClearReq->domain); - pStatisticInfoClearRsp->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pStatisticInfoClearRsp->reason = rst; + pStatisticInfoClearRsp->result = (rst < 0) ? rst : RET_SUCCESS; } SetMsgHead(&pStatisticInfoClearRsp->msgHeader, MC_RSP_STATISTIC_INFO_CLEAR, sizeof(StatisticInfoClearResponse) - sizeof(MessageHeader)); @@ -408,8 +402,7 @@ void HandleBufferClearRequest(char* reqMsg, std::shared_ptr logReader rst = buffer->Delete(pLogClearMsg->logType); if (pLogClearRst) { pLogClearRst->logType = pLogClearMsg->logType; - pLogClearRst->result = (rst < 0) ? RET_FAIL : RET_SUCCESS; - pLogClearRst->reason = rst; + pLogClearRst->result = (rst < 0) ? rst : RET_SUCCESS; pLogClearRst++; } pLogClearMsg++; diff --git a/services/hilogtool/include/hilogtool.h b/services/hilogtool/include/hilogtool.h index 67fa9e9..aea36bb 100644 --- a/services/hilogtool/include/hilogtool.h +++ b/services/hilogtool/include/hilogtool.h @@ -17,6 +17,8 @@ namespace OHOS { namespace HiviewDFX { +#define DOMAIN_NUMBER_BASE (16) +#define DOMAIN_MAX_SCOPE 0xDFFFFFF typedef struct { uint16_t noBlockMode; uint8_t nPid; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 88f40c9..1da81ed 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -34,7 +34,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -const int MSG_MAX_LEN = 2048; const int LOG_PERSIST_FILE_SIZE = 4 * ONE_MB; const int LOG_PERSIST_FILE_NUM = 10; const uint32_t DEFAULT_JOBID = 1; @@ -274,7 +273,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::str for (iter = 0; iter < logTypeNum; iter++) { pBuffSizeMsg->logType = GetLogType(vecLogType[iter]); if (pBuffSizeMsg->logType == 0xffff) { - ParseErrorCode(ERR_LOG_TYPE_INVALID); + cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } pBuffSizeMsg++; @@ -293,7 +292,7 @@ int32_t BufferSizeOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::str for (iter = 0; iter < logTypeNum; iter++) { pBuffResizeMsg->logType = GetLogType(vecLogType[iter]); if (pBuffResizeMsg->logType == 0xffff) { - ParseErrorCode(ERR_LOG_TYPE_INVALID); + cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } pBuffResizeMsg->buffSize = GetBuffSize(buffSizeStr); @@ -321,14 +320,13 @@ int32_t StatisticInfoOp(SeqPacketSocketClient& controller, uint8_t msgCmd, if (domainStr == "") { domain = 0xffffffff; if (logType == 0xffff) { - ParseErrorCode(ERR_LOG_TYPE_INVALID); + cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } } else { std::istringstream(domainStr) >> domain; - if (domain == 0) { - std::cout << "Invalid parameter" << std::endl; - ParseErrorCode(ERR_DOMAIN_INVALID); + if (domain == 0 || domain > DOMAIN_MAX_SCOPE) { + cout << ParseErrorCode(ERR_DOMAIN_INVALID) << endl; return RET_FAIL; } } @@ -367,17 +365,17 @@ int32_t LogClearOp(SeqPacketSocketClient& controller, uint8_t msgCmd, std::strin LogClearRequest* pLogClearReq = reinterpret_cast(msgToSend); LogClearMsg* pLogClearMsg = reinterpret_cast(&pLogClearReq->logClearMsg); if (!pLogClearMsg) { - ParseErrorCode(ERR_MEM_ALLOC_FAIL); + cout << ParseErrorCode(ERR_MEM_ALLOC_FAIL) << endl; return RET_FAIL; } if (logTypeNum * sizeof(LogClearMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { - ParseErrorCode(ERR_MSG_LEN_INVALID); + cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl; return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { pLogClearMsg->logType = GetLogType(vecLogType[iter]); if (pLogClearMsg->logType == 0xffff) { - ParseErrorCode(ERR_LOG_TYPE_INVALID); + cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } pLogClearMsg++; @@ -409,19 +407,23 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi LogPersistStartMsg* pLogPersistStartMsg = reinterpret_cast(&pLogPersistStartReq->logPersistStartMsg); if (sizeof(LogPersistStartRequest) > MSG_MAX_LEN) { - ParseErrorCode(ERR_MSG_LEN_INVALID); + cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl; return RET_FAIL; } for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); if (tmpType == 0xffff) { - ParseErrorCode(ERR_LOG_TYPE_INVALID); + cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; } pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID : stoi(logPersistParam->jobIdStr); + if (pLogPersistStartMsg->jobId <= 0) { + cout << ParseErrorCode(ERR_LOG_PERSIST_JOBID_INVALID) << endl; + return RET_FAIL; + } pLogPersistStartMsg->compressAlg = (logPersistParam->compressAlgStr == "") ? COMPRESS_TYPE_ZLIB : GetCompressAlg(logPersistParam->compressAlgStr); pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( @@ -429,7 +431,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { - ParseErrorCode(ERR_LOG_PERSIST_FILE_NAME_INVALID); + cout << ParseErrorCode(ERR_LOG_PERSIST_FILE_NAME_INVALID) << endl; return RET_FAIL; } if (logPersistParam->fileNameStr != " ") { @@ -451,7 +453,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi break; } if (jobIdNum * sizeof(LogPersistStopMsg) + sizeof(MessageHeader) > MSG_MAX_LEN) { - ParseErrorCode(ERR_MSG_LEN_INVALID); + cout << ParseErrorCode(ERR_MSG_LEN_INVALID) << endl; return RET_FAIL; } for (iter = 0; iter < jobIdNum; iter++) { @@ -472,7 +474,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi for (iter = 0; iter < logTypeNum; iter++) { uint16_t tmpType = GetLogType(vecLogType[iter]); if (tmpType == 0xffff) { - ParseErrorCode(ERR_LOG_TYPE_INVALID); + cout << ParseErrorCode(ERR_LOG_TYPE_INVALID) << endl; return RET_FAIL; } pLogPersistQueryMsg->logType = (0b01 << tmpType) | pLogPersistQueryMsg->logType; @@ -514,7 +516,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType PropertySet(key.c_str(), "false"); cout << "hilog private formatter is disabled" << endl; } else { - ParseErrorCode(ERR_PROPERTY_VALUE_INVALID); + cout << ParseErrorCode(ERR_PRIVATE_SWITCH_VALUE_INVALID) << endl; return RET_FAIL; } break; @@ -568,7 +570,7 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType PropertySet(key.c_str(), "false"); cout << "flow control by domain is disabled" << endl; } else { - ParseErrorCode(ERR_PROPERTY_VALUE_INVALID); + cout << ParseErrorCode(ERR_FLOWCTRL_SWITCH_VALUE_INVALID) << endl; return RET_FAIL; } break; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 6635b67..d48b96a 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -20,7 +20,7 @@ #include #include #include - +#include #include "hilog/log.h" #include "format.h" #include "log_controller.h" @@ -35,85 +35,49 @@ using hash_t = std::uint64_t; constexpr hash_t PRIME = 0x100000001B3ull; constexpr hash_t BASIS = 0xCBF29CE484222325ull; -string ParseErrorCode(ErrorCode errorCode) { - string errorMsg; - switch (errorCode) - { - case ERR_LOG_LEVEL_INVALID: - errorMsg = "Invalid log level"; - break; - case ERR_LOG_TYPE_INVALID: - errorMsg = "Invalid log type"; - break; - case ERR_QUERY_TYPE_INVALID: - errorMsg = "Query condition on both types and excluded types is undefined.\n"; - errorMsg += "Please remove types or excluded types condition, and try again."; - break; - case ERR_QUERY_LEVEL_INVALID: - errorMsg = "Query condition on both levels and excluded levels is undefined.\n"; - errorMsg += "Please remove levels or excluded levels condition, and try again."; - break; - case ERR_QUERY_TAG_INVALID: - errorMsg = "Query condition on both tags and excluded tags is undefined.\n"; - errorMsg += "Please remove tags or excluded tags condition, and try again."; - break; - case ERR_QUERY_PID_INVALID: - errorMsg = "Query condition on both pid and excluded pid is undefined.\n"; - errorMsg += "Please remove pid or excluded pid condition, and try again."; - break; - case ERR_BUFF_SIZE_INVALID: - errorMsg = "Invalid buffer size"; - break; - case ERR_BUFF_SIZE_EXP: - errorMsg = "buffer size exception"; - break; - case ERR_LOG_PERSIST_FILE_SIZE_INVALID: - errorMsg = "Invalid log persist file size"; - break; - case ERR_LOG_PERSIST_FILE_NAME_INVALID: - errorMsg = "Invalid log persist file name"; - break; - case ERR_LOG_PERSIST_FILE_PATH_EXP: - errorMsg = "log persist file path exception"; - break; - case ERR_LOG_PERSIST_COMPRESS_INIT_FAIL: - errorMsg = "log persist compress initial failed"; - break; - case ERR_LOG_PERSIST_FILE_OPEN_FAIL: - errorMsg = "log persist open file failed"; - break; - case ERR_LOG_PERSIST_MMAP_FAIL: - errorMsg = "log persist mmap failed"; - break; - case ERR_LOG_PERSIST_JOBID_FAIL: - errorMsg = "log persist jobid not exist"; - break; - case ERR_DOMAIN_INVALID: - errorMsg = "Invalid domain"; - break; - case ERR_MEM_ALLOC_FAIL: - errorMsg = "alloc memory failed"; - break; - case ERR_MSG_LEN_INVALID: - errorMsg = "Invalid message length"; - break; - case ERR_PROPERTY_VALUE_INVALID: - errorMsg = "Invalid property value"; - break; - case ERR_LOG_CONTENT_NULL: - errorMsg = "log content NULL"; - break; - case ERR_COMMAND_NOT_FOUND: - errorMsg = "command not found"; - break; - case ERR_FORMAT_INVALID: - errorMsg = "Invalid format parameter"; - break; - default: - break; - } - return errorMsg; +unordered_map errorMsg +{ + {ERR_LOG_LEVEL_INVALID, "Invalid log level, the valid log levels include D/I/W/E/F"}, + {ERR_LOG_TYPE_INVALID, "Invalid log type, the valid log types include app/core/init"}, + {ERR_QUERY_TYPE_INVALID, "Query condition on both types and excluded types is undefined"}, + {ERR_QUERY_LEVEL_INVALID, "Query condition on both levels and excluded levels is undefined"}, + {ERR_QUERY_DOMAIN_INVALID, "Invalid domain format, a hexadecimal number is needed"}, + {ERR_QUERY_TAG_INVALID, "Query condition on both tags and excluded tags is undefined"}, + {ERR_QUERY_PID_INVALID, "Query condition on both pid and excluded pid is undefined"}, + {ERR_BUFF_SIZE_INVALID, "Invalid buffer size, buffer size should be more than 0 and less than " + + to_string(MAX_BUFFER_SIZE)}, + {ERR_BUFF_SIZE_EXP, "Buffer resize exception"}, + {ERR_LOG_PERSIST_FILE_SIZE_INVALID, "Invalid log persist file size, file size should be not less than " + + to_string(MAX_PERSISTER_BUFFER_SIZE)}, + {ERR_LOG_PERSIST_FILE_NAME_INVALID, "Invalid log persist file name, file name should notĀ containĀ [\\/:*?\"<>|]"}, + {ERR_LOG_PERSIST_COMPRESS_BUFFER_EXP, "Invalid Log persist compression buffer"}, + {ERR_LOG_PERSIST_FILE_PATH_INVALID, "Invalid persister file path"}, + {ERR_LOG_PERSIST_COMPRESS_INIT_FAIL, "Log persist compression initialization failed"}, + {ERR_LOG_PERSIST_FILE_OPEN_FAIL, "Log persist open file failed"}, + {ERR_LOG_PERSIST_MMAP_FAIL, "Log persist mmap failed"}, + {ERR_LOG_PERSIST_JOBID_FAIL, "Log persist jobid not exist"}, + {ERR_DOMAIN_INVALID, "Invalid domain, domain should not be more than 0 and less than " + + to_string(DOMAIN_MAX_SCOPE)}, + {ERR_MEM_ALLOC_FAIL, "Alloc memory failed"}, + {ERR_MSG_LEN_INVALID, "Invalid message length, message length should be not more than " + + to_string(MSG_MAX_LEN)}, + {ERR_PRIVATE_SWITCH_VALUE_INVALID, "Invalid private switch value, valid:on/off"}, + {ERR_FLOWCTRL_SWITCH_VALUE_INVALID, "Invalid flowcontrl switch value, valid:pidon/pidoff/domainon/domainoff"}, + {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"} +}; + +string ParseErrorCode(ErrorCode errorCode) +{ + if (errorMsg.count(errorCode) == 0) { + cout << "ERR_CODE not exist" << endl; + } + string errorMsgStr = "[ERR_CODE:" + to_string(errorCode) + "], " + errorMsg[errorCode]; + return errorMsgStr; } + hash_t Hash(char const *str) { hash_t ret {BASIS}; @@ -152,14 +116,15 @@ string GetOrigType(uint16_t shiftType) { string logType = ""; if (((1 << LOG_INIT) & shiftType) != 0) { - logType += "init"; + logType += "init,"; } if (((1 << LOG_CORE) & shiftType) != 0) { - logType += "core"; + logType += "core,"; } if (((1 << LOG_APP) & shiftType) != 0) { - logType += "app "; + logType += "app,"; } + logType.erase(logType.end() - 1); return logType; } @@ -220,10 +185,10 @@ int32_t ControlCmdResult(const char* message) } BuffSizeResult* pBuffSizeRst = (BuffSizeResult*)&pBuffSizeRsp->buffSizeRst; while (pBuffSizeRst && resultLen < msgLen) { - if (pBuffSizeRst->result == RET_FAIL) { + if (pBuffSizeRst->result < 0) { outputStr += GetLogTypeStr(pBuffSizeRst->logType); - outputStr += " buffer size fail, reason:"; - outputStr += ParseErrorCode((ErrorCode)pBuffSizeRst->reason); + outputStr += " buffer size fail\n"; + outputStr += ParseErrorCode((ErrorCode)pBuffSizeRst->result); outputStr += "\n"; } else { outputStr += GetLogTypeStr(pBuffSizeRst->logType); @@ -243,10 +208,10 @@ int32_t ControlCmdResult(const char* message) } BuffResizeResult* pBuffResizeRst = (BuffResizeResult*)&pBuffResizeRsp->buffResizeRst; while (pBuffResizeRst && resultLen < msgLen) { - if (pBuffResizeRst->result == RET_FAIL) { + if (pBuffResizeRst->result < 0) { outputStr += GetLogTypeStr(pBuffResizeRst->logType); - outputStr += " buffer resize fail, reason:"; - outputStr += ParseErrorCode((ErrorCode)pBuffResizeRst->reason); + outputStr += " buffer resize fail\n"; + outputStr += ParseErrorCode((ErrorCode)pBuffResizeRst->result); outputStr += "\n"; } else { outputStr += GetLogTypeStr(pBuffResizeRst->logType); @@ -282,10 +247,10 @@ int32_t ControlCmdResult(const char* message) outputStr += logOrDomain; outputStr += " dropped log lines is "; outputStr += GetByteLenStr(staInfoQueryRsp->dropped); - } else if (staInfoQueryRsp->result == RET_FAIL) { + } else if (staInfoQueryRsp->result < 0) { outputStr += logOrDomain; - outputStr += " statistic info query fail, reason:"; - outputStr += ParseErrorCode((ErrorCode)staInfoQueryRsp->reason); + outputStr += " statistic info query fail\n"; + outputStr += ParseErrorCode((ErrorCode)staInfoQueryRsp->result); } break; } @@ -303,10 +268,10 @@ int32_t ControlCmdResult(const char* message) if (staInfoClearRsp->result == RET_SUCCESS) { outputStr += logOrDomain; outputStr += " statistic info clear success "; - } else if (staInfoClearRsp->result == RET_FAIL) { + } else if (staInfoClearRsp->result < 0) { outputStr += logOrDomain; - outputStr += " statistic info clear fail, reason:"; - outputStr += ParseErrorCode((ErrorCode)staInfoClearRsp->reason); + outputStr += " statistic info clear fail\n"; + outputStr += ParseErrorCode((ErrorCode)staInfoClearRsp->result); } break; } @@ -317,10 +282,10 @@ int32_t ControlCmdResult(const char* message) } LogClearResult* pLogClearRst = (LogClearResult*)&pLogClearRsp->logClearRst; while (pLogClearRst && resultLen < msgLen) { - if (pLogClearRst->result == RET_FAIL) { + if (pLogClearRst->result < 0) { outputStr += GetLogTypeStr(pLogClearRst->logType); - outputStr += " log clear fail, reason:"; - outputStr += ParseErrorCode((ErrorCode)pLogClearRst->reason); + outputStr += " log clear fail\n"; + outputStr += ParseErrorCode((ErrorCode)pLogClearRst->result); outputStr += "\n"; } else { outputStr += GetLogTypeStr(pLogClearRst->logType); @@ -340,11 +305,11 @@ int32_t ControlCmdResult(const char* message) LogPersistStartResult* pLogPersistStartRst = (LogPersistStartResult*)&pLogPersistStartRsp->logPersistStartRst; while (pLogPersistStartRst && resultLen < msgLen) { - if (pLogPersistStartRst->result == RET_FAIL) { + if (pLogPersistStartRst->result < 0) { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); outputStr += "] start failed\n"; - outputStr += ParseErrorCode((ErrorCode)pLogPersistStartRst->reason); + outputStr += ParseErrorCode((ErrorCode)pLogPersistStartRst->result); } else { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStartRst->jobId); @@ -362,11 +327,11 @@ int32_t ControlCmdResult(const char* message) } LogPersistStopResult* pLogPersistStopRst = (LogPersistStopResult*)&pLogPersistStopRsp->logPersistStopRst; while (pLogPersistStopRst && resultLen < msgLen) { - if (pLogPersistStopRst->result == RET_FAIL) { + if (pLogPersistStopRst->result < 0) { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); outputStr += "] stop failed\n"; - outputStr += ParseErrorCode((ErrorCode)pLogPersistStopRst->reason); + outputStr += ParseErrorCode((ErrorCode)pLogPersistStopRst->result); } else { outputStr += "Persist task [jobid:"; outputStr += to_string(pLogPersistStopRst->jobId); @@ -385,11 +350,11 @@ int32_t ControlCmdResult(const char* message) LogPersistQueryResult* pLogPersistQueryRst = (LogPersistQueryResult*)&pLogPersistQueryRsp->logPersistQueryRst; while (pLogPersistQueryRst && resultLen < msgLen) { - if (pLogPersistQueryRst->result == RET_FAIL) { + if (pLogPersistQueryRst->result < 0) { outputStr = "Persist task [logtype:"; outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); outputStr += "] query failed\n"; - outputStr += ParseErrorCode((ErrorCode)pLogPersistQueryRst->reason); + outputStr += ParseErrorCode((ErrorCode)pLogPersistQueryRst->result); } else { outputStr += to_string(pLogPersistQueryRst->jobId); outputStr += " "; @@ -449,7 +414,7 @@ HilogShowFormat HilogFormat (const char* formatArg) format = MONOTONIC_SHOWFORMAT; break; default: - cout << ParseErrorCode(ERR_FORMAT_INVALID)< v(sregex_token_iterator(domains.begin() + 1, domains.end(), delimiter, -1), sregex_token_iterator()); for (auto s: v) { + strtoul(s.c_str(), &endptr, DOMAIN_NUMBER_BASE); + if (*endptr != '\0') { + cout << ParseErrorCode(ERR_QUERY_DOMAIN_INVALID) << endl; + exit(RET_FAIL); + } context.noDomains[context.nNoDomain++] = s; } } else { vector v(sregex_token_iterator(domains.begin(), domains.end(), delimiter, -1), sregex_token_iterator()); for (auto s: v) { + strtoul(s.c_str(), &endptr, DOMAIN_NUMBER_BASE); + if (*endptr != '\0') { + cout << ParseErrorCode(ERR_QUERY_DOMAIN_INVALID) << endl; + exit(RET_FAIL); + } context.domains[context.nDomain++] = s; context.domainArgs += (s + " "); } @@ -392,7 +403,7 @@ int HilogEntry(int argc, char* argv[]) } } if (context.nTag != 0 && context.nNoTag != 0) { - cout << ParseErrorCode(ERR_QUERY_PID_INVALID) << endl; + cout << ParseErrorCode(ERR_QUERY_TAG_INVALID) << endl; exit(RET_FAIL); } -- Gitee