From c5c070dd9f614ef362e07977c17c6af1c6b06567 Mon Sep 17 00:00:00 2001 From: lyj_love_code Date: Mon, 28 Feb 2022 11:47:48 +0800 Subject: [PATCH] fix codex --- frameworks/native/format.cpp | 13 +++++----- frameworks/native/hilog_base.cpp | 10 ++++---- .../native/hilog_input_socket_client.cpp | 8 +++---- frameworks/native/hilog_printf.cpp | 8 +++---- .../include/hilog_input_socket_client.h | 3 ++- .../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 | 4 +++- services/hilogd/flow_control_init.cpp | 8 +++---- services/hilogd/include/log_buffer.h | 6 ++--- services/hilogd/include/log_data.h | 2 +- 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 | 1 + services/hilogd/service_controller.cpp | 2 +- services/hilogtool/log_controller.cpp | 24 ++++++++++--------- services/hilogtool/log_display.cpp | 4 ++-- services/hilogtool/main.cpp | 16 +++++++------ 21 files changed, 83 insertions(+), 69 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..d581c53 100644 --- a/frameworks/native/hilog_input_socket_client.cpp +++ b/frameworks/native/hilog_input_socket_client.cpp @@ -22,13 +22,13 @@ 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 +38,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..5c55515 100644 --- a/frameworks/native/include/hilog_input_socket_client.h +++ b/frameworks/native/include/hilog_input_socket_client.h @@ -30,5 +30,6 @@ public: } // 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..fb18dcf 100644 --- a/services/hilogd/cmd_executor.cpp +++ b/services/hilogd/cmd_executor.cpp @@ -92,7 +92,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..bd05c9b 100644 --- a/services/hilogd/include/log_data.h +++ b/services/hilogd/include/log_data.h @@ -77,7 +77,7 @@ struct HilogData { HilogData(HilogData&& cpy) { - std::memcpy(this, &cpy, sizeof(HilogData)); + std::memcpy_s(this, sizeof(HilogData), &cpy, sizeof(HilogData)); 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 79d36ae..b0a6de2 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..97c636c 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(acceptError, 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..4466b9e 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(); }); } diff --git a/services/hilogd/service_controller.cpp b/services/hilogd/service_controller.cpp index 38c9435..2d740d8 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