From 5be5216840fb092e0e9bea8c4b232e0dc36b67bd Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 21 Jun 2021 03:01:41 +0000 Subject: [PATCH 01/40] Description:persist path Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 14 +------------- services/hilogd/log_querier.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index b49a53e..8ed77da 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -40,7 +40,7 @@ namespace HiviewDFX { using namespace std::literals::chrono_literals; using namespace std; -constexpr auto& g_logPersisterDir = "/data/misc/logd/"; + static std::list> logPersisters; static std::mutex g_listMutex; @@ -89,19 +89,7 @@ LogPersister::~LogPersister() int LogPersister::Init() { - char cPath[PATH_MAX]; - char *realPath = realpath(path.c_str(), cPath); - if (realPath == nullptr) { - return RET_FAIL; - } - path = std::string(cPath); - if (path.rfind(g_logPersisterDir, 0) != 0) { - return RET_FAIL; - } int nPos = path.find_last_of('/'); - if (nPos == RET_FAIL) { - return RET_FAIL; - } mmapPath = path.substr(0, nPos) + "/." + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { if (errno == ENOENT) { diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e08ed10..cd04c1a 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -42,7 +42,7 @@ namespace HiviewDFX { using namespace std; constexpr int MAX_DATA_LEN = 2048; - +char g_logPersisterDir[] = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< logReade pLogPersistQueryRst->logType = (*it).logType; pLogPersistQueryRst->compressType = (*it).compressType; pLogPersistQueryRst->compressAlg = (*it).compressAlg; - if (strcpy_s(pLogPersistQueryRst->filePath, FILE_PATH_MAX_LEN, (*it).filePath)) { + strcat_s(g_logPersisterDir, FILE_PATH_MAX_LEN, (*it).filePath); + if (strcpy_s(pLogPersistQueryRst->filePath, FILE_PATH_MAX_LEN, g_logPersisterDir)) { + cout << pLogPersistQueryRst->filePath << endl; return; } pLogPersistQueryRst->fileSize = (*it).fileSize; -- Gitee From 91e4b1df825f284645e4c3acc4b5c7fb0ae794cd Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 21 Jun 2021 07:01:37 +0000 Subject: [PATCH 02/40] Description: persist path modify Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilogtool_msg.h | 4 ++-- services/hilogd/log_persister.cpp | 5 ++++- services/hilogd/log_querier.cpp | 13 +++++++------ services/hilogtool/log_controller.cpp | 8 ++++---- services/hilogtool/main.cpp | 10 +++++----- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index cd4b03c..1cfe5a5 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -238,7 +238,7 @@ typedef struct { std::string compressAlgStr; std::string fileSizeStr; std::string fileNumStr; - std::string filePathStr; + std::string fileNameStr; std::string jobIdStr; } LogPersistParam; typedef struct { @@ -347,4 +347,4 @@ typedef struct { std::string algorithmArgs; } HilogArgs; -#endif /* HILOGTOOL_MSG_H */ \ No newline at end of file +#endif /* HILOGTOOL_MSG_H */ diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 8ed77da..d15525f 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -90,6 +90,9 @@ LogPersister::~LogPersister() int LogPersister::Init() { int nPos = path.find_last_of('/'); + if (nPos == RET_FAIL) { + return RET_FAIL; + } mmapPath = path.substr(0, nPos) + "/." + to_string(id); if (access(path.substr(0, nPos).c_str(), F_OK) != 0) { if (errno == ENOENT) { @@ -168,7 +171,7 @@ int LogPersister::Init() void LogPersister::NotifyForNewData() { - condVariable.notify_one(); + condVariable.notify_one(); isNotified = true; } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index cd04c1a..998a260 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -42,7 +42,7 @@ namespace HiviewDFX { using namespace std; constexpr int MAX_DATA_LEN = 2048; -char g_logPersisterDir[] = "/data/misc/logd/"; +string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1< logReade if (pLogPersistStartRst == nullptr) { return; } + string logPersisterPath = g_logPersisterDir + string(pLogPersistStartMsg->filePath) ; + strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, @@ -135,7 +137,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade pLogPersistStartRst->result = persister->Init(); persister->queryCondition.types = pLogPersistStartMsg->logType; persister->queryCondition.levels = DEFAULT_LOG_LEVEL; - if (pLogPersistStartRst->result == RET_FAIL) { + if (pLogPersistStartRst->result == RET_FAIL) { persister.reset(); } else { persister->Start(); @@ -217,15 +219,14 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade pLogPersistQueryRst->logType = (*it).logType; pLogPersistQueryRst->compressType = (*it).compressType; pLogPersistQueryRst->compressAlg = (*it).compressAlg; - strcat_s(g_logPersisterDir, FILE_PATH_MAX_LEN, (*it).filePath); - if (strcpy_s(pLogPersistQueryRst->filePath, FILE_PATH_MAX_LEN, g_logPersisterDir)) { - cout << pLogPersistQueryRst->filePath << endl; + if (strcpy_s(pLogPersistQueryRst->filePath, FILE_PATH_MAX_LEN, (*it).filePath)) { + cout << "path" << pLogPersistQueryRst->filePath << endl; return; } pLogPersistQueryRst->fileSize = (*it).fileSize; pLogPersistQueryRst->fileNum = (*it).fileNum; pLogPersistQueryRst++; - msgNum++; + msgNum++; if (msgNum * sizeof(LogPersistQueryResult) + sizeof(MessageHeader) > MAX_DATA_LEN) { msgNum--; break; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index d8284f2..36bf51a 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -376,13 +376,13 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi logPersistParam->fileSizeStr); pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); - if (logPersistParam->filePathStr == "") { - logPersistParam->filePathStr = "/data/misc/logd/log_" + to_string(time(nullptr)); + if (logPersistParam->fileNameStr == "") { + logPersistParam->fileNameStr = to_string(time(nullptr)); } - if (logPersistParam->filePathStr.size() > FILE_PATH_MAX_LEN) { + if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { return RET_FAIL; } - ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->filePathStr.c_str()); + ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str()); SetMsgHead(&pLogPersistStartReq->msgHeader, msgCmd, sizeof(LogPersistStartRequest)); controller.WriteAll(msgToSend, sizeof(LogPersistStartRequest)); break; diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 8645f80..ced850d 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -78,8 +78,8 @@ static void Helper() " -e , --regex=\n" " show the logs which match the regular expression,\n" " is a regular expression.\n" - " -F , --path=\n" - " set log file path.\n" + " -F , --filename=\n" + " set log file name.\n" " -l , --length=\n" " set single log file size.\n" " -n , --number\n" @@ -195,7 +195,7 @@ int HilogEntry(int argc, char* argv[]) { "compress", required_argument, nullptr, 'c' }, { "stream", required_argument, nullptr, 'm' }, { "number", required_argument, nullptr, 'n' }, - { "path", required_argument, nullptr, 'F' }, + { "filename", required_argument, nullptr, 'F' }, { "private", required_argument, nullptr, 'p' }, { "domain", required_argument, nullptr, 'D' }, { "tag", required_argument, nullptr, 'T' }, @@ -314,7 +314,7 @@ int HilogEntry(int argc, char* argv[]) context.fileNumArgs = optarg; break; case 'F': - context.filePathArgs = optarg; + context.fileNameArgs = optarg; break; case 'j': context.jobIdArgs = optarg; @@ -393,7 +393,7 @@ int HilogEntry(int argc, char* argv[]) logPersistParam.compressAlgStr = context.algorithmArgs; logPersistParam.fileSizeStr = context.fileSizeArgs; logPersistParam.fileNumStr = context.fileNumArgs; - logPersistParam.filePathStr = context.filePathArgs; + logPersistParam.fileNameStr = context.fileNameArgs; logPersistParam.jobIdStr = context.jobIdArgs; if (context.logFileCtrlArgs == "start") { ret = LogPersistOp(controller, MC_REQ_LOG_PERSIST_START, &logPersistParam); -- Gitee From 46dc77ec6504a4e374a32cfd80f21b4535a9d63f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 21 Jun 2021 07:09:42 +0000 Subject: [PATCH 03/40] Description: persist safe Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 998a260..9d002cc 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -203,11 +203,9 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade uint16_t sendMsgLen = 0; int32_t rst = 0; list::iterator it; - if (msgLen > sizeof(LogPersistQueryMsg) * LOG_TYPE_MAX) { return; } - while (pLogPersistQueryMsg && recvMsgLen < msgLen) { list resultList; cout << pLogPersistQueryMsg->logType << endl; @@ -220,7 +218,6 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade pLogPersistQueryRst->compressType = (*it).compressType; pLogPersistQueryRst->compressAlg = (*it).compressAlg; if (strcpy_s(pLogPersistQueryRst->filePath, FILE_PATH_MAX_LEN, (*it).filePath)) { - cout << "path" << pLogPersistQueryRst->filePath << endl; return; } pLogPersistQueryRst->fileSize = (*it).fileSize; -- Gitee From aa932117b76ca18064e106b580f26967c5d758ab Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 21 Jun 2021 07:30:29 +0000 Subject: [PATCH 04/40] Description: delete space Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 9d002cc..cfb1066 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -123,7 +123,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } - string logPersisterPath = g_logPersisterDir + string(pLogPersistStartMsg->filePath) ; + string logPersisterPath = g_logPersisterDir + string(pLogPersistStartMsg->filePath); strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( -- Gitee From fb4c34a0fa0089d14542681d111bcfb0f5b4c886 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 22 Jun 2021 01:52:30 +0000 Subject: [PATCH 05/40] Description: hilogtool change line Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_display.cpp | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 540c0d4..76e636d 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -398,6 +398,19 @@ bool HilogMatchByRegex(string context, string regExpArg) } } +void Stringsplit(const string& str, const char split, vector& res) +{ + if (str == "") + return; + string strs = str + split; + size_t pos = strs.find(split); + while (pos != strs.npos) { + string temp = strs.substr(0, pos); + res.push_back(temp); + strs = strs.substr(pos + 1, strs.size()); + pos = strs.find(split); + } +} void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, vector& tailBuffer) { if (data->sendId == SENDIDN) { @@ -444,9 +457,27 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* showBuffer.tag_len = data->tag_len; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; + string conOutStr(data->data + data->tag_len); + if(conOutStr[conOutStr.length()-1] == '\n') { + conOutStr[conOutStr.length()-1] = 0; + } + vector conOutStrList; + Stringsplit(conOutStr, '\n', conOutStrList); + if (!conOutStrList.empty()) { + for (auto con : conOutStrList) { + strncpy_s(data->data + data->tag_len, MAX_LOG_LEN, const_cast(con.c_str()), MAX_LOG_LEN); + showBuffer.data = data->data; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); + if (context->tailLines) { + tailBuffer.emplace_back(buffer); + return; + } + cout << buffer << endl; + } + return; + } showBuffer.data = data->data; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); - if (context->tailLines) { tailBuffer.emplace_back(buffer); return; -- Gitee From da5e5c43c3699bb9845d424282c2e1746356048f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 22 Jun 2021 01:58:08 +0000 Subject: [PATCH 06/40] Description: hilogtool change line Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_display.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 76e636d..0a253a6 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -405,10 +405,10 @@ void Stringsplit(const string& str, const char split, vector& res) string strs = str + split; size_t pos = strs.find(split); while (pos != strs.npos) { - string temp = strs.substr(0, pos); - res.push_back(temp); - strs = strs.substr(pos + 1, strs.size()); - pos = strs.find(split); + string temp = strs.substr(0, pos); + res.push_back(temp); + strs = strs.substr(pos + 1, strs.size()); + pos = strs.find(split); } } void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, vector& tailBuffer) -- Gitee From 35e5bf7843299e8552772370b76a33e4141893c4 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 22 Jun 2021 02:30:43 +0000 Subject: [PATCH 07/40] Description: hilogtool change line Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_display.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 0a253a6..5e63ea6 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -402,21 +402,21 @@ void Stringsplit(const string& str, const char split, vector& res) { if (str == "") return; - string strs = str + split; - size_t pos = strs.find(split); - while (pos != strs.npos) { + string strs = str + split; + size_t pos = strs.find(split); + while (pos != strs.npos) { string temp = strs.substr(0, pos); res.push_back(temp); strs = strs.substr(pos + 1, strs.size()); pos = strs.find(split); - } + } } -void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, vector& tailBuffer) +void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, + vector& tailBuffer) { if (data->sendId == SENDIDN) { return; } - if (data->length == 0) { #ifdef DEBUG cout << "Log content null" << endl; @@ -458,7 +458,7 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; string conOutStr(data->data + data->tag_len); - if(conOutStr[conOutStr.length()-1] == '\n') { + if (conOutStr[conOutStr.length()-1] == '\n') { conOutStr[conOutStr.length()-1] = 0; } vector conOutStrList; -- Gitee From 2940305c88ae003e5e6186fa612f05b04af38d9e Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 22 Jun 2021 02:35:00 +0000 Subject: [PATCH 08/40] Description: hilogtool change line Team:OTHERS Feature or Bugfix:Bugfix Binary Source:No PrivateCode(Yes/No):No Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_display.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 5e63ea6..97f85eb 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -400,7 +400,7 @@ bool HilogMatchByRegex(string context, string regExpArg) void Stringsplit(const string& str, const char split, vector& res) { - if (str == "") + if (str == "") return; string strs = str + split; size_t pos = strs.find(split); -- Gitee From 1c060299f09a7d86cc401db8ceda185f937cd8ff Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 23 Jun 2021 08:55:35 +0000 Subject: [PATCH 09/40] persisit valid filename && display change line Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 19 +++++++++- services/hilogtool/log_controller.cpp | 3 -- services/hilogtool/log_display.cpp | 54 ++++++++------------------- 3 files changed, 33 insertions(+), 43 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index cfb1066..f77c8ed 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include "compressing_rotator.h" @@ -53,7 +54,13 @@ inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen msgHeader->msgType = msgCmd; msgHeader->msgLen = msgLen; } - +inline bool IsValidFileName(const std::string& strFileName) +{ + // 文件名中不能包含\/:*?"<>|这些字符 + std::regex regExpress("[\\/:*?\"<>|]"); + bool bValid = !std::regex_search(strFileName, regExpress); + return bValid; +} LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; @@ -123,7 +130,15 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } - string logPersisterPath = g_logPersisterDir + string(pLogPersistStartMsg->filePath); + bool bRet = IsValidFileName(string(pLogPersistStartMsg->filePath)); + string logPersisterPath; + if(bRet == true) { + logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" + :g_logPersisterDir + string(pLogPersistStartMsg->filePath); + } else { + cout << "FileName is not valid!" << endl; + return; + } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 36bf51a..a9aee75 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -376,9 +376,6 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi logPersistParam->fileSizeStr); pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); - if (logPersistParam->fileNameStr == "") { - logPersistParam->fileNameStr = to_string(time(nullptr)); - } if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { return RET_FAIL; } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 97f85eb..465c885 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -398,19 +398,6 @@ bool HilogMatchByRegex(string context, string regExpArg) } } -void Stringsplit(const string& str, const char split, vector& res) -{ - if (str == "") - return; - string strs = str + split; - size_t pos = strs.find(split); - while (pos != strs.npos) { - string temp = strs.substr(0, pos); - res.push_back(temp); - strs = strs.substr(pos + 1, strs.size()); - pos = strs.find(split); - } -} void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* context, vector& tailBuffer) { @@ -429,7 +416,7 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* char* tag = data->data; char* content = data->data + data->tag_len; char *pStrtol = nullptr; - + int logContentPos = 0; if (context->headLines) { if (printHeadCnt++ >= context->headLines) { exit(1); @@ -448,7 +435,6 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* return; } } - char buffer[MAX_LOG_LEN * 2]; showBuffer.level = data->level; showBuffer.pid = data->pid; @@ -457,32 +443,24 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* showBuffer.tag_len = data->tag_len; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; - string conOutStr(data->data + data->tag_len); - if (conOutStr[conOutStr.length()-1] == '\n') { - conOutStr[conOutStr.length()-1] = 0; + string logContent(data->data + data->tag_len); + if (logContent[logContent.length()-1] != '\n') { + logContent += '\n'; } - vector conOutStrList; - Stringsplit(conOutStr, '\n', conOutStrList); - if (!conOutStrList.empty()) { - for (auto con : conOutStrList) { - strncpy_s(data->data + data->tag_len, MAX_LOG_LEN, const_cast(con.c_str()), MAX_LOG_LEN); - showBuffer.data = data->data; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); - if (context->tailLines) { - tailBuffer.emplace_back(buffer); - return; - } - cout << buffer << endl; + std::replace(logContent.begin(), logContent.end(),'\n','\0'); + while (logContent.find_first_of('\0') != size_t(-1)) { + strncpy_s(data->data + data->tag_len, MAX_LOG_LEN, + const_cast(logContent.c_str()), MAX_LOG_LEN); + showBuffer.data = data->data; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); + if (context->tailLines) { + tailBuffer.emplace_back(buffer); + return; } - return; - } - showBuffer.data = data->data; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); - if (context->tailLines) { - tailBuffer.emplace_back(buffer); - return; + cout << buffer << endl; + logContentPos = logContent.find_first_of('\0'); + logContent.erase(0, logContentPos + 1); } - cout << buffer << endl; return; } } // namespace HiviewDFX -- Gitee From 0fa69e505ab9e77bf3f4c968de55afbc68ce7ee4 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 23 Jun 2021 09:48:44 +0000 Subject: [PATCH 10/40] isvalid remove from hilogd to hilogtool Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 18 ++---------------- services/hilogtool/log_controller.cpp | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 298440c..539782b 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include "compressing_rotator.h" @@ -54,13 +53,7 @@ inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen msgHeader->msgType = msgCmd; msgHeader->msgLen = msgLen; } -inline bool IsValidFileName(const std::string& strFileName) -{ - // 文件名中不能包含\/:*?"<>|这些字符 - std::regex regExpress("[\\/:*?\"<>|]"); - bool bValid = !std::regex_search(strFileName, regExpress); - return bValid; -} + LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; @@ -130,15 +123,8 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } - bool bRet = IsValidFileName(string(pLogPersistStartMsg->filePath)); - string logPersisterPath; - if(bRet == true) { - logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" + string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" :g_logPersisterDir + string(pLogPersistStartMsg->filePath); - } else { - cout << "FileName is not valid!" << endl; - return; - } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 57777dc..15d7e3e 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "hilog/log.h" #include "hilog_common.h" @@ -49,6 +50,14 @@ void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t m msgHeader->msgLen = msgLen; } +inline bool IsValidFileName(const std::string& strFileName) +{ + // 文件名中不能包含\/:*?"<>|这些字符 + std::regex regExpress("[\\/:*?\"<>|]"); + bool bValid = !std::regex_search(strFileName, regExpress); + return bValid; +} + void Split(const std::string& src, const std::string& separator, std::vector& dest) { string str = src; @@ -377,10 +386,16 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi logPersistParam->fileSizeStr); pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); + if (IsValidFileName(logPersistParam->fileNameStr) == false) { + cout << "FileName is not valid!" << endl; + return RET_FAIL; + } if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { return RET_FAIL; } - ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str()); + if(logPersistParam->fileNameStr != " ") { + ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str()); + } SetMsgHead(&pLogPersistStartReq->msgHeader, msgCmd, sizeof(LogPersistStartRequest)); controller.WriteAll(msgToSend, sizeof(LogPersistStartRequest)); break; -- Gitee From b98a269dd44db2cd06a57c7b9e72591f78f5867c Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 23 Jun 2021 09:51:56 +0000 Subject: [PATCH 11/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 2 +- services/hilogtool/log_display.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 539782b..2e53a49 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -124,7 +124,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade return; } string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" - :g_logPersisterDir + string(pLogPersistStartMsg->filePath); + : g_logPersisterDir + string(pLogPersistStartMsg->filePath); strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 465c885..16e7038 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -447,7 +447,7 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* if (logContent[logContent.length()-1] != '\n') { logContent += '\n'; } - std::replace(logContent.begin(), logContent.end(),'\n','\0'); + std::replace(logContent.begin(), logContent.end(), '\n', '\0'); while (logContent.find_first_of('\0') != size_t(-1)) { strncpy_s(data->data + data->tag_len, MAX_LOG_LEN, const_cast(logContent.c_str()), MAX_LOG_LEN); -- Gitee From 4ccd273f34ee4da244f8e079cf32f4dec76e4b1d Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 23 Jun 2021 10:26:10 +0000 Subject: [PATCH 12/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 4 ++-- services/hilogtool/log_controller.cpp | 4 ++-- services/hilogtool/log_display.cpp | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 2e53a49..fe464d6 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -123,8 +123,8 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } - string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" - : g_logPersisterDir + string(pLogPersistStartMsg->filePath); + string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? (g_logPersisterDir + "hilog") + : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 15d7e3e..e2be973 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -393,9 +393,9 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { return RET_FAIL; } - if(logPersistParam->fileNameStr != " ") { + if (logPersistParam->fileNameStr != " ") { ret += strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersistParam->fileNameStr.c_str()); - } + } SetMsgHead(&pLogPersistStartReq->msgHeader, msgCmd, sizeof(LogPersistStartRequest)); controller.WriteAll(msgToSend, sizeof(LogPersistStartRequest)); break; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 16e7038..78604a9 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -416,7 +416,7 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* char* tag = data->data; char* content = data->data + data->tag_len; char *pStrtol = nullptr; - int logContentPos = 0; + if (context->headLines) { if (printHeadCnt++ >= context->headLines) { exit(1); @@ -448,6 +448,7 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* logContent += '\n'; } std::replace(logContent.begin(), logContent.end(), '\n', '\0'); + int logContentPos = 0; while (logContent.find_first_of('\0') != size_t(-1)) { strncpy_s(data->data + data->tag_len, MAX_LOG_LEN, const_cast(logContent.c_str()), MAX_LOG_LEN); -- Gitee From 3df5d44a6baf2bc08ab58043c0de8849ec10305a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 23 Jun 2021 14:34:01 +0000 Subject: [PATCH 13/40] Revert "Merge branch 'master' of https://gitee.com/aajwy/hiviewdfx_hilog" This reverts commit 8dd0eb596e4af1d41ab2eddfe46a5dbee03c2826, reversing changes made to 1c060299f09a7d86cc401db8ceda185f937cd8ff. --- adapter/properties.cpp | 35 +++++++++++++++++---------- adapter/properties.h | 1 - services/hilogd/log_buffer.cpp | 2 +- services/hilogd/log_querier.cpp | 2 +- services/hilogtool/log_controller.cpp | 3 +-- services/hilogtool/main.cpp | 2 -- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index aa407d7..39dab72 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -69,17 +69,26 @@ using ProcessInfo = struct { uint32_t processQuota; }; +static int PropLock(pthread_mutex_t *propLock) +{ + return pthread_mutex_trylock(propLock); +} + +static void PropUnlock(pthread_mutex_t *propLock) +{ + pthread_mutex_unlock(propLock); +} + void PropertyGet(const std::string &key, char *value, int len) { if (len < HILOG_PROP_VALUE_MAX) { return; } - /* use OHOS interface */ } void PropertySet(const std::string &key, const char* value) { - /* use OHOS interface */ +/* use OHOS interface */ } std::string GetProgName() @@ -179,13 +188,13 @@ static void UnlockByProp(uint32_t propType) static void RefreshCacheBuf(PropertyCache *cache, const char *key) { - /* use OHOS interface */ +/* use OHOS interface */ } static bool CheckCache(const PropertyCache *cache) { return true; - /* use OHOS interface */ +/* use OHOS interface */ } static bool GetSwitchCache(bool isFirst, SwitchCache& switchCache, uint32_t propType, bool defaultValue) @@ -276,14 +285,14 @@ uint16_t GetGlobalLevel() int notLocked; if (!isFirstFlag.test_and_set() || CheckCache(&levelCache.cache)) { - notLocked = LockByProp(PROP_GLOBAL_LOG_LEVEL); + notLocked = PropLock(&g_globalLevelLock); if (!notLocked) { RefreshCacheBuf(&levelCache.cache, key.c_str()); if (sscanf_s(levelCache.cache.propertyValue, "%d", &levelCache.logLevel) <= 0) { - UnlockByProp(PROP_GLOBAL_LOG_LEVEL); + PropUnlock(&g_globalLevelLock); return HILOG_LEVEL_MIN; } - UnlockByProp(PROP_GLOBAL_LOG_LEVEL); + PropUnlock(&g_globalLevelLock); return levelCache.logLevel; } else { LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, HILOG_LEVEL_MIN}; @@ -318,14 +327,14 @@ uint16_t GetDomainLevel(uint32_t domain) return levelCache->logLevel; } else { // exist domain if (CheckCache(&it->second->cache)) { // change - notLocked = LockByProp(PROP_DOMAIN_LOG_LEVEL); + notLocked = PropLock(&g_domainLevelLock); if (!notLocked) { RefreshCacheBuf(&it->second->cache, key.c_str()); if (sscanf_s(it->second->cache.propertyValue, "%d", &it->second->logLevel) <= 0) { - UnlockByProp(PROP_DOMAIN_LOG_LEVEL); + PropUnlock(&g_domainLevelLock); return HILOG_LEVEL_MIN; } - UnlockByProp(PROP_DOMAIN_LOG_LEVEL); + PropUnlock(&g_domainLevelLock); return it->second->logLevel; } else { LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, HILOG_LEVEL_MIN}; @@ -362,14 +371,14 @@ uint16_t GetTagLevel(const std::string& tag) return levelCache->logLevel; } else { if (CheckCache(&it->second->cache)) { - notLocked = LockByProp(PROP_TAG_LOG_LEVEL); + notLocked = PropLock(&g_tagLevelLock); if (!notLocked) { RefreshCacheBuf(&it->second->cache, key.c_str()); if (sscanf_s(it->second->cache.propertyValue, "%d", &it->second->logLevel) <= 0) { - UnlockByProp(PROP_TAG_LOG_LEVEL); + PropUnlock(&g_tagLevelLock); return HILOG_LEVEL_MIN; } - UnlockByProp(PROP_TAG_LOG_LEVEL); + PropUnlock(&g_tagLevelLock); return it->second->logLevel; } else { LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, HILOG_LEVEL_MIN}; diff --git a/adapter/properties.h b/adapter/properties.h index 42d1f7b..cebbaa1 100644 --- a/adapter/properties.h +++ b/adapter/properties.h @@ -20,7 +20,6 @@ #include #include - static const int HILOG_PROP_VALUE_MAX = 92; using PropType = enum { diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 70c694f..7fd5327 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -236,7 +236,7 @@ bool HilogBuffer::Query(LogReader* reader) size_t HilogBuffer::GetBuffLen(uint16_t logType) { if (logType >= LOG_TYPE_MAX) { - return -1; + return 0; } uint64_t buffSize = g_maxBufferSizeByType[logType]; return buffSize; diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index fe464d6..986dede 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -285,7 +285,7 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, uint32_t msgNum = 0; uint16_t msgLen = pBufferSizeReq->msgHeader.msgLen; uint16_t sendMsgLen = 0; - int64_t buffLen; + uint64_t buffLen; if (msgLen > sizeof(BuffSizeMsg) * LOG_TYPE_MAX) { return; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index e2be973..f5352c2 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -38,7 +38,6 @@ 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; void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { @@ -376,7 +375,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; } - pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID + pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? time(nullptr) : stoi(logPersistParam->jobIdStr); pLogPersistStartMsg->compressType = (logPersistParam->compressTypeStr == "") ? STREAM : stoi(logPersistParam ->compressTypeStr); diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 4d5bf13..ced850d 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -103,8 +103,6 @@ static void Helper() " nsec display time by nano sec.\n" " year display the year.\n" " zone display the time zone.\n" - " -b , --baselevel=\n" - " set loggable level.\n" ); } -- Gitee From deec5dc7004fdfede50d8fcce4d57ca3b1df7616 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 24 Jun 2021 02:10:00 +0000 Subject: [PATCH 14/40] revert valid filename Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 20 +- services/hilogtool/log_controller.cpp | 4 - services/hilogtool/log_display.cpp | 468 -------------------------- 3 files changed, 16 insertions(+), 476 deletions(-) delete mode 100644 services/hilogtool/log_display.cpp diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 986dede..50c7039 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include "compressing_rotator.h" @@ -53,7 +54,13 @@ inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen msgHeader->msgType = msgCmd; msgHeader->msgLen = msgLen; } - +inline bool IsValidFileName(const std::string& strFileName) +{ + // 文件名中不能包含\/:*?"<>|这些字符 + std::regex regExpress("[\\/:*?\"<>|]"); + bool bValid = !std::regex_search(strFileName, regExpress); + return bValid; +} LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; @@ -123,8 +130,13 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } - string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? (g_logPersisterDir + "hilog") - : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); + if(IsValidFileName(string(pLogPersistStartMsg->filePath) == true) { + string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" + :g_logPersisterDir + string(pLogPersistStartMsg->filePath); + } else { + cout << "FileName is not valid!" << endl; + return; + } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( @@ -285,7 +297,7 @@ void HandleBufferSizeRequest(char* reqMsg, std::shared_ptr logReader, uint32_t msgNum = 0; uint16_t msgLen = pBufferSizeReq->msgHeader.msgLen; uint16_t sendMsgLen = 0; - uint64_t buffLen; + int64_t buffLen; if (msgLen > sizeof(BuffSizeMsg) * LOG_TYPE_MAX) { return; diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index f5352c2..da68725 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -385,10 +385,6 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi logPersistParam->fileSizeStr); pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault : stoi(logPersistParam->fileNumStr); - if (IsValidFileName(logPersistParam->fileNameStr) == false) { - cout << "FileName is not valid!" << endl; - return RET_FAIL; - } if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { return RET_FAIL; } diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp deleted file mode 100644 index 78604a9..0000000 --- a/services/hilogtool/log_display.cpp +++ /dev/null @@ -1,468 +0,0 @@ -/* - * Copyright (c) 2021 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 -#include -#include -#include -#include -#include -#include - -#include "hilog/log.h" -#include "format.h" -#include "log_controller.h" -#include "log_display.h" - -namespace OHOS { -namespace HiviewDFX { -using namespace std; - -using hash_t = std::uint64_t; - -constexpr hash_t PRIME = 0x100000001B3ull; -constexpr hash_t BASIS = 0xCBF29CE484222325ull; - -hash_t Hash(char const *str) -{ - hash_t ret {BASIS}; - while (*str) { - ret ^= *str; - ret *= PRIME; - str++; - } - return ret; -} -constexpr hash_t HashCompileTime(char const *str, hash_t lastValue = BASIS) -{ - return *str ? HashCompileTime(str + 1, (*str ^ lastValue) * PRIME) : lastValue; -} -constexpr unsigned long long operator "" _hash(char const *p, size_t) -{ - return HashCompileTime(p); -} - -string GetLogTypeStr(uint16_t logType) -{ - string logTypeStr = "invalid"; - if (logType == LOG_INIT) { - logTypeStr = "init"; - } - if (logType == LOG_CORE) { - logTypeStr = "core"; - } - if (logType == LOG_APP) { - logTypeStr = "app"; - } - return logTypeStr; -} - -string GetOrigType(uint16_t shiftType) -{ - string logType = ""; - if (((1 << LOG_INIT) & shiftType) != 0) { - logType += "init"; - } - if (((1 << LOG_CORE) & shiftType) != 0) { - logType += "core"; - } - if (((1 << LOG_APP) & shiftType) != 0) { - logType += "app "; - } - return logType; -} - -string GetPressTypeStr(uint16_t pressType) -{ - string pressTypeStr; - if (pressType == NONE) { - pressTypeStr = "none"; - } - if (pressType == STREAM) { - pressTypeStr = "stream"; - } - if (pressType == DIVIDED) { - pressTypeStr = "divided"; - } - return pressTypeStr; -} - -string GetPressAlgStr(uint16_t pressAlg) -{ - string pressAlgStr; - if (pressAlg == COMPRESS_TYPE_ZSTD) { - pressAlgStr = "zstd"; - } - if (pressAlg == COMPRESS_TYPE_ZLIB) { - pressAlgStr = "zlib"; - } - return pressAlgStr; -} - -string GetByteLenStr(uint64_t buffSize) -{ - string buffSizeStr; - if (buffSize < ONE_KB) { - buffSizeStr += to_string(buffSize); - buffSizeStr += "B"; - } else if (buffSize < ONE_MB) { - buffSize = buffSize / ONE_KB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "K"; - } else if (buffSize < ONE_GB) { - buffSize = buffSize / ONE_MB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "M"; - } else if (buffSize < ONE_TB) { - buffSize = buffSize / ONE_GB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "G"; - } else { - buffSize = buffSize / ONE_TB; - buffSizeStr += to_string(buffSize); - buffSizeStr += "T"; - } - return buffSizeStr; -} - -/* - * print control command operation result - */ -int32_t ControlCmdResult(const char* message) -{ - MessageHeader* msgHeader = (MessageHeader*)message; - uint8_t msgCmd = msgHeader->msgType; - uint16_t msgLen = msgHeader->msgLen; - string outputStr = ""; - uint32_t resultLen = 0; - switch (msgCmd) { - case MC_RSP_BUFFER_SIZE: { - BufferSizeResponse* pBuffSizeRsp = (BufferSizeResponse*)message; - if (!pBuffSizeRsp) { - return RET_FAIL; - } - BuffSizeResult* pBuffSizeRst = (BuffSizeResult*)&pBuffSizeRsp->buffSizeRst; - while (pBuffSizeRst && resultLen < msgLen) { - if (pBuffSizeRst->result == RET_FAIL) { - outputStr += GetLogTypeStr(pBuffSizeRst->logType); - outputStr += " buffer size fail"; - outputStr += "\n"; - } else { - outputStr += GetLogTypeStr(pBuffSizeRst->logType); - outputStr += " buffer size is "; - outputStr += GetByteLenStr(pBuffSizeRst->buffSize); - outputStr += "\n"; - } - pBuffSizeRst++; - resultLen += sizeof(BuffSizeResult); - } - break; - } - case MC_RSP_BUFFER_RESIZE: { - BufferResizeResponse* pBuffResizeRsp = (BufferResizeResponse*)message; - if (!pBuffResizeRsp) { - return RET_FAIL; - } - BuffResizeResult* pBuffResizeRst = (BuffResizeResult*)&pBuffResizeRsp->buffResizeRst; - while (pBuffResizeRst && resultLen < msgLen) { - if (pBuffResizeRst->result == RET_FAIL) { - outputStr += GetLogTypeStr(pBuffResizeRst->logType); - outputStr += " buffer resize fail"; - outputStr += "\n"; - } else { - outputStr += GetLogTypeStr(pBuffResizeRst->logType); - outputStr += " buffer size is "; - outputStr += GetByteLenStr(pBuffResizeRst->buffSize); - outputStr += "\n"; - } - pBuffResizeRst++; - resultLen += sizeof(BuffSizeResult); - } - break; - } - case MC_RSP_STATISTIC_INFO_QUERY: { - StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; - string logOrDomain; - if (!staInfoQueryRsp) { - return RET_FAIL; - } - if (staInfoQueryRsp->domain != 0xffffffff) { - logOrDomain = to_string(staInfoQueryRsp->domain); - } else { - logOrDomain = GetLogTypeStr(staInfoQueryRsp->logType); - } - if (staInfoQueryRsp->result == RET_SUCCESS) { - outputStr += logOrDomain; - outputStr += " print log length is "; - outputStr += GetByteLenStr(staInfoQueryRsp->printLen); - outputStr += "\n"; - outputStr += logOrDomain; - outputStr += " cache log length is "; - outputStr += GetByteLenStr(staInfoQueryRsp->cacheLen); - outputStr += "\n"; - outputStr += logOrDomain; - outputStr += " dropped log lines is "; - outputStr += GetByteLenStr(staInfoQueryRsp->dropped); - } else if (staInfoQueryRsp->result == RET_FAIL) { - outputStr += logOrDomain; - outputStr += " statistic info query fail "; - } - break; - } - case MC_RSP_STATISTIC_INFO_CLEAR: { - StatisticInfoClearResponse* staInfoClearRsp = (StatisticInfoClearResponse*)message; - string logOrDomain; - if (!staInfoClearRsp) { - return RET_FAIL; - } - if (staInfoClearRsp->domain != 0xffffffff) { - logOrDomain = to_string(staInfoClearRsp->domain); - } else { - logOrDomain = GetLogTypeStr(staInfoClearRsp->logType); - } - if (staInfoClearRsp->result == RET_SUCCESS) { - outputStr += logOrDomain; - outputStr += " statistic info clear success "; - } else if (staInfoClearRsp->result == RET_FAIL) { - outputStr += logOrDomain; - outputStr += " statistic info clear fail "; - } - break; - } - case MC_RSP_LOG_CLEAR: { - LogClearResponse* pLogClearRsp = (LogClearResponse*)message; - if (!pLogClearRsp) { - RET_FAIL; - } - LogClearResult* pLogClearRst = (LogClearResult*)&pLogClearRsp->logClearRst; - while (pLogClearRst && resultLen < msgLen) { - if (pLogClearRst->result == RET_FAIL) { - outputStr += GetLogTypeStr(pLogClearRst->logType); - outputStr += " log clear fail"; - outputStr += "\n"; - } else { - outputStr += GetLogTypeStr(pLogClearRst->logType); - outputStr += " log clear success "; - outputStr += "\n"; - } - pLogClearRst++; - resultLen += sizeof(LogClearResult); - } - break; - } - case MC_RSP_LOG_PERSIST_START: { - LogPersistStartResponse* pLogPersistStartRsp = (LogPersistStartResponse*)message; - if (!pLogPersistStartRsp) { - return RET_FAIL; - } - LogPersistStartResult* pLogPersistStartRst = - (LogPersistStartResult*)&pLogPersistStartRsp->logPersistStartRst; - while (pLogPersistStartRst && resultLen < msgLen) { - if (pLogPersistStartRst->result == RET_FAIL) { - outputStr += to_string(pLogPersistStartRst->jobId); - outputStr += " log file task start fail"; - outputStr += "\n"; - } else { - outputStr += to_string(pLogPersistStartRst->jobId); - outputStr += " log file task start success"; - outputStr += "\n"; - } - pLogPersistStartRst++; - resultLen += sizeof(LogPersistStartResult); - } - break; - } - case MC_RSP_LOG_PERSIST_STOP: { - LogPersistStopResponse* pLogPersistStopRsp = (LogPersistStopResponse*)message; - if (!pLogPersistStopRsp) { - return RET_FAIL; - } - LogPersistStopResult* pLogPersistStopRst = (LogPersistStopResult*)&pLogPersistStopRsp->logPersistStopRst; - while (pLogPersistStopRst && resultLen < msgLen) { - if (pLogPersistStopRst->result == RET_FAIL) { - outputStr += to_string(pLogPersistStopRst->jobId); - outputStr += " log file task stop fail"; - outputStr += "\n"; - } else { - outputStr += to_string(pLogPersistStopRst->jobId); - outputStr += " log file task stop success"; - outputStr += "\n"; - } - pLogPersistStopRst++; - resultLen += sizeof(LogPersistStopResult); - } - break; - } - case MC_RSP_LOG_PERSIST_QUERY: { - LogPersistQueryResponse* pLogPersistQueryRsp = (LogPersistQueryResponse*)message; - if (!pLogPersistQueryRsp) { - return RET_FAIL; - } - LogPersistQueryResult* pLogPersistQueryRst = - (LogPersistQueryResult*)&pLogPersistQueryRsp->logPersistQueryRst; - while (pLogPersistQueryRst && resultLen < msgLen) { - if (pLogPersistQueryRst->result == RET_FAIL) { - outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); - outputStr = " log file task query fail"; - outputStr += "\n"; - } else { - outputStr += to_string(pLogPersistQueryRst->jobId); - outputStr += " "; - outputStr += GetOrigType(pLogPersistQueryRst->logType); - outputStr += " "; - outputStr += GetPressTypeStr(pLogPersistQueryRst->compressType); - outputStr += " "; - outputStr += GetPressAlgStr(pLogPersistQueryRst->compressAlg); - outputStr += " "; - outputStr += pLogPersistQueryRst->filePath; - outputStr += " "; - outputStr += to_string(pLogPersistQueryRst->fileSize); - outputStr += " "; - outputStr += to_string(pLogPersistQueryRst->fileNum); - outputStr += "\n"; - } - pLogPersistQueryRst++; - resultLen += sizeof(LogPersistQueryResult); - } - break; - } - default: - break; - } - cout << outputStr << endl; - return 0; -} - -HilogShowFormat HilogFormat (const char* formatArg) -{ - static HilogShowFormat format; - - switch (Hash(formatArg)) { - case "color"_hash: - format = COLOR_SHOWFORMAT; - break; - case "colour"_hash: - format = COLOR_SHOWFORMAT; - break; - case "time"_hash: - format = TIME_SHOWFORMAT; - break; - case "usec"_hash: - format = TIME_USEC_SHOWFORMAT; - break; - case "nsec"_hash: - format = TIME_NSEC_SHOWFORMAT; - break; - case "year"_hash: - format = YEAR_SHOWFORMAT; - break; - case "zone"_hash: - format = ZONE_SHOWFORMAT; - break; - case "epoch"_hash: - format = EPOCH_SHOWFORMAT; - break; - case "monotonic"_hash: - format = MONOTONIC_SHOWFORMAT; - break; - default: - cout << "Format Invalid parameter"<& tailBuffer) -{ - if (data->sendId == SENDIDN) { - return; - } - if (data->length == 0) { -#ifdef DEBUG - cout << "Log content null" << endl; -#endif - return; - } - - static int printHeadCnt = 0; - HilogShowFormatBuffer showBuffer; - char* tag = data->data; - char* content = data->data + data->tag_len; - char *pStrtol = nullptr; - - if (context->headLines) { - if (printHeadCnt++ >= context->headLines) { - exit(1); - } - } - if ((context->tag != "") && strcmp(tag, context->tag.c_str())) { - return; - } - if ((context->domain != "") && (data->domain & 0xFFFFF) != - std::strtol(context->domain.c_str(), &pStrtol, DOMAIN_NUMBER_BASE)) { - return; - } - if (context->regexArgs != "") { - string str = content; - if (HilogMatchByRegex(str, context->regexArgs)) { - return; - } - } - char buffer[MAX_LOG_LEN * 2]; - showBuffer.level = data->level; - showBuffer.pid = data->pid; - showBuffer.tid = data->tid; - showBuffer.domain = data->domain; - showBuffer.tag_len = data->tag_len; - showBuffer.tv_sec = data->tv_sec; - showBuffer.tv_nsec = data->tv_nsec; - string logContent(data->data + data->tag_len); - if (logContent[logContent.length()-1] != '\n') { - logContent += '\n'; - } - std::replace(logContent.begin(), logContent.end(), '\n', '\0'); - int logContentPos = 0; - while (logContent.find_first_of('\0') != size_t(-1)) { - strncpy_s(data->data + data->tag_len, MAX_LOG_LEN, - const_cast(logContent.c_str()), MAX_LOG_LEN); - showBuffer.data = data->data; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); - if (context->tailLines) { - tailBuffer.emplace_back(buffer); - return; - } - cout << buffer << endl; - logContentPos = logContent.find_first_of('\0'); - logContent.erase(0, logContentPos + 1); - } - return; -} -} // namespace HiviewDFX -} // namespace OHOS -- Gitee From d7493f3c354feb4508fb25177bfcfb3dc7850906 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 24 Jun 2021 03:13:59 +0000 Subject: [PATCH 15/40] display by double pointer --- services/hilogd/log_querier.cpp | 11 +- services/hilogtool/log_display.cpp | 483 +++++++++++++++++++++++++++++ 2 files changed, 491 insertions(+), 3 deletions(-) create mode 100644 services/hilogtool/log_display.cpp diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 50c7039..3600b9c 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -56,7 +56,7 @@ inline void SetMsgHead(MessageHeader* msgHeader, uint8_t msgCmd, uint16_t msgLen } inline bool IsValidFileName(const std::string& strFileName) { - // 文件名中不能包含\/:*?"<>|这些字符 + // File name shouldn't contain "[\\/:*?\"<>|]" std::regex regExpress("[\\/:*?\"<>|]"); bool bValid = !std::regex_search(strFileName, regExpress); return bValid; @@ -130,11 +130,16 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } - if(IsValidFileName(string(pLogPersistStartMsg->filePath) == true) { - string logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" + string logPersisterPath; + if(IsValidFileName(string(pLogPersistStartMsg->filePath)) == true) { + logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" :g_logPersisterDir + string(pLogPersistStartMsg->filePath); } else { cout << "FileName is not valid!" << endl; + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->result = RET_FAIL; + SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); + logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); return; } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp new file mode 100644 index 0000000..fca14bb --- /dev/null +++ b/services/hilogtool/log_display.cpp @@ -0,0 +1,483 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include +#include + +#include "hilog/log.h" +#include "format.h" +#include "log_controller.h" +#include "log_display.h" + +namespace OHOS { +namespace HiviewDFX { +using namespace std; + +using hash_t = std::uint64_t; + +constexpr hash_t PRIME = 0x100000001B3ull; +constexpr hash_t BASIS = 0xCBF29CE484222325ull; + +hash_t Hash(char const *str) +{ + hash_t ret {BASIS}; + while (*str) { + ret ^= *str; + ret *= PRIME; + str++; + } + return ret; +} +constexpr hash_t HashCompileTime(char const *str, hash_t lastValue = BASIS) +{ + return *str ? HashCompileTime(str + 1, (*str ^ lastValue) * PRIME) : lastValue; +} +constexpr unsigned long long operator "" _hash(char const *p, size_t) +{ + return HashCompileTime(p); +} + +string GetLogTypeStr(uint16_t logType) +{ + string logTypeStr = "invalid"; + if (logType == LOG_INIT) { + logTypeStr = "init"; + } + if (logType == LOG_CORE) { + logTypeStr = "core"; + } + if (logType == LOG_APP) { + logTypeStr = "app"; + } + return logTypeStr; +} + +string GetOrigType(uint16_t shiftType) +{ + string logType = ""; + if (((1 << LOG_INIT) & shiftType) != 0) { + logType += "init"; + } + if (((1 << LOG_CORE) & shiftType) != 0) { + logType += "core"; + } + if (((1 << LOG_APP) & shiftType) != 0) { + logType += "app "; + } + return logType; +} + +string GetPressTypeStr(uint16_t pressType) +{ + string pressTypeStr; + if (pressType == NONE) { + pressTypeStr = "none"; + } + if (pressType == STREAM) { + pressTypeStr = "stream"; + } + if (pressType == DIVIDED) { + pressTypeStr = "divided"; + } + return pressTypeStr; +} + +string GetPressAlgStr(uint16_t pressAlg) +{ + string pressAlgStr; + if (pressAlg == COMPRESS_TYPE_ZSTD) { + pressAlgStr = "zstd"; + } + if (pressAlg == COMPRESS_TYPE_ZLIB) { + pressAlgStr = "zlib"; + } + return pressAlgStr; +} + +string GetByteLenStr(uint64_t buffSize) +{ + string buffSizeStr; + if (buffSize < ONE_KB) { + buffSizeStr += to_string(buffSize); + buffSizeStr += "B"; + } else if (buffSize < ONE_MB) { + buffSize = buffSize / ONE_KB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "K"; + } else if (buffSize < ONE_GB) { + buffSize = buffSize / ONE_MB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "M"; + } else if (buffSize < ONE_TB) { + buffSize = buffSize / ONE_GB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "G"; + } else { + buffSize = buffSize / ONE_TB; + buffSizeStr += to_string(buffSize); + buffSizeStr += "T"; + } + return buffSizeStr; +} + +/* + * print control command operation result + */ +int32_t ControlCmdResult(const char* message) +{ + MessageHeader* msgHeader = (MessageHeader*)message; + uint8_t msgCmd = msgHeader->msgType; + uint16_t msgLen = msgHeader->msgLen; + string outputStr = ""; + uint32_t resultLen = 0; + switch (msgCmd) { + case MC_RSP_BUFFER_SIZE: { + BufferSizeResponse* pBuffSizeRsp = (BufferSizeResponse*)message; + if (!pBuffSizeRsp) { + return RET_FAIL; + } + BuffSizeResult* pBuffSizeRst = (BuffSizeResult*)&pBuffSizeRsp->buffSizeRst; + while (pBuffSizeRst && resultLen < msgLen) { + if (pBuffSizeRst->result == RET_FAIL) { + outputStr += GetLogTypeStr(pBuffSizeRst->logType); + outputStr += " buffer size fail"; + outputStr += "\n"; + } else { + outputStr += GetLogTypeStr(pBuffSizeRst->logType); + outputStr += " buffer size is "; + outputStr += GetByteLenStr(pBuffSizeRst->buffSize); + outputStr += "\n"; + } + pBuffSizeRst++; + resultLen += sizeof(BuffSizeResult); + } + break; + } + case MC_RSP_BUFFER_RESIZE: { + BufferResizeResponse* pBuffResizeRsp = (BufferResizeResponse*)message; + if (!pBuffResizeRsp) { + return RET_FAIL; + } + BuffResizeResult* pBuffResizeRst = (BuffResizeResult*)&pBuffResizeRsp->buffResizeRst; + while (pBuffResizeRst && resultLen < msgLen) { + if (pBuffResizeRst->result == RET_FAIL) { + outputStr += GetLogTypeStr(pBuffResizeRst->logType); + outputStr += " buffer resize fail"; + outputStr += "\n"; + } else { + outputStr += GetLogTypeStr(pBuffResizeRst->logType); + outputStr += " buffer size is "; + outputStr += GetByteLenStr(pBuffResizeRst->buffSize); + outputStr += "\n"; + } + pBuffResizeRst++; + resultLen += sizeof(BuffSizeResult); + } + break; + } + case MC_RSP_STATISTIC_INFO_QUERY: { + StatisticInfoQueryResponse* staInfoQueryRsp = (StatisticInfoQueryResponse*)message; + string logOrDomain; + if (!staInfoQueryRsp) { + return RET_FAIL; + } + if (staInfoQueryRsp->domain != 0xffffffff) { + logOrDomain = to_string(staInfoQueryRsp->domain); + } else { + logOrDomain = GetLogTypeStr(staInfoQueryRsp->logType); + } + if (staInfoQueryRsp->result == RET_SUCCESS) { + outputStr += logOrDomain; + outputStr += " print log length is "; + outputStr += GetByteLenStr(staInfoQueryRsp->printLen); + outputStr += "\n"; + outputStr += logOrDomain; + outputStr += " cache log length is "; + outputStr += GetByteLenStr(staInfoQueryRsp->cacheLen); + outputStr += "\n"; + outputStr += logOrDomain; + outputStr += " dropped log lines is "; + outputStr += GetByteLenStr(staInfoQueryRsp->dropped); + } else if (staInfoQueryRsp->result == RET_FAIL) { + outputStr += logOrDomain; + outputStr += " statistic info query fail "; + } + break; + } + case MC_RSP_STATISTIC_INFO_CLEAR: { + StatisticInfoClearResponse* staInfoClearRsp = (StatisticInfoClearResponse*)message; + string logOrDomain; + if (!staInfoClearRsp) { + return RET_FAIL; + } + if (staInfoClearRsp->domain != 0xffffffff) { + logOrDomain = to_string(staInfoClearRsp->domain); + } else { + logOrDomain = GetLogTypeStr(staInfoClearRsp->logType); + } + if (staInfoClearRsp->result == RET_SUCCESS) { + outputStr += logOrDomain; + outputStr += " statistic info clear success "; + } else if (staInfoClearRsp->result == RET_FAIL) { + outputStr += logOrDomain; + outputStr += " statistic info clear fail "; + } + break; + } + case MC_RSP_LOG_CLEAR: { + LogClearResponse* pLogClearRsp = (LogClearResponse*)message; + if (!pLogClearRsp) { + RET_FAIL; + } + LogClearResult* pLogClearRst = (LogClearResult*)&pLogClearRsp->logClearRst; + while (pLogClearRst && resultLen < msgLen) { + if (pLogClearRst->result == RET_FAIL) { + outputStr += GetLogTypeStr(pLogClearRst->logType); + outputStr += " log clear fail"; + outputStr += "\n"; + } else { + outputStr += GetLogTypeStr(pLogClearRst->logType); + outputStr += " log clear success "; + outputStr += "\n"; + } + pLogClearRst++; + resultLen += sizeof(LogClearResult); + } + break; + } + case MC_RSP_LOG_PERSIST_START: { + LogPersistStartResponse* pLogPersistStartRsp = (LogPersistStartResponse*)message; + if (!pLogPersistStartRsp) { + return RET_FAIL; + } + LogPersistStartResult* pLogPersistStartRst = + (LogPersistStartResult*)&pLogPersistStartRsp->logPersistStartRst; + while (pLogPersistStartRst && resultLen < msgLen) { + if (pLogPersistStartRst->result == RET_FAIL) { + outputStr += to_string(pLogPersistStartRst->jobId); + outputStr += " log file task start fail"; + outputStr += "\n"; + } else { + outputStr += to_string(pLogPersistStartRst->jobId); + outputStr += " log file task start success"; + outputStr += "\n"; + } + pLogPersistStartRst++; + resultLen += sizeof(LogPersistStartResult); + } + break; + } + case MC_RSP_LOG_PERSIST_STOP: { + LogPersistStopResponse* pLogPersistStopRsp = (LogPersistStopResponse*)message; + if (!pLogPersistStopRsp) { + return RET_FAIL; + } + LogPersistStopResult* pLogPersistStopRst = (LogPersistStopResult*)&pLogPersistStopRsp->logPersistStopRst; + while (pLogPersistStopRst && resultLen < msgLen) { + if (pLogPersistStopRst->result == RET_FAIL) { + outputStr += to_string(pLogPersistStopRst->jobId); + outputStr += " log file task stop fail"; + outputStr += "\n"; + } else { + outputStr += to_string(pLogPersistStopRst->jobId); + outputStr += " log file task stop success"; + outputStr += "\n"; + } + pLogPersistStopRst++; + resultLen += sizeof(LogPersistStopResult); + } + break; + } + case MC_RSP_LOG_PERSIST_QUERY: { + LogPersistQueryResponse* pLogPersistQueryRsp = (LogPersistQueryResponse*)message; + if (!pLogPersistQueryRsp) { + return RET_FAIL; + } + LogPersistQueryResult* pLogPersistQueryRst = + (LogPersistQueryResult*)&pLogPersistQueryRsp->logPersistQueryRst; + while (pLogPersistQueryRst && resultLen < msgLen) { + if (pLogPersistQueryRst->result == RET_FAIL) { + outputStr += GetLogTypeStr(pLogPersistQueryRst->logType); + outputStr = " log file task query fail"; + outputStr += "\n"; + } else { + outputStr += to_string(pLogPersistQueryRst->jobId); + outputStr += " "; + outputStr += GetOrigType(pLogPersistQueryRst->logType); + outputStr += " "; + outputStr += GetPressTypeStr(pLogPersistQueryRst->compressType); + outputStr += " "; + outputStr += GetPressAlgStr(pLogPersistQueryRst->compressAlg); + outputStr += " "; + outputStr += pLogPersistQueryRst->filePath; + outputStr += " "; + outputStr += to_string(pLogPersistQueryRst->fileSize); + outputStr += " "; + outputStr += to_string(pLogPersistQueryRst->fileNum); + outputStr += "\n"; + } + pLogPersistQueryRst++; + resultLen += sizeof(LogPersistQueryResult); + } + break; + } + default: + break; + } + cout << outputStr << endl; + return 0; +} + +HilogShowFormat HilogFormat (const char* formatArg) +{ + static HilogShowFormat format; + + switch (Hash(formatArg)) { + case "color"_hash: + format = COLOR_SHOWFORMAT; + break; + case "colour"_hash: + format = COLOR_SHOWFORMAT; + break; + case "time"_hash: + format = TIME_SHOWFORMAT; + break; + case "usec"_hash: + format = TIME_USEC_SHOWFORMAT; + break; + case "nsec"_hash: + format = TIME_NSEC_SHOWFORMAT; + break; + case "year"_hash: + format = YEAR_SHOWFORMAT; + break; + case "zone"_hash: + format = ZONE_SHOWFORMAT; + break; + case "epoch"_hash: + format = EPOCH_SHOWFORMAT; + break; + case "monotonic"_hash: + format = MONOTONIC_SHOWFORMAT; + break; + default: + cout << "Format Invalid parameter"<& tailBuffer) +{ + if (data->sendId == SENDIDN) { + return; + } + if (data->length == 0) { +#ifdef DEBUG + cout << "Log content null" << endl; +#endif + return; + } + + static int printHeadCnt = 0; + HilogShowFormatBuffer showBuffer; + char* tag = data->data; + char* content = data->data + data->tag_len; + char *pStrtol = nullptr; + + if (context->headLines) { + if (printHeadCnt++ >= context->headLines) { + exit(1); + } + } + if ((context->tag != "") && strcmp(tag, context->tag.c_str())) { + return; + } + if ((context->domain != "") && (data->domain & 0xFFFFF) != + std::strtol(context->domain.c_str(), &pStrtol, DOMAIN_NUMBER_BASE)) { + return; + } + if (context->regexArgs != "") { + string str = content; + if (HilogMatchByRegex(str, context->regexArgs)) { + return; + } + } + char buffer[MAX_LOG_LEN * 2]; + showBuffer.level = data->level; + showBuffer.pid = data->pid; + showBuffer.tid = data->tid; + showBuffer.domain = data->domain; + showBuffer.tag_len = data->tag_len; + showBuffer.tv_sec = data->tv_sec; + showBuffer.tv_nsec = data->tv_nsec; + + int offset = data->tag_len; + + char *dataBegin = data->data + offset; + char *dataPos = data->data + offset; + while(*dataPos != 0) { + if(*dataPos == '\n') { + if(dataPos != dataBegin) { + *dataPos = 0; + showBuffer.tag_len = offset; + showBuffer.data = data->data; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); + if (context->tailLines) { + tailBuffer.emplace_back(buffer); + return; + } + cout << buffer << endl; + offset += dataPos - dataBegin + 1; + } else { + offset++; + } + dataBegin = dataPos + 1; + } + dataPos++; + } + if(dataPos != dataBegin) { + showBuffer.data = data->data; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); + if (context->tailLines) { + tailBuffer.emplace_back(buffer); + return; + } + cout << buffer << endl; + } + return; +} +} // namespace HiviewDFX +} // namespace OHOS -- Gitee From 793cb259bc80098f7f9b961d25c928b29b55053b Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 24 Jun 2021 07:13:06 +0000 Subject: [PATCH 16/40] persist file changeline Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_data.h | 1 + services/hilogd/log_persister.cpp | 26 ++++++++++++++++++++++---- services/hilogtool/log_display.cpp | 8 ++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/services/hilogd/include/log_data.h b/services/hilogd/include/log_data.h index dac520b..75e13fa 100644 --- a/services/hilogd/include/log_data.h +++ b/services/hilogd/include/log_data.h @@ -52,6 +52,7 @@ struct HilogData { tag = tmp; content = tmp + mtagLen; if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) { + tag[mtagLen - 1] = '\0'; return; } if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) { diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index d15525f..74eb841 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -59,12 +59,30 @@ string GenPersistLogHeader(const HilogData *data) showBuffer.tid = data->tid; showBuffer.domain = data->domain; showBuffer.tag_len = data->tag_len; - showBuffer.data = data->content; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; - showBuffer.data = data->tag; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); - return buffer; + char *dataBegin = data->content; + char *dataPos = data->content; + while (*dataPos != 0) { + if (*dataPos == '\n') { + if (dataPos != dataBegin) { + *dataPos = 0; + showBuffer.data = data->content; + showBuffer.data = data->tag; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); + return buffer; + } + dataBegin = dataPos + 1; + } + dataPos++; + } + if (dataPos != dataBegin) { + showBuffer.data = data->content; + showBuffer.data = data->tag; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); + return buffer; + } + return NULL; } LogPersister::LogPersister(uint32_t id, string path, uint16_t compressType, diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index fca14bb..6b1b1d4 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -448,9 +448,9 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* char *dataBegin = data->data + offset; char *dataPos = data->data + offset; - while(*dataPos != 0) { - if(*dataPos == '\n') { - if(dataPos != dataBegin) { + while (*dataPos != 0) { + if (*dataPos == '\n') { + if (dataPos != dataBegin) { *dataPos = 0; showBuffer.tag_len = offset; showBuffer.data = data->data; @@ -468,7 +468,7 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* } dataPos++; } - if(dataPos != dataBegin) { + if (dataPos != dataBegin) { showBuffer.data = data->data; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); if (context->tailLines) { -- Gitee From 0bbd1a1761d9d459b40490c57f2df5d59bb971ac Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 24 Jun 2021 07:24:06 +0000 Subject: [PATCH 17/40] jobid Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index da68725..cfd8af0 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -375,7 +375,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } pLogPersistStartMsg->logType = (0b01 << tmpType) | pLogPersistStartMsg->logType; } - pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? time(nullptr) + pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID : stoi(logPersistParam->jobIdStr); pLogPersistStartMsg->compressType = (logPersistParam->compressTypeStr == "") ? STREAM : stoi(logPersistParam ->compressTypeStr); -- Gitee From d5ba81ffa68bde6ea354578c335a5ba1efdf9f34 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 24 Jun 2021 07:42:14 +0000 Subject: [PATCH 18/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_querier.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 3600b9c..97ba789 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -131,12 +131,12 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade return; } string logPersisterPath; - if(IsValidFileName(string(pLogPersistStartMsg->filePath)) == true) { - logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? g_logPersisterDir + "hilog" - :g_logPersisterDir + string(pLogPersistStartMsg->filePath); + if (IsValidFileName(string(pLogPersistStartMsg->filePath)) == true) { + logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? (g_logPersisterDir + "hilog") + : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); } else { cout << "FileName is not valid!" << endl; - pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; + pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = RET_FAIL; SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); -- Gitee From f5ce77ceef061e1a8913cdc69bc139e21c0ed258 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 24 Jun 2021 07:50:29 +0000 Subject: [PATCH 19/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index cfd8af0..3d799f8 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -38,7 +38,7 @@ 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; void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t msgLen) { if (!msgHeader) { -- Gitee From 8360410e243c3dfc74ede9f23bcef305651c3cd5 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 25 Jun 2021 06:33:22 +0000 Subject: [PATCH 20/40] persisit Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 4 +- services/hilogd/log_persister.cpp | 112 +++++++++++++----------- services/hilogtool/log_display.cpp | 7 +- 3 files changed, 68 insertions(+), 55 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 1eb4de7..f96b8ea 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -28,6 +28,7 @@ #include "log_compress.h" namespace OHOS { namespace HiviewDFX { +using namespace std; typedef struct { uint16_t offset; char content[]; @@ -77,11 +78,10 @@ private: bool isExited(); FILE *fdinfo; int fd = -1; - LogCompress *LogCompress; }; -std::string GenPersistLogHeader(const HilogData *data); +int GenPersistLogHeader(HilogData *data, list& persistList); } // namespace HiviewDFX } // namespace OHOS #endif diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 74eb841..16696ae 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -50,41 +50,6 @@ static std::mutex g_listMutex; (x) = nullptr; \ } while (0) -string GenPersistLogHeader(const HilogData *data) -{ - char buffer[MAX_LOG_LEN * 2]; - HilogShowFormatBuffer showBuffer; - showBuffer.level = data->level; - showBuffer.pid = data->pid; - showBuffer.tid = data->tid; - showBuffer.domain = data->domain; - showBuffer.tag_len = data->tag_len; - showBuffer.tv_sec = data->tv_sec; - showBuffer.tv_nsec = data->tv_nsec; - char *dataBegin = data->content; - char *dataPos = data->content; - while (*dataPos != 0) { - if (*dataPos == '\n') { - if (dataPos != dataBegin) { - *dataPos = 0; - showBuffer.data = data->content; - showBuffer.data = data->tag; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); - return buffer; - } - dataBegin = dataPos + 1; - } - dataPos++; - } - if (dataPos != dataBegin) { - showBuffer.data = data->content; - showBuffer.data = data->tag; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); - return buffer; - } - return NULL; -} - LogPersister::LogPersister(uint32_t id, string path, uint16_t compressType, uint16_t compressAlg, int sleepTime, LogPersisterRotator *rotator, HilogBuffer *_buffer) @@ -208,25 +173,69 @@ void LogPersister::SetBufferOffset(int off) fprintf(fdinfo, "%04x\n", off); } -bool LogPersister::writeUnCompressedBuffer(HilogData *data) +int GenPersistLogHeader(HilogData *data, list& persistList) { - string header = GenPersistLogHeader(data); - uint16_t headerLen = header.length(); - uint16_t size = headerLen + 1; - uint16_t orig_offset = buffer->offset; - int r = 0; - if (buffer->offset + size > MAX_PERSISTER_BUFFER_SIZE) - return false; + char buffer[MAX_LOG_LEN]; + HilogShowFormatBuffer showBuffer; + showBuffer.level = data->level; + showBuffer.pid = data->pid; + showBuffer.tid = data->tid; + showBuffer.domain = data->domain; + showBuffer.tv_sec = data->tv_sec; + showBuffer.tv_nsec = data->tv_nsec; + int offset = data->tag_len; + char *dataBegin = data->content; + char *dataPos = data->content; + while (*dataPos != 0) { + if (*dataPos == '\n') { + if (dataPos != dataBegin) { + *dataPos = 0; + showBuffer.tag_len = offset; + showBuffer.data = dataBegin; + showBuffer.data = data->tag; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); + persistList.push_back(buffer); + offset += dataPos-dataBegin+1; + } else { + offset++; + } + dataBegin = dataPos + 1; + } + dataPos++; + } + if (dataPos != dataBegin) { + showBuffer.tag_len = data->tag_len; + showBuffer.data = data->content; + showBuffer.data = data->tag; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); + persistList.push_back(buffer); + } + return persistList.size(); +} - r = memcpy_s(buffer->content + buffer->offset, MAX_PERSISTER_BUFFER_SIZE - buffer->offset, - header.c_str(), headerLen); - if (r != 0) { - SetBufferOffset(orig_offset); - return true; +bool LogPersister::writeUnCompressedBuffer(HilogData *data) +{ + list persistList; + int listSize = GenPersistLogHeader(data, persistList); + while(listSize--) { + string header = persistList.front(); + persistList.pop_front(); + uint16_t headerLen = header.length(); + uint16_t size = headerLen + 1; + uint16_t orig_offset = buffer->offset; + int r = 0; + if (buffer->offset + size > MAX_PERSISTER_BUFFER_SIZE) + return false; + r = memcpy_s(buffer->content + buffer->offset, MAX_PERSISTER_BUFFER_SIZE - buffer->offset, + header.c_str(), headerLen); + if (r != 0) { + SetBufferOffset(orig_offset); + return true; + } + SetBufferOffset(buffer->offset + headerLen); + buffer->content[buffer->offset] = '\n'; + SetBufferOffset(buffer->offset + 1); } - SetBufferOffset(buffer->offset + headerLen); - buffer->content[buffer->offset] = '\n'; - SetBufferOffset(buffer->offset + 1); return true; } @@ -242,6 +251,7 @@ int LogPersister::WriteData(HilogData *data) break; case COMPRESS_TYPE_ZLIB: { LogCompress = new ZlibCompress(); + cout << buffer->content << endl; LogCompress->Compress((Bytef *)buffer->content, buffer->offset); rotator->Input((char *)LogCompress->zdata, LogCompress->zdlen); rotator->FinishInput(); diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 6b1b1d4..66bc09a 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -457,9 +457,11 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); if (context->tailLines) { tailBuffer.emplace_back(buffer); + //清空否 return; + } else { + cout << buffer << endl; } - cout << buffer << endl; offset += dataPos - dataBegin + 1; } else { offset++; @@ -474,8 +476,9 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* if (context->tailLines) { tailBuffer.emplace_back(buffer); return; + } else { + cout << buffer << endl; } - cout << buffer << endl; } return; } -- Gitee From ab33b788fb649ede1e3132f0dd120c06868c07a1 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 25 Jun 2021 06:35:08 +0000 Subject: [PATCH 21/40] persisit Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_display.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 66bc09a..8353dda 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -457,7 +457,6 @@ void HilogShowLog(HilogShowFormat showFormat, HilogDataMessage* data, HilogArgs* HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); if (context->tailLines) { tailBuffer.emplace_back(buffer); - //清空否 return; } else { cout << buffer << endl; -- Gitee From 1f5e59d1bd2bbdf6f428bd1e402747803c796502 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 25 Jun 2021 07:10:13 +0000 Subject: [PATCH 22/40] persisit Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 16696ae..26bacfc 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -195,7 +195,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.data = data->tag; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); - offset += dataPos-dataBegin+1; + offset += dataPos - dataBegin + 1; } else { offset++; } @@ -217,7 +217,7 @@ bool LogPersister::writeUnCompressedBuffer(HilogData *data) { list persistList; int listSize = GenPersistLogHeader(data, persistList); - while(listSize--) { + while (listSize--) { string header = persistList.front(); persistList.pop_front(); uint16_t headerLen = header.length(); @@ -251,7 +251,9 @@ int LogPersister::WriteData(HilogData *data) break; case COMPRESS_TYPE_ZLIB: { LogCompress = new ZlibCompress(); +#ifdef DEBUG cout << buffer->content << endl; +#endif LogCompress->Compress((Bytef *)buffer->content, buffer->offset); rotator->Input((char *)LogCompress->zdata, LogCompress->zdlen); rotator->FinishInput(); -- Gitee From f2962878b8593647ccbd1ab2a759df4c00b82c99 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 25 Jun 2021 08:53:23 +0000 Subject: [PATCH 23/40] delete unused code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_data.h | 1 - services/hilogtool/log_controller.cpp | 8 -------- 2 files changed, 9 deletions(-) diff --git a/services/hilogd/include/log_data.h b/services/hilogd/include/log_data.h index 75e13fa..dac520b 100644 --- a/services/hilogd/include/log_data.h +++ b/services/hilogd/include/log_data.h @@ -52,7 +52,6 @@ struct HilogData { tag = tmp; content = tmp + mtagLen; if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) { - tag[mtagLen - 1] = '\0'; return; } if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) { diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 3d799f8..b148692 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -49,14 +49,6 @@ void SetMsgHead(MessageHeader* msgHeader, const uint8_t msgCmd, const uint16_t m msgHeader->msgLen = msgLen; } -inline bool IsValidFileName(const std::string& strFileName) -{ - // 文件名中不能包含\/:*?"<>|这些字符 - std::regex regExpress("[\\/:*?\"<>|]"); - bool bValid = !std::regex_search(strFileName, regExpress); - return bValid; -} - void Split(const std::string& src, const std::string& separator, std::vector& dest) { string str = src; -- Gitee From 2eb3e635eeb9c41e665f1d1b2ae44a12e6e7ff8f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 25 Jun 2021 09:12:09 +0000 Subject: [PATCH 24/40] delete unused code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogtool/log_controller.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index b148692..09691bd 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include "hilog/log.h" #include "hilog_common.h" -- Gitee From 8e9800419a1c73b384d8dc72fb6f352801733df6 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 25 Jun 2021 12:41:39 +0000 Subject: [PATCH 25/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 26bacfc..76bf1eb 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -175,7 +175,7 @@ void LogPersister::SetBufferOffset(int off) int GenPersistLogHeader(HilogData *data, list& persistList) { - char buffer[MAX_LOG_LEN]; + char buffer[MAX_LOG_LEN*2]; HilogShowFormatBuffer showBuffer; showBuffer.level = data->level; showBuffer.pid = data->pid; @@ -183,6 +183,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.domain = data->domain; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; + showBuffer.data = data->tag; int offset = data->tag_len; char *dataBegin = data->content; char *dataPos = data->content; @@ -191,22 +192,18 @@ int GenPersistLogHeader(HilogData *data, list& persistList) if (dataPos != dataBegin) { *dataPos = 0; showBuffer.tag_len = offset; - showBuffer.data = dataBegin; - showBuffer.data = data->tag; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); offset += dataPos - dataBegin + 1; - } else { - offset++; - } + } else { + offset++; + } dataBegin = dataPos + 1; } dataPos++; } if (dataPos != dataBegin) { showBuffer.tag_len = data->tag_len; - showBuffer.data = data->content; - showBuffer.data = data->tag; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); } -- Gitee From 88e06c07d60ba02780548f8acd960f1e2b9cd9e2 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Sat, 26 Jun 2021 01:43:21 +0000 Subject: [PATCH 26/40] sync code Signed-off-by: aajwy <13051180828@163.com> --- adapter/properties.cpp | 37 +++++++++++++--------------------- services/hilogd/BUILD.gn | 8 ++++---- services/hilogd/log_buffer.cpp | 2 +- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 39dab72..1e6e91b 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -69,26 +69,17 @@ using ProcessInfo = struct { uint32_t processQuota; }; -static int PropLock(pthread_mutex_t *propLock) -{ - return pthread_mutex_trylock(propLock); -} - -static void PropUnlock(pthread_mutex_t *propLock) -{ - pthread_mutex_unlock(propLock); -} - void PropertyGet(const std::string &key, char *value, int len) { if (len < HILOG_PROP_VALUE_MAX) { return; } + /* use OHOS interface */ } void PropertySet(const std::string &key, const char* value) { -/* use OHOS interface */ + /* use OHOS interface */ } std::string GetProgName() @@ -188,13 +179,13 @@ static void UnlockByProp(uint32_t propType) static void RefreshCacheBuf(PropertyCache *cache, const char *key) { -/* use OHOS interface */ + /* use OHOS interface */ } static bool CheckCache(const PropertyCache *cache) { return true; -/* use OHOS interface */ + /* use OHOS interface */ } static bool GetSwitchCache(bool isFirst, SwitchCache& switchCache, uint32_t propType, bool defaultValue) @@ -285,14 +276,14 @@ uint16_t GetGlobalLevel() int notLocked; if (!isFirstFlag.test_and_set() || CheckCache(&levelCache.cache)) { - notLocked = PropLock(&g_globalLevelLock); + notLocked = LockByProp(PROP_GLOBAL_LOG_LEVEL); if (!notLocked) { RefreshCacheBuf(&levelCache.cache, key.c_str()); if (sscanf_s(levelCache.cache.propertyValue, "%d", &levelCache.logLevel) <= 0) { - PropUnlock(&g_globalLevelLock); + UnlockByProp(PROP_GLOBAL_LOG_LEVEL); return HILOG_LEVEL_MIN; } - PropUnlock(&g_globalLevelLock); + UnlockByProp(PROP_GLOBAL_LOG_LEVEL); return levelCache.logLevel; } else { LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, HILOG_LEVEL_MIN}; @@ -327,14 +318,14 @@ uint16_t GetDomainLevel(uint32_t domain) return levelCache->logLevel; } else { // exist domain if (CheckCache(&it->second->cache)) { // change - notLocked = PropLock(&g_domainLevelLock); + notLocked = LockByProp(PROP_DOMAIN_LOG_LEVEL); if (!notLocked) { RefreshCacheBuf(&it->second->cache, key.c_str()); if (sscanf_s(it->second->cache.propertyValue, "%d", &it->second->logLevel) <= 0) { - PropUnlock(&g_domainLevelLock); + UnlockByProp(PROP_DOMAIN_LOG_LEVEL); return HILOG_LEVEL_MIN; } - PropUnlock(&g_domainLevelLock); + UnlockByProp(PROP_DOMAIN_LOG_LEVEL); return it->second->logLevel; } else { LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, HILOG_LEVEL_MIN}; @@ -371,14 +362,14 @@ uint16_t GetTagLevel(const std::string& tag) return levelCache->logLevel; } else { if (CheckCache(&it->second->cache)) { - notLocked = PropLock(&g_tagLevelLock); + notLocked = LockByProp(PROP_TAG_LOG_LEVEL); if (!notLocked) { RefreshCacheBuf(&it->second->cache, key.c_str()); if (sscanf_s(it->second->cache.propertyValue, "%d", &it->second->logLevel) <= 0) { - PropUnlock(&g_tagLevelLock); + UnlockByProp(PROP_TAG_LOG_LEVEL); return HILOG_LEVEL_MIN; } - PropUnlock(&g_tagLevelLock); + UnlockByProp(PROP_TAG_LOG_LEVEL); return it->second->logLevel; } else { LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, HILOG_LEVEL_MIN}; @@ -392,4 +383,4 @@ uint16_t GetTagLevel(const std::string& tag) return it->second->logLevel; } } -} +} \ No newline at end of file diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index d12c70a..b035651 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,9 +33,9 @@ ohos_executable("hilogd") { "log_reader.cpp", "main.cpp", ] - + configs = [ ":hilogd_config" ] - + defines = [ "DEBUG" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", @@ -65,9 +65,9 @@ ohos_executable("hilogd_native") { "log_reader.cpp", "main.cpp", ] - + configs = [ ":hilogd_config" ] - + defines = [ "DEBUG" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter_native", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 7fd5327..70c694f 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -236,7 +236,7 @@ bool HilogBuffer::Query(LogReader* reader) size_t HilogBuffer::GetBuffLen(uint16_t logType) { if (logType >= LOG_TYPE_MAX) { - return 0; + return -1; } uint64_t buffSize = g_maxBufferSizeByType[logType]; return buffSize; -- Gitee From 680a4ce0a6903ce950e2e0779e9cb871f8c13d5a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Sat, 26 Jun 2021 01:46:00 +0000 Subject: [PATCH 27/40] sync code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index b035651..1f75183 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -35,7 +35,7 @@ ohos_executable("hilogd") { ] configs = [ ":hilogd_config" ] - defines = [ "DEBUG" ] + deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", @@ -67,7 +67,7 @@ ohos_executable("hilogd_native") { ] configs = [ ":hilogd_config" ] - defines = [ "DEBUG" ] + deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter_native", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", -- Gitee From 5932cf1cc0ee7ffa449b1bb3a2f90a3690115cc4 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Sat, 26 Jun 2021 01:47:50 +0000 Subject: [PATCH 28/40] sync code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/BUILD.gn | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index 1f75183..a0e4359 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,7 +33,7 @@ ohos_executable("hilogd") { "log_reader.cpp", "main.cpp", ] - + configs = [ ":hilogd_config" ] deps = [ @@ -65,7 +65,7 @@ ohos_executable("hilogd_native") { "log_reader.cpp", "main.cpp", ] - + configs = [ ":hilogd_config" ] deps = [ @@ -82,4 +82,4 @@ ohos_executable("hilogd_native") { install_enable = true part_name = "hiviewdfx_hilog_native" subsystem_name = "hiviewdfx" -} +} \ No newline at end of file -- Gitee From 17295a77ad18d8ed773c643a407d57d2c907ac77 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Sat, 26 Jun 2021 13:24:11 +0000 Subject: [PATCH 29/40] add datadeepcopy Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 76bf1eb..ea632fa 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -177,16 +177,18 @@ int GenPersistLogHeader(HilogData *data, list& persistList) { char buffer[MAX_LOG_LEN*2]; HilogShowFormatBuffer showBuffer; - showBuffer.level = data->level; - showBuffer.pid = data->pid; - showBuffer.tid = data->tid; - showBuffer.domain = data->domain; - showBuffer.tv_sec = data->tv_sec; - showBuffer.tv_nsec = data->tv_nsec; - showBuffer.data = data->tag; - int offset = data->tag_len; - char *dataBegin = data->content; - char *dataPos = data->content; + HilogData* dataDeepCopy = new HilogData[MAX_LOG_LEN*2]; + memcpy_s(dataDeepCopy, MAX_LOG_LEN*2, data, MAX_LOG_LEN*2); + showBuffer.level = dataDeepCopy->level; + showBuffer.pid = dataDeepCopy->pid; + showBuffer.tid = dataDeepCopy->tid; + showBuffer.domain = dataDeepCopy->domain; + showBuffer.tv_sec = dataDeepCopy->tv_sec; + showBuffer.tv_nsec = dataDeepCopy->tv_nsec; + showBuffer.data = dataDeepCopy->tag; + int offset = dataDeepCopy->tag_len; + char *dataBegin = dataDeepCopy->content; + char *dataPos = dataDeepCopy->content; while (*dataPos != 0) { if (*dataPos == '\n') { if (dataPos != dataBegin) { @@ -203,7 +205,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) dataPos++; } if (dataPos != dataBegin) { - showBuffer.tag_len = data->tag_len; + showBuffer.tag_len = dataDeepCopy->tag_len; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); } -- Gitee From 2e7d9e0d47ba8d6c1af081113d85b0b7b4c718c2 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Sun, 27 Jun 2021 07:54:02 +0000 Subject: [PATCH 30/40] solved code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_persister.h | 1 + services/hilogd/log_persister.cpp | 16 ++++++++++------ services/hilogd/log_querier.cpp | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index f96b8ea..a08992f 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -79,6 +79,7 @@ private: FILE *fdinfo; int fd = -1; LogCompress *LogCompress; + list persistList; }; int GenPersistLogHeader(HilogData *data, list& persistList); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index ea632fa..d9317ad 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -175,10 +175,10 @@ void LogPersister::SetBufferOffset(int off) int GenPersistLogHeader(HilogData *data, list& persistList) { - char buffer[MAX_LOG_LEN*2]; + char buffer[MAX_LOG_LEN * 2]; HilogShowFormatBuffer showBuffer; - HilogData* dataDeepCopy = new HilogData[MAX_LOG_LEN*2]; - memcpy_s(dataDeepCopy, MAX_LOG_LEN*2, data, MAX_LOG_LEN*2); + HilogData* dataDeepCopy = new HilogData[MAX_LOG_LEN * 2]; + memcpy_s(dataDeepCopy, MAX_LOG_LEN*2, data, MAX_LOG_LEN * 2); showBuffer.level = dataDeepCopy->level; showBuffer.pid = dataDeepCopy->pid; showBuffer.tid = dataDeepCopy->tid; @@ -214,23 +214,27 @@ int GenPersistLogHeader(HilogData *data, list& persistList) bool LogPersister::writeUnCompressedBuffer(HilogData *data) { - list persistList; - int listSize = GenPersistLogHeader(data, persistList); + int listSize = persistList.size(); + + if (persistList.empty()) { + listSize = GenPersistLogHeader(data, persistList); + } while (listSize--) { string header = persistList.front(); - persistList.pop_front(); uint16_t headerLen = header.length(); uint16_t size = headerLen + 1; uint16_t orig_offset = buffer->offset; int r = 0; if (buffer->offset + size > MAX_PERSISTER_BUFFER_SIZE) return false; + r = memcpy_s(buffer->content + buffer->offset, MAX_PERSISTER_BUFFER_SIZE - buffer->offset, header.c_str(), headerLen); if (r != 0) { SetBufferOffset(orig_offset); return true; } + persistList.pop_front(); SetBufferOffset(buffer->offset + headerLen); buffer->content[buffer->offset] = '\n'; SetBufferOffset(buffer->offset + 1); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 97ba789..f88f7ea 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -132,7 +132,7 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade } string logPersisterPath; if (IsValidFileName(string(pLogPersistStartMsg->filePath)) == true) { - logPersisterPath = strlen(pLogPersistStartMsg->filePath) == 0 ? (g_logPersisterDir + "hilog") + logPersisterPath = (strlen(pLogPersistStartMsg->filePath) == 0) ? (g_logPersisterDir + "hilog") : (g_logPersisterDir + string(pLogPersistStartMsg->filePath)); } else { cout << "FileName is not valid!" << endl; -- Gitee From 417aeffa640ed5ef9b930e6111307e21a46c70d3 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 28 Jun 2021 12:33:41 +0000 Subject: [PATCH 31/40] add datacopy Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 36 ++++++++++++++++--------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index d9317ad..8d2c513 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -173,22 +173,24 @@ void LogPersister::SetBufferOffset(int off) fprintf(fdinfo, "%04x\n", off); } -int GenPersistLogHeader(HilogData *data, list& persistList) +int GenPersistLogHeader(HilogData *data, list&persistList) { - char buffer[MAX_LOG_LEN * 2]; - HilogShowFormatBuffer showBuffer; - HilogData* dataDeepCopy = new HilogData[MAX_LOG_LEN * 2]; - memcpy_s(dataDeepCopy, MAX_LOG_LEN*2, data, MAX_LOG_LEN * 2); - showBuffer.level = dataDeepCopy->level; - showBuffer.pid = dataDeepCopy->pid; - showBuffer.tid = dataDeepCopy->tid; - showBuffer.domain = dataDeepCopy->domain; - showBuffer.tv_sec = dataDeepCopy->tv_sec; - showBuffer.tv_nsec = dataDeepCopy->tv_nsec; - showBuffer.data = dataDeepCopy->tag; - int offset = dataDeepCopy->tag_len; - char *dataBegin = dataDeepCopy->content; - char *dataPos = dataDeepCopy->content; + char buffer[MAX_LOG_SIZE]; + hilogShowFormatBuffer showBuffer; + showBuffer.level = data->level; + showBuffer.pid = data->pid; + showBuffer.tid = data->tid; + showBuffer.domain = data->domain; + showBuffer.tv_sec = data->tv_sec; + showBuffer.tv_nsec = data->tv_nsec; + showBuffer.data = data->tag; + int offset = data->tag_len; + + static char *dataCopy; + memcpy_s(&dataCopy, data->len, &data->content, data->len); + char *dataBegin = dataCopy; + char *dataPos = dataCopy; + while (*dataPos != 0) { if (*dataPos == '\n') { if (dataPos != dataBegin) { @@ -198,14 +200,14 @@ int GenPersistLogHeader(HilogData *data, list& persistList) persistList.push_back(buffer); offset += dataPos - dataBegin + 1; } else { - offset++; + offset++; } dataBegin = dataPos + 1; } dataPos++; } if (dataPos != dataBegin) { - showBuffer.tag_len = dataDeepCopy->tag_len; + showBuffer.tag_len = offset; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); } -- Gitee From 17c3ff1337266692418dd381d260a0bcb09d8739 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Mon, 28 Jun 2021 12:35:04 +0000 Subject: [PATCH 32/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 8d2c513..ea9606d 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -173,7 +173,7 @@ void LogPersister::SetBufferOffset(int off) fprintf(fdinfo, "%04x\n", off); } -int GenPersistLogHeader(HilogData *data, list&persistList) +int GenPersistLogHeader(HilogData *data, list& persistList) { char buffer[MAX_LOG_SIZE]; hilogShowFormatBuffer showBuffer; @@ -200,7 +200,7 @@ int GenPersistLogHeader(HilogData *data, list&persistList) persistList.push_back(buffer); offset += dataPos - dataBegin + 1; } else { - offset++; + offset++; } dataBegin = dataPos + 1; } -- Gitee From 8c0ac8bbe88e08b22e33231bb166429abb23a412 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 01:24:31 +0000 Subject: [PATCH 33/40] MAXLOGLEN Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index ea9606d..6b8501b 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -175,8 +175,8 @@ void LogPersister::SetBufferOffset(int off) int GenPersistLogHeader(HilogData *data, list& persistList) { - char buffer[MAX_LOG_SIZE]; - hilogShowFormatBuffer showBuffer; + char buffer[MAX_LOG_LEN]; + HilogShowFormatBuffer showBuffer; showBuffer.level = data->level; showBuffer.pid = data->pid; showBuffer.tid = data->tid; -- Gitee From 2f26a117ee75f480bf00b45b590be16daebe12a4 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 05:16:35 +0000 Subject: [PATCH 34/40] persisit memcpy_s --- services/hilogd/log_persister.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 6b8501b..a014a68 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -183,19 +183,20 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.domain = data->domain; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; - showBuffer.data = data->tag; - int offset = data->tag_len; - - static char *dataCopy; - memcpy_s(&dataCopy, data->len, &data->content, data->len); - char *dataBegin = dataCopy; - char *dataPos = dataCopy; + int offset = data->tag_len; + char *dataCopy= (char*)calloc(MAX_LOG_LEN, sizeof(char)); + memcpy_s(dataCopy, offset, data->tag, offset); + memcpy_s(dataCopy + offset, data->len, data->content, data->len); + showBuffer.data = dataCopy; + char *dataBegin = dataCopy + offset; + char *dataPos = dataCopy + offset; while (*dataPos != 0) { if (*dataPos == '\n') { if (dataPos != dataBegin) { *dataPos = 0; showBuffer.tag_len = offset; + showBuffer.data = dataCopy; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); offset += dataPos - dataBegin + 1; @@ -208,6 +209,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) } if (dataPos != dataBegin) { showBuffer.tag_len = offset; + showBuffer.data = dataCopy; HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); } -- Gitee From e1f65055e20a87228790c8464fdd394bfe4ecd54 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 05:23:13 +0000 Subject: [PATCH 35/40] persisit free Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index a014a68..78c5bcd 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -213,6 +213,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); persistList.push_back(buffer); } + free(dataCopy); return persistList.size(); } -- Gitee From 99b4a35503429cec9f2291353275d4a74e11dcf0 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 06:01:54 +0000 Subject: [PATCH 36/40] code format Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 78c5bcd..86b7392 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -185,9 +185,9 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.tv_nsec = data->tv_nsec; int offset = data->tag_len; - char *dataCopy= (char*)calloc(MAX_LOG_LEN, sizeof(char)); + char *dataCopy = (char*)calloc(data->len, sizeof(char)); memcpy_s(dataCopy, offset, data->tag, offset); - memcpy_s(dataCopy + offset, data->len, data->content, data->len); + memcpy_s(dataCopy + offset, MAX_LOG_LEN, data->content, MAX_LOG_LEN); showBuffer.data = dataCopy; char *dataBegin = dataCopy + offset; char *dataPos = dataCopy + offset; -- Gitee From 77c46b6f73d21d3034a265265162ba71e5b9257c Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 06:15:15 +0000 Subject: [PATCH 37/40] max len Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 86b7392..eb2ae30 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -185,9 +185,9 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.tv_nsec = data->tv_nsec; int offset = data->tag_len; - char *dataCopy = (char*)calloc(data->len, sizeof(char)); + char *dataCopy = (char*)calloc(MAX_LOG_LEN, sizeof(char)); memcpy_s(dataCopy, offset, data->tag, offset); - memcpy_s(dataCopy + offset, MAX_LOG_LEN, data->content, MAX_LOG_LEN); + memcpy_s(dataCopy + offset, data->len, data->content, data->len); showBuffer.data = dataCopy; char *dataBegin = dataCopy + offset; char *dataPos = dataCopy + offset; -- Gitee From 2a06583630099a7ad4cceb12b1368299f5ef94a4 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 07:21:26 +0000 Subject: [PATCH 38/40] modify len Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilogtool_msg.h | 2 +- services/hilogd/include/log_persister.h | 2 +- services/hilogd/log_persister.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 1cfe5a5..52f31c4 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -22,7 +22,7 @@ #include #include "hilog_common.h" -#define FILE_PATH_MAX_LEN 100 +#define FILE_PATH_MAX_LEN 1024 typedef enum { LOG_QUERY_REQUEST = 0x01, diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index a08992f..74dd580 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -59,7 +59,7 @@ public: uint8_t getType() const; std::string getPath(); LogPersisterBuffer *buffer; - + private: uint32_t id; std::string path; diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index eb2ae30..b3880b1 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -185,7 +185,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.tv_nsec = data->tv_nsec; int offset = data->tag_len; - char *dataCopy = (char*)calloc(MAX_LOG_LEN, sizeof(char)); + char *dataCopy= (char*)calloc(data->len + offset, sizeof(char)); memcpy_s(dataCopy, offset, data->tag, offset); memcpy_s(dataCopy + offset, data->len, data->content, data->len); showBuffer.data = dataCopy; -- Gitee From 798e5dc5e96c19e7f4c51aaa53ca0fbcfaf36b6d Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 07:25:44 +0000 Subject: [PATCH 39/40] modify len Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_persister.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index b3880b1..11e7d2c 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -187,7 +187,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) int offset = data->tag_len; char *dataCopy= (char*)calloc(data->len + offset, sizeof(char)); memcpy_s(dataCopy, offset, data->tag, offset); - memcpy_s(dataCopy + offset, data->len, data->content, data->len); + memcpy_s(dataCopy + offset, data->len - offset, data->content, data->len - offset); showBuffer.data = dataCopy; char *dataBegin = dataCopy + offset; char *dataPos = dataCopy + offset; -- Gitee From b2fe8895627e5b58fecce1eecec54bcb807b544c Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 29 Jun 2021 07:45:33 +0000 Subject: [PATCH 40/40] modify len Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/include/hilogtool_msg.h | 2 +- services/hilogd/log_persister.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index 52f31c4..1cfe5a5 100644 --- a/frameworks/native/include/hilogtool_msg.h +++ b/frameworks/native/include/hilogtool_msg.h @@ -22,7 +22,7 @@ #include #include "hilog_common.h" -#define FILE_PATH_MAX_LEN 1024 +#define FILE_PATH_MAX_LEN 100 typedef enum { LOG_QUERY_REQUEST = 0x01, diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 11e7d2c..be8a760 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -185,7 +185,7 @@ int GenPersistLogHeader(HilogData *data, list& persistList) showBuffer.tv_nsec = data->tv_nsec; int offset = data->tag_len; - char *dataCopy= (char*)calloc(data->len + offset, sizeof(char)); + char *dataCopy= (char*)calloc(data->len, sizeof(char)); memcpy_s(dataCopy, offset, data->tag, offset); memcpy_s(dataCopy + offset, data->len - offset, data->content, data->len - offset); showBuffer.data = dataCopy; -- Gitee