diff --git a/adapter/properties.cpp b/adapter/properties.cpp index aa407d75243c4ea2450f8bc5a06acc934d44e8b9..1e6e91b60574bbd13e1218d95639b8fabd741962 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -383,4 +383,4 @@ uint16_t GetTagLevel(const std::string& tag) return it->second->logLevel; } } -} +} \ No newline at end of file diff --git a/adapter/properties.h b/adapter/properties.h index 42d1f7b6bd2d9fe7b17374c240633c434af99f83..cebbaa193af0ecdd55d7bffdbf5d18deefea87d0 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/frameworks/native/include/hilogtool_msg.h b/frameworks/native/include/hilogtool_msg.h index cd4b03c68ef58b5a2d7568aa0f7d0e97ef1f3fb6..1cfe5a523992e4d0851b10cd42eaea733d472694 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/BUILD.gn b/services/hilogd/BUILD.gn index d12c70abadfaa32713948af9df6fecc7d84ae12d..a0e435916b6e90ad0e875f09f14ceb5bb82cc4ca 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -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 diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 1eb4de77f969ff0e992df0e3f140f2dec979dcd5..74dd580a493fa2077979a442afdf3a5c6b51784f 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[]; @@ -58,7 +59,7 @@ public: uint8_t getType() const; std::string getPath(); LogPersisterBuffer *buffer; - + private: uint32_t id; std::string path; @@ -77,11 +78,11 @@ private: bool isExited(); FILE *fdinfo; int fd = -1; - LogCompress *LogCompress; + list persistList; }; -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 b49a53e41ee70caacd59a495e224c664dba91fac..be8a76013db79817a3375a4d2f1ad1c0c6260ab3 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; @@ -50,23 +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.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; -} - LogPersister::LogPersister(uint32_t id, string path, uint16_t compressType, uint16_t compressAlg, int sleepTime, LogPersisterRotator *rotator, HilogBuffer *_buffer) @@ -89,15 +72,6 @@ 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; @@ -180,7 +154,7 @@ int LogPersister::Init() void LogPersister::NotifyForNewData() { - condVariable.notify_one(); + condVariable.notify_one(); isNotified = true; } @@ -199,25 +173,77 @@ void LogPersister::SetBufferOffset(int off) fprintf(fdinfo, "%04x\n", off); } +int GenPersistLogHeader(HilogData *data, list& persistList) +{ + 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 *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; + 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; + } else { + offset++; + } + dataBegin = dataPos + 1; + } + dataPos++; + } + if (dataPos != dataBegin) { + showBuffer.tag_len = offset; + showBuffer.data = dataCopy; + HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, OFF_SHOWFORMAT); + persistList.push_back(buffer); + } + free(dataCopy); + return persistList.size(); +} + bool LogPersister::writeUnCompressedBuffer(HilogData *data) { - 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; + int listSize = 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; + if (persistList.empty()) { + listSize = GenPersistLogHeader(data, persistList); + } + while (listSize--) { + string header = persistList.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); } - SetBufferOffset(buffer->offset + headerLen); - buffer->content[buffer->offset] = '\n'; - SetBufferOffset(buffer->offset + 1); return true; } @@ -233,6 +259,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(); diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 8ef90acff30ad9f63f7d8c1ffbb8f2fffc9f60e4..f88f7eae331f10dbfb0998bf96b141ff6bf22eff 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" @@ -42,7 +43,7 @@ namespace HiviewDFX { using namespace std; constexpr int MAX_DATA_LEN = 2048; - +string g_logPersisterDir = "/data/misc/logd/"; constexpr int DEFAULT_LOG_LEVEL = 1<msgType = msgCmd; msgHeader->msgLen = 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; +} LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; @@ -123,6 +130,19 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade if (pLogPersistStartRst == nullptr) { return; } + 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()); rotator = MakeRotator(*pLogPersistStartMsg); std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, @@ -135,7 +155,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(); @@ -201,11 +221,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; @@ -223,7 +241,7 @@ void HandlePersistQueryRequest(char* reqMsg, std::shared_ptr logReade 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 84ddbfc15e64e98b90ea948a3f24cf45d712ac7e..09691bdb45f94b4aac06fda2f6e49221b592f7b2 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -38,7 +38,6 @@ 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) { @@ -377,13 +376,12 @@ 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->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()); + 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 540c0d4730c6633b19e9b7329431f4f43f0731f4..8353dda13161b7873d861c3864dc8d4e952e8593 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -398,12 +398,12 @@ bool HilogMatchByRegex(string context, string regExpArg) } } -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; @@ -435,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; @@ -444,14 +443,42 @@ 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; - showBuffer.data = data->data; - HilogShowBuffer(buffer, MAX_LOG_LEN * 2, showBuffer, showFormat); - if (context->tailLines) { - tailBuffer.emplace_back(buffer); - return; + 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; + } else { + 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; + } else { + cout << buffer << endl; + } } - cout << buffer << endl; return; } } // namespace HiviewDFX diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 36b54c5e978cec77bc6da02e0db4d0ddd28e843e..ced850de9c4f294d307b5a73a42e279c2eb6fd2d 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" @@ -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" ); } @@ -197,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' }, @@ -316,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; @@ -395,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);