diff --git a/src/common/base.cpp b/src/common/base.cpp index 95fd3ea220ee6ae6be99d924583594008baa694c..4c8e74dda1e96d8f2b34d66d4f7e2e2769fab478 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,19 +159,35 @@ 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; +#ifdef _WIN32 + if (localtime_s(&tmTime, &sinceUnix0Sec) != 0) { + WRITE_LOG(LOG_FATAL, "get localtime failed!"); + return; + } - 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()); - } +#else + if (localtime_r(&sinceUnix0Sec, &tmTime) == nullptr) { + WRITE_LOG(LOG_FATAL, "get localtime failed! "); + return; + } +#endif + char buffer[TIME_BUF_SIZE] = {0}; + + 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; } + timeString = buffer; } #ifndef HDC_HILOG @@ -2735,17 +2750,13 @@ void CloseOpenFd(void) g_cmdlogSwitch = false; return; } - size_t envLen = strlen(env); - if (envLen > 1) { + const char *envEnableValue = "1"; // 1 is enable + size_t envEnableValueLen = 1; // len is 1 + if (strlen(env) > envEnableValueLen) { g_cmdlogSwitch = false; return; } - constexpr size_t maxLen = 1; - if (envLen != maxLen) { - g_cmdlogSwitch = false; - return; - } - if (strncmp(env, "1", maxLen) == 0) { + if (strncmp(env, envEnableValue, envEnableValueLen) == 0) { g_cmdlogSwitch = true; return; } @@ -2825,7 +2836,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 +2868,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 +2903,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 +2910,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 +2922,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 +2935,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 +2954,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 +3100,7 @@ void CloseOpenFd(void) return; } std::unique_lock lock(g_threadCompressCmdLogsMutex); - g_compressCmdLogsThread = std::make_shared(ThreadCmdStrAndCmdLogs); + g_compressCmdLogsThread = std::make_unique(ThreadCmdStrAndCmdLogs); } std::string CmdLogStringFormat(uint32_t targetSessionId, const std::string& cmdStr)