From 82a56a0302174fdcc2f90721297b93c9ff6a209d Mon Sep 17 00:00:00 2001 From: lyj_love_code Date: Mon, 28 Feb 2022 19:27:12 +0800 Subject: [PATCH 1/3] fix codex Signed-off-by: lyj_love_code --- frameworks/native/format.cpp | 13 +++++----- frameworks/native/hilog_base.cpp | 10 ++++---- .../native/hilog_input_socket_client.cpp | 9 +++---- frameworks/native/hilog_printf.cpp | 8 +++---- .../include/hilog_input_socket_client.h | 5 ++-- .../native/seq_packet_socket_server.cpp | 8 +++++-- frameworks/native/socket.cpp | 2 +- .../napi/src/hilog/src/hilog_napi_base.cpp | 4 ++-- services/hilogd/cmd_executor.cpp | 12 +++++++--- services/hilogd/flow_control_init.cpp | 8 +++---- services/hilogd/include/log_buffer.h | 6 ++--- services/hilogd/include/log_data.h | 4 +++- services/hilogd/kmsg_parser.cpp | 8 +++---- services/hilogd/log_buffer.cpp | 12 +++++----- services/hilogd/log_compress.cpp | 4 ++-- services/hilogd/log_kmsg.cpp | 5 +++- services/hilogd/log_persister.cpp | 18 ++++++++++---- services/hilogd/main.cpp | 16 +++++++++---- services/hilogd/service_controller.cpp | 2 +- services/hilogtool/log_controller.cpp | 24 ++++++++++--------- services/hilogtool/log_display.cpp | 4 ++-- services/hilogtool/main.cpp | 16 +++++++------ 22 files changed, 117 insertions(+), 81 deletions(-) diff --git a/frameworks/native/format.cpp b/frameworks/native/format.cpp index 5126e9f..64854dc 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 3d66f11..081fb1c 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 a7f57bc..ea62bb4 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 224b3ca..e6d523d 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 bd4c4a2..0bea240 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 8b2ae34..0793e7e 100644 --- a/frameworks/native/seq_packet_socket_server.cpp +++ b/frameworks/native/seq_packet_socket_server.cpp @@ -27,7 +27,9 @@ 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 }; + std::cerr << (strerror_r(listeningStatus, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; #endif return listeningStatus; } @@ -46,7 +48,9 @@ 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 e686ac3..e8a991b 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 78bfeb9..2fac852 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -58,7 +58,9 @@ void CmdExecutor::MainLoop() int listeningStatus = cmdServer.Listen(MAX_CLIENT_CONNECTIONS); if (listeningStatus < 0) { std::cerr << "Socket listen failed: " << listeningStatus << "\n"; - std::cerr << strerror(listeningStatus) << "\n"; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + std::cerr << (strerror_r(listeningStatus, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; return; } std::cout << "Server started to listen !\n"; @@ -74,7 +76,9 @@ void CmdExecutor::MainLoop() } else if (pollResult < 0) { int pollError = errno; std::cerr << "Socket polling error: " << pollError << "\n"; - std::cerr << strerror(pollError) << "\n"; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + std::cerr << (strerror_r(pollError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; break; } else if (pollResult != 1 || outEvent != POLLIN) { std::cerr << "Wrong poll result data." @@ -92,7 +96,9 @@ void CmdExecutor::MainLoop() } else { int acceptError = errno; std::cerr << "Socket accept failed: " << acceptError << "\n"; - std::cerr << strerror(acceptError) << "\n"; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + std::cerr << (strerror_r(acceptError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; break; } } diff --git a/services/hilogd/flow_control_init.cpp b/services/hilogd/flow_control_init.cpp index caf40b9..3d0540c 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 4e676ea..46919c1 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 1544646..2d1c649 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 c7f763e..8021c62 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 10eb040..b82d41c 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 78ac606..d64d4f5 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 2ddd955..7edf711 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 }; + std::cout << "Cannot open kmsg! Err=" << + (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << 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 bcd27da..71fcd6b 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,10 @@ 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 }; std::cerr << "File: " << m_plainLogFilePath << " can't be removed. " - << "Errno: " << errno << " " << strerror(errno) << "\n"; + << "Errno: " << errno << " " << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; } DeregisterLogPersister(shared_from_this()); @@ -231,8 +234,10 @@ int LogPersister::PrepareUncompressedFile(const std::string& parentPath, bool re FILE* plainTextFile = fopen(m_plainLogFilePath.c_str(), restore ? "r+" : "w+"); if (!plainTextFile) { + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; std::cerr << __PRETTY_FUNCTION__ << " Open uncompressed log file(" << m_plainLogFilePath << ") failed: " - << strerror(errno) << "\n"; + << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } @@ -244,11 +249,16 @@ 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)) { + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; std::cerr << "File: " << plainTextFile << " can't be closed. " - << "Errno: " << errno << " " << strerror(errno) << "\n"; + << "Errno: " << errno << " " << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; } if (m_mappedPlainLogFile == MAP_FAILED) { - std::cerr << __PRETTY_FUNCTION__ << " mmap file failed: " << strerror(errno) << "\n"; + constexpr int bufSize = 1024; + char buf[bufSize] = { 0 }; + std::cerr << __PRETTY_FUNCTION__ << " mmap file failed: " + << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; return RET_FAIL; } if (restore == true) { diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index ec84747..3c1a60a 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 }; + std::cout << "open file error:" << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") + << 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 }; + cout << "Failed to init input server socket ! error=" + << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << 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 673dd74..c7982ab 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 cb27e04..66ac7a4 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,9 @@ 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 }; + fprintf(stderr, "Unexpected EOF %s\n", strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno"); exit(1); return; } @@ -419,10 +421,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 +435,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 +464,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 +605,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 3354e07..4e0a76a 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 d35e920..7eca765 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,11 @@ 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 }; + fprintf(stderr, "Unexpected EOF %s\n", strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno"); + exit(1); + return 0; } MessageHeader* msgHeader = reinterpret_cast(recvBuffer); -- Gitee From 8d7e0ba8d6804322368bc36680f8ab3f745ed7f9 Mon Sep 17 00:00:00 2001 From: lyj_love_code Date: Mon, 28 Feb 2022 19:49:29 +0800 Subject: [PATCH 2/3] fix codex Signed-off-by: lyj_love_code --- services/hilogd/cmd_executor.cpp | 8 ++------ services/hilogtool/main.cpp | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index 2fac852..a4e66e7 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -56,10 +56,10 @@ 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"; - constexpr int bufSize = 1024; - char buf[bufSize] = { 0 }; std::cerr << (strerror_r(listeningStatus, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; return; } @@ -76,8 +76,6 @@ void CmdExecutor::MainLoop() } else if (pollResult < 0) { int pollError = errno; std::cerr << "Socket polling error: " << pollError << "\n"; - constexpr int bufSize = 1024; - char buf[bufSize] = { 0 }; std::cerr << (strerror_r(pollError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; break; } else if (pollResult != 1 || outEvent != POLLIN) { @@ -96,8 +94,6 @@ void CmdExecutor::MainLoop() } else { int acceptError = errno; std::cerr << "Socket accept failed: " << acceptError << "\n"; - constexpr int bufSize = 1024; - char buf[bufSize] = { 0 }; std::cerr << (strerror_r(acceptError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; break; } diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 7eca765..0d7c6cd 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -597,7 +597,7 @@ int HilogEntry(int argc, char* argv[]) if (controller.RecvMsg(recvBuffer, RECV_BUF_LEN) == 0) { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - fprintf(stderr, "Unexpected EOF %s\n", strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno"); + fprintf(stderr, "Unexpected EOF %s\n", (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno")); exit(1); return 0; } -- Gitee From 1994bd7875fef30c91c540514b6698914c088c10 Mon Sep 17 00:00:00 2001 From: lyj_love_code Date: Mon, 28 Feb 2022 21:17:06 +0800 Subject: [PATCH 3/3] fix codex Signed-off-by: lyj_love_code --- .../native/seq_packet_socket_server.cpp | 6 ++++-- services/hilogd/cmd_executor.cpp | 9 +++++--- services/hilogd/log_kmsg.cpp | 4 ++-- services/hilogd/log_persister.cpp | 21 +++++++++---------- services/hilogd/main.cpp | 8 +++---- services/hilogtool/log_controller.cpp | 3 ++- services/hilogtool/main.cpp | 3 ++- 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/frameworks/native/seq_packet_socket_server.cpp b/frameworks/native/seq_packet_socket_server.cpp index 0793e7e..5b6a195 100644 --- a/frameworks/native/seq_packet_socket_server.cpp +++ b/frameworks/native/seq_packet_socket_server.cpp @@ -29,7 +29,8 @@ int SeqPacketSocketServer::StartAcceptingConnection(AcceptingHandler onAccepted) std::cerr << "Socket listen failed: " << listeningStatus << "\n"; constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - std::cerr << (strerror_r(listeningStatus, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + strerror_r(listeningStatus, buf, bufSize); + std::cerr << buf << "\n"; #endif return listeningStatus; } @@ -50,7 +51,8 @@ int SeqPacketSocketServer::AcceptingLoop(AcceptingHandler func) std::cerr << "Socket accept failed: " << acceptError << "\n"; constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - std::cerr << (strerror_r(acceptError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + strerror_r(acceptError, buf, bufSize); + std::cerr << buf << "\n"; #endif return acceptError; diff --git a/services/hilogd/cmd_executor.cpp b/services/hilogd/cmd_executor.cpp index a4e66e7..6fe607c 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -60,7 +60,8 @@ void CmdExecutor::MainLoop() char buf[bufSize] = { 0 }; if (listeningStatus < 0) { std::cerr << "Socket listen failed: " << listeningStatus << "\n"; - std::cerr << (strerror_r(listeningStatus, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + strerror_r(listeningStatus, buf, bufSize); + std::cerr << buf << "\n"; return; } std::cout << "Server started to listen !\n"; @@ -76,7 +77,8 @@ void CmdExecutor::MainLoop() } else if (pollResult < 0) { int pollError = errno; std::cerr << "Socket polling error: " << pollError << "\n"; - std::cerr << (strerror_r(pollError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + strerror_r(pollError, buf, bufSize); + std::cerr << buf << "\n"; break; } else if (pollResult != 1 || outEvent != POLLIN) { std::cerr << "Wrong poll result data." @@ -94,7 +96,8 @@ void CmdExecutor::MainLoop() } else { int acceptError = errno; std::cerr << "Socket accept failed: " << acceptError << "\n"; - std::cerr << (strerror_r(acceptError, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + strerror_r(acceptError, buf, bufSize); + std::cerr << buf << "\n"; break; } } diff --git a/services/hilogd/log_kmsg.cpp b/services/hilogd/log_kmsg.cpp index 7edf711..e260501 100644 --- a/services/hilogd/log_kmsg.cpp +++ b/services/hilogd/log_kmsg.cpp @@ -59,8 +59,8 @@ int LogKmsg::LinuxReadAllKmsg() if (kmsgCtl < 0) { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - std::cout << "Cannot open kmsg! Err=" << - (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << std::endl; + 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 71fcd6b..f77da8d 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -217,8 +217,9 @@ int LogPersister::Deinit() 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_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + << "Errno: " << errno << " " << buf << "\n"; } DeregisterLogPersister(shared_from_this()); @@ -233,11 +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) { - constexpr int bufSize = 1024; - char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); std::cerr << __PRETTY_FUNCTION__ << " Open uncompressed log file(" << m_plainLogFilePath << ") failed: " - << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + << buf << "\n"; return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } @@ -249,16 +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)) { - constexpr int bufSize = 1024; - char buf[bufSize] = { 0 }; + strerror_r(errno, buf, bufSize); std::cerr << "File: " << plainTextFile << " can't be closed. " - << "Errno: " << errno << " " << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << "\n"; + << "Errno: " << errno << " " << buf << "\n"; } if (m_mappedPlainLogFile == MAP_FAILED) { - constexpr int bufSize = 1024; - char buf[bufSize] = { 0 }; - std::cerr << __PRETTY_FUNCTION__ << " mmap file failed: " - << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown 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 3c1a60a..f4f43c3 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -146,8 +146,8 @@ int HilogdEntry() } else { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - std::cout << "open file error:" << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") - << std::endl; + strerror_r(errno, buf, bufSize); + std::cout << "open file error:" << buf << std::endl; } } #endif @@ -173,8 +173,8 @@ int HilogdEntry() #ifdef DEBUG constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - cout << "Failed to init input server socket ! error=" - << (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno") << std::endl; + 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) { diff --git a/services/hilogtool/log_controller.cpp b/services/hilogtool/log_controller.cpp index 66ac7a4..eb336c6 100644 --- a/services/hilogtool/log_controller.cpp +++ b/services/hilogtool/log_controller.cpp @@ -225,7 +225,8 @@ void LogQueryResponseOp(SeqPacketSocketClient& controller, char* recvBuffer, uin if (controller.RecvMsg(recvBuffer, bufLen) == 0) { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - fprintf(stderr, "Unexpected EOF %s\n", strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno"); + strerror_r(errno, buf, bufSize); + fprintf(stderr, "Unexpected EOF %s\n", buf); exit(1); return; } diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index 0d7c6cd..ceb493e 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -597,7 +597,8 @@ int HilogEntry(int argc, char* argv[]) if (controller.RecvMsg(recvBuffer, RECV_BUF_LEN) == 0) { constexpr int bufSize = 1024; char buf[bufSize] = { 0 }; - fprintf(stderr, "Unexpected EOF %s\n", (strerror_r(errno, buf, bufSize) == 0 ? buf : "Unknown errno")); + strerror_r(errno, buf, bufSize); + fprintf(stderr, "Unexpected EOF %s\n", buf); exit(1); return 0; } -- Gitee