From e7400e77d81329440173f17ce92cec6f2a6e5550 Mon Sep 17 00:00:00 2001 From: luciferWei Date: Fri, 15 Aug 2025 11:19:47 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Feat=EF=BC=9A=E4=BF=AE=E6=94=B9review?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=20\nSigned-off-by:=20LuciferWei=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luciferWei --- src/common/base.cpp | 58 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index 95fd3ea2..1e1cab38 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -53,9 +53,8 @@ namespace Base { bool g_heartbeatSwitch = true; constexpr int DEF_FILE_PERMISSION = 0750; bool g_cmdlogSwitch = false; - std::vector g_cmdLogsFilesStrings; std::mutex g_threadCompressCmdLogsMutex; - std::shared_ptr g_compressCmdLogsThread; + std::unique_ptr g_compressCmdLogsThread; uint8_t GetLogLevel() { return g_logLevel; @@ -160,21 +159,22 @@ namespace Base { void GetTimeString(string &timeString) { system_clock::time_point timeNow = system_clock::now(); - system_clock::duration sinceUnix0 = timeNow.time_since_epoch(); // since 1970 - time_t sinceUnix0Time = duration_cast(sinceUnix0).count(); - std::tm *timeTm = std::localtime(&sinceUnix0Time); + system_clock::duration sinceUnix0 = timeNow.time_since_epoch(); + time_t sinceUnix0Sec = duration_cast(sinceUnix0).count(); + auto milliseconds = duration_cast(sinceUnix0).count() % TIME_BASE; + + struct tm tmTime; + localtime_r(&sinceUnix0Sec, &tmTime); - const auto sinceUnix0Rest = duration_cast(sinceUnix0).count() % TIME_BASE; - string msTimeSurplus = StringFormat("%03llu", sinceUnix0Rest); - timeString = msTimeSurplus; - if (timeTm != nullptr) { - char buffer[TIME_BUF_SIZE] = {0}; - if (strftime(buffer, TIME_BUF_SIZE, "%Y%m%d-%H%M%S", timeTm) > 0) { - timeString = StringFormat("%s%s", buffer, msTimeSurplus.c_str()); - } - } + char buffer[TIME_BUF_SIZE] = {0}; + snprintf(buffer, sizeof(buffer), "%04d%02d%02d-%02d%02d%02d%03ld", + tmTime.tm_year + 1900, tmTime.tm_mon + 1, tmTime.tm_mday, + tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, + milliseconds); + timeString = buffer; } + #ifndef HDC_HILOG void LogToPath(const char *path, const char *str) { @@ -2741,10 +2741,6 @@ void CloseOpenFd(void) return; } constexpr size_t maxLen = 1; - if (envLen != maxLen) { - g_cmdlogSwitch = false; - return; - } if (strncmp(env, "1", maxLen) == 0) { g_cmdlogSwitch = true; return; @@ -2825,7 +2821,7 @@ void CloseOpenFd(void) bool retVal = false; string sourceFileNameFull = filePath + sourceFileName; if (access(sourceFileNameFull.c_str(), F_OK) != 0) { - WRITE_LOG(LOG_FATAL, "file %s not exist", sourceFileNameFull.c_str()); + WRITE_LOG(LOG_FATAL, "file %s not exist", sourceFileName.c_str()); return retVal; } if (targetFileName.empty()) { @@ -2857,8 +2853,8 @@ void CloseOpenFd(void) WRITE_LOG(LOG_DEBUG, "subprocess exited withe status: %d", exitCode); retVal = true; } else { - WRITE_LOG(LOG_FATAL, "CompressLogFile failed, soiurceFileNameFull:%s, error:%s", - sourceFileNameFull.c_str(), + WRITE_LOG(LOG_FATAL, "CompressLogFile failed, sourceFileName:%s, error:%s", + sourceFileName.c_str(), strerror(errno)); } } @@ -2892,8 +2888,6 @@ void CloseOpenFd(void) { uv_fs_t fs; int value = uv_fs_stat(nullptr, &fs, fileName.c_str(), nullptr); - uint64_t fileSize = fs.statbuf.st_size; - uv_fs_req_cleanup(&fs); if (value != 0) { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; @@ -2901,6 +2895,8 @@ void CloseOpenFd(void) WRITE_LOG(LOG_FATAL, "uv_fs_stat failed, file:%s error:%s", fileName.c_str(), buf); return true; } + uint64_t fileSize = fs.statbuf.st_size; + uv_fs_req_cleanup(&fs); if (fileSize < fileMaxSize) { return true; } @@ -2911,7 +2907,7 @@ void CloseOpenFd(void) const std::string& matchPreStr, const std::string& matchSufStr) { - g_cmdLogsFilesStrings.clear(); + std::vector cmdLogsFilesStrings; #ifdef _WIN32 WIN32_FIND_DATAA findData; std::string matchStr = matchPreStr + "*" + matchSufStr; @@ -2924,14 +2920,18 @@ void CloseOpenFd(void) do { if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { SetErrorMode(SEM_FAILCRITICALERRORS); - g_cmdLogsFilesStrings.push_back(findData.cFileName); + cmdLogsFilesStrings.push_back(findData.cFileName); } } while (FindNextFile(hFind, &findData)); + if (!FindClose(hFind)) { + WRITE_LOG(LOG_FATAL, "FindClose failed, path:%s", findFileMatchStr.c_str()); + return ; + } #else DIR *dir = opendir(path.c_str()); if (dir == nullptr) { WRITE_LOG(LOG_WARN, "open %s failed", path.c_str()); - return g_cmdLogsFilesStrings; + return cmdLogsFilesStrings; } struct dirent *entry; while ((entry = readdir(dir)) != nullptr) { @@ -2939,12 +2939,12 @@ void CloseOpenFd(void) if ((strncmp(fileName.c_str(), matchPreStr.c_str(), matchPreStr.length()) == 0) && (strncmp(fileName.c_str() + fileName.length() - matchSufStr.length(), matchSufStr.c_str(), matchSufStr.length()) == 0)) { - g_cmdLogsFilesStrings.push_back(fileName); + cmdLogsFilesStrings.push_back(fileName); } } closedir(dir); #endif - return g_cmdLogsFilesStrings; + return cmdLogsFilesStrings; } void ControlFilesByRegex(const std::string& path, @@ -3085,7 +3085,7 @@ void CloseOpenFd(void) return; } std::unique_lock lock(g_threadCompressCmdLogsMutex); - g_compressCmdLogsThread = std::make_shared(ThreadCmdStrAndCmdLogs); + g_compressCmdLogsThread = std::unique_ptr(new std::thread(ThreadCmdStrAndCmdLogs)); } std::string CmdLogStringFormat(uint32_t targetSessionId, const std::string& cmdStr) -- Gitee From 830fea8a7ea1a6e23b6d27baade884ee9a800e1e Mon Sep 17 00:00:00 2001 From: luciferWei Date: Fri, 15 Aug 2025 14:08:25 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Feat=EF=BC=9A=E4=BF=AE=E6=94=B9review?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=20\nSigned-off-by:=20LuciferWei=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luciferWei --- src/common/base.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index 1e1cab38..ecac57a6 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -164,13 +164,17 @@ namespace Base { auto milliseconds = duration_cast(sinceUnix0).count() % TIME_BASE; struct tm tmTime; +#ifdef _WIN32 + localtime_s(&tmTime, &sinceUnix0Sec); +#else localtime_r(&sinceUnix0Sec, &tmTime); - +#endif char buffer[TIME_BUF_SIZE] = {0}; - snprintf(buffer, sizeof(buffer), "%04d%02d%02d-%02d%02d%02d%03ld", - tmTime.tm_year + 1900, tmTime.tm_mon + 1, tmTime.tm_mday, - tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, - milliseconds); + // 1900: count of years + (void)snprintf_s(buffer, sizeof(buffer), "%04d%02d%02d-%02d%02d%02d%03lld", + tmTime.tm_year + 1900, tmTime.tm_mon + 1, tmTime.tm_mday, + tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, + milliseconds); timeString = buffer; } @@ -3085,7 +3089,7 @@ void CloseOpenFd(void) return; } std::unique_lock lock(g_threadCompressCmdLogsMutex); - g_compressCmdLogsThread = std::unique_ptr(new std::thread(ThreadCmdStrAndCmdLogs)); + g_compressCmdLogsThread = std::make_unique(ThreadCmdStrAndCmdLogs); } std::string CmdLogStringFormat(uint32_t targetSessionId, const std::string& cmdStr) -- Gitee From 403af4a45d3b58332a4711b8cf5bf4abe05b5174 Mon Sep 17 00:00:00 2001 From: luciferWei Date: Fri, 15 Aug 2025 14:22:38 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Feat=EF=BC=9A=E4=BF=AE=E6=94=B9review?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=20\nSigned-off-by:=20LuciferWei=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luciferWei --- src/common/base.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index ecac57a6..c72dd17b 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -165,16 +165,27 @@ namespace Base { struct tm tmTime; #ifdef _WIN32 - localtime_s(&tmTime, &sinceUnix0Sec); + if (localtime_s(&tmTime, &sinceUnix0Sec) != 0) { + WRITE_LOG(LOG_FATAL, "get localtime failed!"); + return ""; + } + #else - localtime_r(&sinceUnix0Sec, &tmTime); + if (localtime_r(&sinceUnix0Sec, &tmTime) == nullptr) { + WRITE_LOG(LOG_FATAL, "get localtime failed!"); + return ""; + } #endif char buffer[TIME_BUF_SIZE] = {0}; // 1900: count of years - (void)snprintf_s(buffer, sizeof(buffer), "%04d%02d%02d-%02d%02d%02d%03lld", - tmTime.tm_year + 1900, tmTime.tm_mon + 1, tmTime.tm_mday, - tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, - milliseconds); + int res = snprintf_s(buffer, sizeof(buffer), "%04d%02d%02d-%02d%02d%02d%03lld", tmTime.tm_year + 1900, + tmTime.tm_mon + 1, tmTime.tm_mday, + tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, + milliseconds); + if (res < 0) { + WRITE_LOG(LOG_FATAL, "snprintf_s Error!"); + return ""; + } timeString = buffer; } -- Gitee From 714dc1c8b854d167765b2a5c4f9db442d427ab74 Mon Sep 17 00:00:00 2001 From: luciferWei Date: Fri, 15 Aug 2025 14:52:14 +0800 Subject: [PATCH 4/6] =?UTF-8?q?Feat=EF=BC=9A=E4=BF=AE=E6=94=B9review?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=20\nSigned-off-by:=20LuciferWei=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luciferWei --- src/common/base.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index c72dd17b..e7f26e9b 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -167,24 +167,25 @@ namespace Base { #ifdef _WIN32 if (localtime_s(&tmTime, &sinceUnix0Sec) != 0) { WRITE_LOG(LOG_FATAL, "get localtime failed!"); - return ""; + return; } #else if (localtime_r(&sinceUnix0Sec, &tmTime) == nullptr) { - WRITE_LOG(LOG_FATAL, "get localtime failed!"); - return ""; + WRITE_LOG(LOG_FATAL, "get localtime failed! "); + return; } #endif char buffer[TIME_BUF_SIZE] = {0}; - // 1900: count of years - int res = snprintf_s(buffer, sizeof(buffer), "%04d%02d%02d-%02d%02d%02d%03lld", tmTime.tm_year + 1900, + + int res = snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%04d%02d%02d-%02d%02d%02d%03lld", + tmTime.tm_year + 1900, // 1900: count of years tmTime.tm_mon + 1, tmTime.tm_mday, tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec, milliseconds); if (res < 0) { WRITE_LOG(LOG_FATAL, "snprintf_s Error!"); - return ""; + return; } timeString = buffer; } -- Gitee From 98fdaa5036ceb467c3f1b971d420dafc63a387da Mon Sep 17 00:00:00 2001 From: luciferWei Date: Mon, 18 Aug 2025 10:26:58 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Feat=EF=BC=9A=E4=BF=AE=E6=94=B9review?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=20\nSigned-off-by:=20LuciferWei=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luciferWei --- src/common/base.cpp | 7 ++----- src/common/define.h | 2 ++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index e7f26e9b..c8cc7f29 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -190,7 +190,6 @@ namespace Base { timeString = buffer; } - #ifndef HDC_HILOG void LogToPath(const char *path, const char *str) { @@ -2751,13 +2750,11 @@ void CloseOpenFd(void) g_cmdlogSwitch = false; return; } - size_t envLen = strlen(env); - if (envLen > 1) { + if (strlen(env); > ENV_SERVER_CMD_LOG_ENABLE_LEN) { g_cmdlogSwitch = false; return; } - constexpr size_t maxLen = 1; - if (strncmp(env, "1", maxLen) == 0) { + if (strncmp(env, ENV_SERVER_CMD_LOG_ENABLE, ENV_SERVER_CMD_LOG_ENABLE_LEN) == 0) { g_cmdlogSwitch = true; return; } diff --git a/src/common/define.h b/src/common/define.h index 1ab9c64c..531139f6 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -236,6 +236,8 @@ const string TERMINAL_HDC_PROCESS_FAILED = "[E002101]:Terminal hdc process faile strerror_r(errno, buf, 1024) #endif const string ENV_SERVER_CMD_LOG = "OHOS_HDC_CMD_RECORD"; +const string ENV_SERVER_CMD_LOG_ENABLE = "1"; // 1 is enable +constexpr size_t ENV_SERVER_CMD_LOG_ENABLE_LEN = 1; // len is 1 const string CMD_LOG_FILE_NAME = "hdc-cmd.log"; const string CMD_LOG_DIR_NAME = "hdc_cmd"; const string CMD_LOG_FILE_TYPE = ".log"; -- Gitee From 887083678962f765e93308c349d3d9902e7b604f Mon Sep 17 00:00:00 2001 From: luciferWei Date: Mon, 18 Aug 2025 11:07:52 +0800 Subject: [PATCH 6/6] =?UTF-8?q?Feat=EF=BC=9A=E4=BF=AE=E6=94=B9review?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=20\nSigned-off-by:=20LuciferWei=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luciferWei --- src/common/base.cpp | 6 ++++-- src/common/define.h | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index c8cc7f29..4c8e74dd 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -2750,11 +2750,13 @@ void CloseOpenFd(void) g_cmdlogSwitch = false; return; } - if (strlen(env); > ENV_SERVER_CMD_LOG_ENABLE_LEN) { + const char *envEnableValue = "1"; // 1 is enable + size_t envEnableValueLen = 1; // len is 1 + if (strlen(env) > envEnableValueLen) { g_cmdlogSwitch = false; return; } - if (strncmp(env, ENV_SERVER_CMD_LOG_ENABLE, ENV_SERVER_CMD_LOG_ENABLE_LEN) == 0) { + if (strncmp(env, envEnableValue, envEnableValueLen) == 0) { g_cmdlogSwitch = true; return; } diff --git a/src/common/define.h b/src/common/define.h index 531139f6..1ab9c64c 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -236,8 +236,6 @@ const string TERMINAL_HDC_PROCESS_FAILED = "[E002101]:Terminal hdc process faile strerror_r(errno, buf, 1024) #endif const string ENV_SERVER_CMD_LOG = "OHOS_HDC_CMD_RECORD"; -const string ENV_SERVER_CMD_LOG_ENABLE = "1"; // 1 is enable -constexpr size_t ENV_SERVER_CMD_LOG_ENABLE_LEN = 1; // len is 1 const string CMD_LOG_FILE_NAME = "hdc-cmd.log"; const string CMD_LOG_DIR_NAME = "hdc_cmd"; const string CMD_LOG_FILE_TYPE = ".log"; -- Gitee