diff --git a/frameworks/native/format.cpp b/frameworks/native/format.cpp index 5126e9f868999e407a4ebbd8d7e44e86b52bdd7e..64854dc26ff1bb1361de6008b690b13a18cca19a 100644 --- a/frameworks/native/format.cpp +++ b/frameworks/native/format.cpp @@ -61,7 +61,6 @@ int HilogShowTimeBuffer(char* buffer, int bufLen, uint32_t showFormat, { time_t now = contentOut.tv_sec; unsigned long nsecTime = contentOut.tv_nsec; - struct tm* ptm = nullptr; size_t timeLen = 0; int ret = 0; nsecTime = (now < 0) ? (NSEC - nsecTime) : nsecTime; @@ -71,24 +70,24 @@ int HilogShowTimeBuffer(char* buffer, int bufLen, uint32_t showFormat, (showFormat & (1 << MONOTONIC_SHOWFORMAT)) ? "%6lld" : "%19lld", (long long)now); timeLen += ((ret > 0) ? ret : 0); } else { - ptm = localtime(&now); - if (ptm == nullptr) { + struct tm tmLocal; + if (localtime_r(&now, &tmLocal) == nullptr) { return 0; } - timeLen = strftime(buffer, bufLen, "%m-%d %H:%M:%S", ptm); + timeLen = strftime(buffer, bufLen, "%m-%d %H:%M:%S", &tmLocal); timeLen = strlen(buffer); if (showFormat & (1 << YEAR_SHOWFORMAT)) { - timeLen = strftime(buffer, bufLen, "%Y-%m-%d %H:%M:%S", ptm); + timeLen = strftime(buffer, bufLen, "%Y-%m-%d %H:%M:%S", &tmLocal); timeLen = strlen(buffer); } if (showFormat & (1 << ZONE_SHOWFORMAT)) { - timeLen = strftime(buffer, bufLen, "%z %m-%d %H:%M:%S", ptm); + timeLen = strftime(buffer, bufLen, "%z %m-%d %H:%M:%S", &tmLocal); timeLen = strlen(buffer); } } if (showFormat & (1 << TIME_NSEC_SHOWFORMAT)) { ret = snprintf_s(buffer + timeLen, bufLen - timeLen, bufLen - timeLen - 1, - ".%09ld", nsecTime); + ".%09lu", nsecTime); timeLen += ((ret > 0) ? ret : 0); } else if (showFormat & (1 << TIME_USEC_SHOWFORMAT)) { ret = snprintf_s(buffer + timeLen, bufLen - timeLen, bufLen - timeLen - 1, diff --git a/frameworks/native/hilog_base.cpp b/frameworks/native/hilog_base.cpp index 3d66f11c47b6056f98287d5d3f99a07a0c6e0bd5..081fb1cc03e60791e619d563af6a67df6739f10f 100644 --- a/frameworks/native/hilog_base.cpp +++ b/frameworks/native/hilog_base.cpp @@ -100,7 +100,7 @@ static int CheckConnection(SocketHandler& socketHandler) return 0; } -static int SendMessage(HilogMsg *header, const char *tag, int tagLen, const char *fmt, int fmtLen) +static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, uint16_t fmtLen) { SocketHandler socketHandler; int ret = CheckSocket(socketHandler); @@ -115,7 +115,7 @@ static int SendMessage(HilogMsg *header, const char *tag, int tagLen, const char struct timeval tv = {0}; gettimeofday(&tv, nullptr); header->tv_sec = tv.tv_sec; - header->tv_nsec = tv.tv_usec * 1000; // 1000 : usec convert to nsec + header->tv_nsec = static_cast(tv.tv_usec * 1000); // 1000 : usec convert to nsec header->len = sizeof(HilogMsg) + tagLen + fmtLen; header->tag_len = tagLen; @@ -141,15 +141,15 @@ static int HiLogBasePrintArgs(const LogType type, const LogLevel level, const un vsnprintfp_s(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, HILOG_DEFAULT_PRIVACY, fmt, ap); - int tagLen = strnlen(tag, MAX_TAG_LEN - 1); - int logLen = strnlen(buf, MAX_LOG_LEN - 1); + auto tagLen = strnlen(tag, MAX_TAG_LEN - 1); + auto logLen = strnlen(buf, MAX_LOG_LEN - 1); HilogMsg header = {0}; header.type = type; header.level = level; #ifndef __RECV_MSG_WITH_UCRED_ header.pid = getpid(); #endif - header.tid = gettid(); + header.tid = static_cast(gettid()); header.domain = domain; return SendMessage(&header, tag, tagLen + 1, buf, logLen + 1); diff --git a/frameworks/native/hilog_input_socket_client.cpp b/frameworks/native/hilog_input_socket_client.cpp index a7f57bc6041fcaa694956d6165779b7a6f981d1b..ea62bb4a9036b9ee33fe3f990ee35601ff6b222d 100644 --- a/frameworks/native/hilog_input_socket_client.cpp +++ b/frameworks/native/hilog_input_socket_client.cpp @@ -22,13 +22,14 @@ namespace OHOS { namespace HiviewDFX { static HilogInputSocketClient g_hilogInputSocketClient; -extern "C" int HilogWriteLogMessage(HilogMsg *header, const char *tag, int tagLen, const char *fmt, int fmtLen) +extern "C" int HilogWriteLogMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, + uint16_t fmtLen) { return g_hilogInputSocketClient.WriteLogMessage(header, tag, tagLen, fmt, fmtLen); } -int HilogInputSocketClient::WriteLogMessage(HilogMsg *header, const char *tag, int tagLen, const char *fmt, - int fmtLen) +int HilogInputSocketClient::WriteLogMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, + uint16_t fmtLen) { int ret = CheckSocket(); if (ret < 0) { @@ -38,7 +39,7 @@ int HilogInputSocketClient::WriteLogMessage(HilogMsg *header, const char *tag, i struct timeval tv = {0}; gettimeofday(&tv, nullptr); header->tv_sec = tv.tv_sec; - header->tv_nsec = tv.tv_usec * 1000; // 1000 : usec convert to nsec + header->tv_nsec = static_cast(tv.tv_usec * 1000); // 1000 : usec convert to nsec header->len = sizeof(HilogMsg) + tagLen + fmtLen; header->tag_len = tagLen; diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 224b3ca0565378479cf97079f604ae6de281f993..e6d523df25fa0dacf752c90e8aa9bbc4b1e007f6 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -232,7 +232,7 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int /* format log string */ debug = IsDebugOn(); priv = (!debug) && IsPrivateSwitchOn(); - + #ifdef __clang__ /* code specific to clang compiler */ #pragma clang diagnostic push @@ -250,14 +250,14 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int #endif /* fill header info */ - int tagLen = strnlen(tag, MAX_TAG_LEN - 1); - int logLen = strnlen(buf, MAX_LOG_LEN - 1); + auto tagLen = strnlen(tag, MAX_TAG_LEN - 1); + auto logLen = strnlen(buf, MAX_LOG_LEN - 1); header.type = type; header.level = level; #ifndef __RECV_MSG_WITH_UCRED_ header.pid = getpid(); #endif - header.tid = syscall(SYS_gettid); + header.tid = static_cast(syscall(SYS_gettid)); header.domain = domain; /* flow control */ diff --git a/frameworks/native/include/hilog_input_socket_client.h b/frameworks/native/include/hilog_input_socket_client.h index bd4c4a22e1bd502d84977325067ed0839aea7a1b..0bea24060de3360cfd3d973ec64d3d2f3fb7ccb1 100644 --- a/frameworks/native/include/hilog_input_socket_client.h +++ b/frameworks/native/include/hilog_input_socket_client.h @@ -24,11 +24,12 @@ namespace HiviewDFX { class HilogInputSocketClient : DgramSocketClient { public: HilogInputSocketClient() : DgramSocketClient(INPUT_SOCKET_NAME, SOCK_NONBLOCK | SOCK_CLOEXEC) {}; - int WriteLogMessage(HilogMsg *header, const char *tag, int tagLen, const char *fmt, int fmtLen); + int WriteLogMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, uint16_t fmtLen); ~HilogInputSocketClient() = default; }; } // namespace HiviewDFX } // namespace OHOS -extern "C" int HilogWriteLogMessage(HilogMsg *header, const char *tag, int tagLen, const char *fmt, int fmtLen); +extern "C" int HilogWriteLogMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, + uint16_t fmtLen); #endif /* HILOG_INPUT_SOCKET_CLIENT_H */ diff --git a/frameworks/native/seq_packet_socket_server.cpp b/frameworks/native/seq_packet_socket_server.cpp index 8b2ae34bf87a6600314c2fe561e0f337c8fc7bad..5b6a1951869fdbbee7c1993f000664833505c46e 100644 --- a/frameworks/native/seq_packet_socket_server.cpp +++ b/frameworks/native/seq_packet_socket_server.cpp @@ -27,7 +27,10 @@ int SeqPacketSocketServer::StartAcceptingConnection(AcceptingHandler onAccepted) if (listeningStatus < 0) { #ifdef DEBUG std::cerr << "Socket listen failed: " << listeningStatus << "\n"; - std::cerr << strerror(listeningStatus) << "\n"; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(listeningStatus, buf, bufSize); + std::cerr << buf << "\n"; #endif return listeningStatus; } @@ -46,7 +49,10 @@ int SeqPacketSocketServer::AcceptingLoop(AcceptingHandler func) int acceptError = errno; #ifdef DEBUG std::cerr << "Socket accept failed: " << acceptError << "\n"; - std::cerr <(len); int midRes = 0; if (data == nullptr) { diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp index e686ac3152066e62c07893e6932ac2a453c4971e..e8a991b2e3a3275f38add052fdca118f200388fd 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp @@ -45,8 +45,8 @@ void ParseLogContent(string& formatStr, vector& params, string& logConte ret += formatStr; return; } - int32_t size = params.size(); - int32_t len = formatStr.size(); + auto size = params.size(); + auto len = formatStr.size(); int32_t pos = 0; int32_t count = 0; bool debug = IsDebugOn(); diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index 78bfeb90c8e7ec9717cf7b5fd822666c457cf7c4..6fe607c138506a87a356aa26f1ab25b52a9f432c 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -56,9 +56,12 @@ void CmdExecutor::MainLoop() } std::cout << "Begin to cmd accept !\n"; int listeningStatus = cmdServer.Listen(MAX_CLIENT_CONNECTIONS); + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; if (listeningStatus < 0) { std::cerr << "Socket listen failed: " << listeningStatus << "\n"; - std::cerr << strerror(listeningStatus) << "\n"; + strerror_r(listeningStatus, buf, bufSize); + std::cerr << buf << "\n"; return; } std::cout << "Server started to listen !\n"; @@ -74,7 +77,8 @@ void CmdExecutor::MainLoop() } else if (pollResult < 0) { int pollError = errno; std::cerr << "Socket polling error: " << pollError << "\n"; - std::cerr << strerror(pollError) << "\n"; + strerror_r(pollError, buf, bufSize); + std::cerr << buf << "\n"; break; } else if (pollResult != 1 || outEvent != POLLIN) { std::cerr << "Wrong poll result data." @@ -92,7 +96,8 @@ void CmdExecutor::MainLoop() } else { int acceptError = errno; std::cerr << "Socket accept failed: " << acceptError << "\n"; - std::cerr << strerror(acceptError) << "\n"; + strerror_r(acceptError, buf, bufSize); + std::cerr << buf << "\n"; break; } } diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index caf40b9c72e952e801164bf13fc3a51eb504d555..3d0540c7df4818b822c79f6f2b2b46c6386a416f 100644 --- a/services/hilogd/flow_control_init.cpp +++ b/services/hilogd/flow_control_init.cpp @@ -112,8 +112,8 @@ void ParseDomainQuota(std::string &domainStr) return; } peakStr = domainStr.substr(domainNameEnd); - uint32_t domain = std::stoi(domainIdStr, nullptr, 0); - uint32_t peak = std::stoi(peakStr, nullptr, 0); + uint32_t domain = static_cast(std::stoi(domainIdStr, nullptr, 0)); + uint32_t peak = static_cast(std::stoi(peakStr, nullptr, 0)); if (domain <= 0 || peak <= 0) { return; } @@ -167,7 +167,7 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) std::unordered_map::iterator it; uint32_t domain = hilogMsg->domain; uint32_t domainId = (domain & DOMAIN_FILTER) >> DOMAIN_FILTER_SUBSYSTEM; - int logLen = hilogMsg->len - sizeof(HilogMsg) - 1 - 1; /* quota length exclude '\0' of tag and log content */ + auto logLen = hilogMsg->len - sizeof(HilogMsg) - 1 - 1; /* quota length exclude '\0' of tag and log content */ int ret = 0; it = g_domainMap.find(domainId); if (it != g_domainMap.end()) { @@ -185,7 +185,7 @@ int FlowCtrlDomain(HilogMsg* hilogMsg) } else { /* new statistic period */ it->second->startTime = tsNow; it->second->sumLen = logLen; - ret = it->second->dropped; + ret = static_cast(it->second->dropped); it->second->dropped = 0; } } diff --git a/services/hilogd/include/log_buffer.h b/services/hilogd/include/log_buffer.h index 4e676ea44ae719c98813e7d16070c00a3a2bba5b..46919c14b8e5865ac56e73c3bee24237489559b3 100644 --- a/services/hilogd/include/log_buffer.h +++ b/services/hilogd/include/log_buffer.h @@ -43,9 +43,9 @@ public: ReaderId CreateBufReader(std::function onNewDataCallback); void RemoveBufReader(const ReaderId& id); - size_t Delete(uint16_t logType); - size_t GetBuffLen(uint16_t logType); - size_t SetBuffLen(uint16_t logType, uint64_t buffSize); + int32_t Delete(uint16_t logType); + int64_t GetBuffLen(uint16_t logType); + int32_t SetBuffLen(uint16_t logType, uint64_t buffSize); int32_t GetStatisticInfoByLog(uint16_t logType, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped); int32_t GetStatisticInfoByDomain(uint32_t domain, uint64_t& printLen, uint64_t& cacheLen, int32_t& dropped); int32_t ClearStatisticInfoByLog(uint16_t logType); diff --git a/services/hilogd/include/log_data.h b/services/hilogd/include/log_data.h index 15446464213c56a5abfad634b59c51b704ee7d21..2d1c649fa8520db4cf8877aba30fa506d5496955 100644 --- a/services/hilogd/include/log_data.h +++ b/services/hilogd/include/log_data.h @@ -77,7 +77,9 @@ struct HilogData { HilogData(HilogData&& cpy) { - std::memcpy(this, &cpy, sizeof(HilogData)); + if (memcpy_s(this, sizeof(HilogData), &cpy, sizeof(HilogData)) != 0) { + std::cerr << "HilogData memcpy_s call failed.\n"; + } cpy.tag = nullptr; cpy.content = nullptr; } diff --git a/services/hilogd/kmsg_parser.cpp b/services/hilogd/kmsg_parser.cpp index c7f763e648dbd731607fc3542a37748192d54422..8021c627eb0f65575ca01850a9321b596a72bfc5 100644 --- a/services/hilogd/kmsg_parser.cpp +++ b/services/hilogd/kmsg_parser.cpp @@ -138,7 +138,7 @@ std::optional KmsgParser::ParseKmsg(const std::vector& km uint32_t mpid = ParsePid(kmsgStr); // If there are some other content wrapped in square brackets "[]", parse it as tag // Otherwise, use default tag "kmsg" - int tagLen = 0; + size_t tagLen = 0; std::string tagStr = ParseTag(kmsgStr); if (!tagStr.empty()) { tagLen = tagStr.size(); @@ -153,8 +153,8 @@ std::optional KmsgParser::ParseKmsg(const std::vector& km } } // Now build HilogMsg and insert it into buffer - int len = kmsgStr.size() + 1; - int msgLen = sizeof(HilogMsg) + tagLen + len + 1; + auto len = kmsgStr.size() + 1; + auto msgLen = sizeof(HilogMsg) + tagLen + len + 1; HilogMsgWrapper msgWrap((std::vector(msgLen, '\0'))); HilogMsg& msg = msgWrap.GetHilogMsg(); msg.len = msgLen; @@ -165,7 +165,7 @@ std::optional KmsgParser::ParseKmsg(const std::vector& km time_point logtime = BootTime() + microseconds{timestamp}; struct timespec logts = TimepointToTimespec(logtime); msg.tv_sec = logts.tv_sec; - msg.tv_nsec = logts.tv_nsec; + msg.tv_nsec = static_cast(logts.tv_nsec); msg.pid = mpid; msg.tid = mpid; if (strncpy_s(msg.tag, tagLen + 1, mtag.data(), tagLen) != 0) { diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index 10eb04041b800dca9f796223592fcd2cc26e90b7..b82d41c6f4c47b643d77905e51c0a12e4d13fecb 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -26,7 +26,7 @@ using namespace std; const float DROP_RATIO = 0.05; static int g_maxBufferSize = 4194304; -static int g_maxBufferSizeByType[LOG_TYPE_MAX] = {262144, 262144, 262144, 262144, 262144}; +static size_t g_maxBufferSizeByType[LOG_TYPE_MAX] = {262144, 262144, 262144, 262144, 262144}; const int DOMAIN_STRICT_MASK = 0xd000000; const int DOMAIN_FUZZY_MASK = 0xdffff; const int DOMAIN_MODULE_BITS = 8; @@ -58,7 +58,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) std::unique_lock lock(hilogBufferMutex); // Delete old entries when full - if (elemSize + sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { + if (elemSize + sizeByType[msg.type] >= g_maxBufferSizeByType[msg.type]) { // Drop 5% of maximum log when full std::list::iterator it = msgList.begin(); while (sizeByType[msg.type] > g_maxBufferSizeByType[msg.type] * (1 - DROP_RATIO) && @@ -75,7 +75,7 @@ size_t HilogBuffer::Insert(const HilogMsg& msg) } // Re-confirm if enough elements has been removed - if (sizeByType[msg.type] >= (size_t)g_maxBufferSizeByType[msg.type]) { + if (sizeByType[msg.type] >= g_maxBufferSizeByType[msg.type]) { std::cout << "Failed to clean old logs." << std::endl; } } @@ -142,7 +142,7 @@ void HilogBuffer::UpdateStatistics(const HilogData& logData) } } -size_t HilogBuffer::Delete(uint16_t logType) +int32_t HilogBuffer::Delete(uint16_t logType) { std::list &msgList = (logType == (0b01 << LOG_KMSG)) ? hilogKlogList : hilogDataList; if (logType >= LOG_TYPE_MAX) { @@ -230,7 +230,7 @@ std::shared_ptr HilogBuffer::GetReader(const ReaderId return std::shared_ptr(); } -size_t HilogBuffer::GetBuffLen(uint16_t logType) +int64_t HilogBuffer::GetBuffLen(uint16_t logType) { if (logType >= LOG_TYPE_MAX) { return ERR_LOG_TYPE_INVALID; @@ -239,7 +239,7 @@ size_t HilogBuffer::GetBuffLen(uint16_t logType) return buffSize; } -size_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) +int32_t HilogBuffer::SetBuffLen(uint16_t logType, uint64_t buffSize) { if (logType >= LOG_TYPE_MAX) { return ERR_LOG_TYPE_INVALID; diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 78ac60621e2a691236bdf459d7a9033c2adf9743..d64d4f5f334069e64a2fde6523a428b649da30e5 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -44,8 +44,8 @@ int ZlibCompress::Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffe return -1; } size_t const toRead = CHUNK; - auto src_pos = 0; - auto dst_pos = 0; + size_t src_pos = 0; + size_t dst_pos = 0; size_t read = inBuffer.offset; int flush = 0; cStream.zalloc = Z_NULL; diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 2ddd95586c065cf83083cefcdd54ac72ce48f675..e2605016dd5c7e118572d7faf0616e837695d3f1 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -57,7 +57,10 @@ int LogKmsg::LinuxReadAllKmsg() const ssize_t maxFailTime = 10; kmsgCtl = GetControlFile("/dev/kmsg"); if (kmsgCtl < 0) { - std::cout << "Cannot open kmsg! Err=" << strerror(errno) << std::endl; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); + std::cout << "Cannot open kmsg! Err=" << buf << std::endl; return -1; } std::unique_ptr parser = std::make_unique(); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index bcd27daf4c5bab8bef81ccbbbca0ac1a34114d15..f77da8d36e67e1928d0f09680afd89b60b83778c 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -66,6 +66,7 @@ std::shared_ptr LogPersister::CreateLogPersister(HilogBuffer &buff LogPersister::LogPersister(HilogBuffer &buffer) : m_hilogBuffer(buffer) { + m_mappedPlainLogFile = nullptr; m_bufReader = m_hilogBuffer.CreateBufReader([this]() { NotifyNewLogAvailable(); }); } @@ -214,8 +215,11 @@ int LogPersister::Deinit() munmap(m_mappedPlainLogFile, MAX_PERSISTER_BUFFER_SIZE); std::cout << "Removing unmapped plain log file: " << m_plainLogFilePath << "\n"; if (remove(m_plainLogFilePath.c_str())) { + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); std::cerr << "File: " << m_plainLogFilePath << " can't be removed. " - << "Errno: " << errno << " " << strerror(errno) << "\n"; + << "Errno: " << errno << " " << buf << "\n"; } DeregisterLogPersister(shared_from_this()); @@ -230,9 +234,12 @@ int LogPersister::PrepareUncompressedFile(const std::string& parentPath, bool re m_plainLogFilePath = parentPath + "/" + fileName; FILE* plainTextFile = fopen(m_plainLogFilePath.c_str(), restore ? "r+" : "w+"); + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; if (!plainTextFile) { + strerror_r(errno, buf, bufSize); std::cerr << __PRETTY_FUNCTION__ << " Open uncompressed log file(" << m_plainLogFilePath << ") failed: " - << strerror(errno) << "\n"; + << buf << "\n"; return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } @@ -244,11 +251,13 @@ int LogPersister::PrepareUncompressedFile(const std::string& parentPath, bool re m_mappedPlainLogFile = (LogPersisterBuffer *)mmap(nullptr, sizeof(LogPersisterBuffer), PROT_READ | PROT_WRITE, MAP_SHARED, fileno(plainTextFile), 0); if (fclose(plainTextFile)) { + strerror_r(errno, buf, bufSize); std::cerr << "File: " << plainTextFile << " can't be closed. " - << "Errno: " << errno << " " << strerror(errno) << "\n"; + << "Errno: " << errno << " " << buf << "\n"; } if (m_mappedPlainLogFile == MAP_FAILED) { - std::cerr << __PRETTY_FUNCTION__ << " mmap file failed: " << strerror(errno) << "\n"; + strerror_r(errno, buf, bufSize); + std::cerr << __PRETTY_FUNCTION__ << " mmap file failed: " << buf << "\n"; return RET_FAIL; } if (restore == true) { diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index ec84747b13a8a52f6dd5b2116872cb105409b9d1..f4f43c34caa6f22ad5536ec195dbde1d6a16fc90 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -144,7 +144,10 @@ int HilogdEntry() if (fd > 0) { g_fd = dup2(fd, fileno(stdout)); } else { - std::cout << "open file error:" << strerror(errno) << std::endl; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); + std::cout << "open file error:" << buf << std::endl; } } #endif @@ -164,11 +167,14 @@ int HilogdEntry() logCollector.onDataRecv(cred, data); }; #endif - + HilogInputSocketServer incomingLogsServer(onDataReceive); if (incomingLogsServer.Init() < 0) { #ifdef DEBUG - cout << "Failed to init input server socket ! error=" << strerror(errno) << std::endl; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); + cout << "Failed to init input server socket ! error=" << buf << std::endl; #endif } else { if (chmod(INPUT_SOCKET, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) { @@ -190,7 +196,7 @@ int HilogdEntry() LogKmsg logKmsg(hilogBuffer); logKmsg.ReadAllKmsg(); }); - + auto cgroupWriteTask = std::async(std::launch::async, [&hilogBuffer]() { prctl(PR_SET_NAME, "hilogd.cgroup_set"); string myPid = to_string(getpid()); @@ -198,7 +204,7 @@ int HilogdEntry() WriteStringToFile(myPid, SYSTEM_BG_CPUSET); WriteStringToFile(myPid, SYSTEM_BG_BLKIO); }); - + CmdExecutor cmdExecutor(hilogBuffer); cmdExecutor.MainLoop(); return 0; diff --git a/services/hilogd/service_controller.cpp b/services/hilogd/service_controller.cpp index 673dd740c37cd4f06a881ef8bb3cdd0bafdb6179..c7982ab5750e19095917301d72e2d73a0d808672 100644 --- a/services/hilogd/service_controller.cpp +++ b/services/hilogd/service_controller.cpp @@ -286,7 +286,7 @@ void ServiceController::HandleBufferSizeRequest(const PacketBuf& rawData) int64_t buffLen = m_hilogBuffer.GetBuffLen(requestMsg->logType); if (pBuffSizeRst) { pBuffSizeRst->logType = requestMsg->logType; - pBuffSizeRst->buffSize = buffLen; + pBuffSizeRst->buffSize = static_cast(buffLen); pBuffSizeRst->result = (buffLen < 0) ? buffLen : RET_SUCCESS; pBuffSizeRst++; } diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index cb27e04b5362b7b741c0fdb598d72cee356b7cd4..eb336c681a4fd3285345f4bc1b10e30628b7490f 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -93,7 +93,7 @@ uint16_t GetLogType(const string& logTypeStr) uint64_t GetBuffSize(const string& buffSizeStr) { uint64_t index = buffSizeStr.size() - 1; - uint64_t buffSize; + long int buffSize; std::regex reg("[0-9]+[bBkKmMgGtT]?"); if (!std::regex_match(buffSizeStr, reg)) { std::cout << ParseErrorCode(ERR_BUFF_SIZE_INVALID) << std::endl; @@ -112,7 +112,7 @@ uint64_t GetBuffSize(const string& buffSizeStr) } else { buffSize = stol(buffSizeStr.substr(0, index + 1)); } - return buffSize; + return static_cast(buffSize); } uint16_t GetCompressAlg(const std::string& pressAlg) @@ -223,7 +223,10 @@ void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uin while(1) { std::fill_n(recvBuffer, bufLen, 0); if (controller.RecvMsg(recvBuffer, bufLen) == 0) { - fprintf(stderr, "Unexpected EOF %s\n", strerror(errno)); + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); + fprintf(stderr, "Unexpected EOF %s\n", buf); exit(1); return; } @@ -419,10 +422,10 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi } if (pLogPersistStartMsg->logType == (0b01 << LOG_KMSG)) { pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_KMSG_JOBID - : stoi(logPersistParam->jobIdStr); + : static_cast(stoi(logPersistParam->jobIdStr)); } else { pLogPersistStartMsg->jobId = (logPersistParam->jobIdStr == "") ? DEFAULT_JOBID - : stoi(logPersistParam->jobIdStr); + : static_cast(stoi(logPersistParam->jobIdStr)); } if (pLogPersistStartMsg->jobId <= 0) { cout << ParseErrorCode(ERR_LOG_PERSIST_JOBID_INVALID) << endl; @@ -433,7 +436,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi pLogPersistStartMsg->fileSize = (logPersistParam->fileSizeStr == "") ? fileSizeDefault : GetBuffSize( logPersistParam->fileSizeStr); pLogPersistStartMsg->fileNum = (logPersistParam->fileNumStr == "") ? fileNumDefault - : stoi(logPersistParam->fileNumStr); + : static_cast(stoi(logPersistParam->fileNumStr)); if (logPersistParam->fileNameStr.size() > FILE_PATH_MAX_LEN) { cout << ParseErrorCode(ERR_LOG_PERSIST_FILE_NAME_INVALID) << endl; return RET_FAIL; @@ -462,7 +465,7 @@ int32_t LogPersistOp(SeqPacketSocketClient& controller, uint8_t msgCmd, LogPersi return RET_FAIL; } for (iter = 0; iter < jobIdNum; iter++) { - pLogPersistStopMsg->jobId = stoi(vecJobId[iter]); + pLogPersistStopMsg->jobId = static_cast(stoi(vecJobId[iter])); pLogPersistStopMsg++; } SetMsgHead(&pLogPersistStopReq->msgHeader, msgCmd, sizeof(LogPersistStopMsg) * jobIdNum); @@ -603,14 +606,14 @@ int32_t SetPropertiesOp(SeqPacketSocketClient& controller, uint8_t operationType int MultiQuerySplit(const std::string& src, const char& delim, std::vector& vecSplit) { - int srcSize = src.length(); - int findPos = 0; - int getPos = 0; + auto srcSize = src.length(); + string::size_type findPos = 0; + string::size_type getPos = 0; vecSplit.clear(); while (getPos < srcSize) { findPos = src.find(delim, findPos); - if (-1 == findPos) { + if (string::npos == findPos) { if (getPos < srcSize) { vecSplit.push_back(src.substr(getPos, srcSize - getPos)); return 0; diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 3354e078fa6b9e21fd8eae45170b352cafa6779b..4e0a76a95536cd6350f047537d632efbcd6a3175 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -68,7 +68,7 @@ unordered_map errorMsg {ERR_KMSG_SWITCH_VALUE_INVALID, "Invalid kmsg switch value, valid:on/off"}, {ERR_LOG_FILE_NUM_INVALID, "Invalid log number, log number should be more than " + to_string(MIN_LOG_FILE_NUM) + " and less than " + to_string(MAX_LOG_FILE_NUM)}, -}; +}; string ParseErrorCode(ErrorCode errorCode) { @@ -476,7 +476,7 @@ void HilogShowLog(uint32_t showFormat, HilogDataMessage* data, const HilogArgs* showBuffer.tag_len = data->tag_len; showBuffer.tv_sec = data->tv_sec; showBuffer.tv_nsec = data->tv_nsec; - int offset = data->tag_len; + auto offset = data->tag_len; const char *dataBegin = data->data + offset; char *dataPos = data->data + offset; while (*dataPos != 0) { diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index d35e9208efb3d16aa5aafd4740487614afad926a..ceb493e7315da83b7ce0c24e9fa84342123450e1 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -115,7 +115,7 @@ static void Helper() ); } -static int GetTypes(HilogArgs context, const string& typesArgs, bool exclude = false) +static uint16_t GetTypes(HilogArgs context, const string& typesArgs, bool exclude = false) { uint16_t types = 0; if (exclude) { @@ -138,7 +138,7 @@ static int GetTypes(HilogArgs context, const string& typesArgs, bool exclude = f return types; } -static int GetLevels(HilogArgs context, const string& levelsArgs, bool exclude = false) +static uint16_t GetLevels(HilogArgs context, const string& levelsArgs, bool exclude = false) { uint16_t levels = 0; if (exclude) { @@ -230,10 +230,10 @@ int HilogEntry(int argc, char* argv[]) context.regexArgs = optarg; break; case 'a': - context.headLines = atoi(optarg); + context.headLines = static_cast(atoi(optarg)); break; case 'z': - context.tailLines = atoi(optarg); + context.tailLines = static_cast(atoi(optarg)); context.noBlockMode = 1; break; case 't': @@ -595,9 +595,12 @@ int HilogEntry(int argc, char* argv[]) char recvBuffer[RECV_BUF_LEN] = {0}; if (controller.RecvMsg(recvBuffer, RECV_BUF_LEN) == 0) { - fprintf(stderr, "Unexpected EOF %s\n", strerror(errno)); - exit(1); - return 0; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); + fprintf(stderr, "Unexpected EOF %s\n", buf); + exit(1); + return 0; } MessageHeader* msgHeader = reinterpret_cast(recvBuffer);