From 6df59a9a1a05ab7b9104f85d975f4cde674b104c Mon Sep 17 00:00:00 2001 From: buzhenwang Date: Thu, 7 Aug 2025 17:59:21 +0800 Subject: [PATCH] modify code check Signed-off-by: buzhenwang --- .../libhilog/param/include/properties.h | 1 + frameworks/libhilog/param/properties.cpp | 5 ++ interfaces/ets/ani/hilog/include/ani_util.h | 6 +-- .../ets/ani/hilog/include/hilog_ani_base.h | 8 +-- interfaces/ets/ani/hilog/src/ani_util.cpp | 4 +- .../ets/ani/hilog/src/hilog_ani_base.cpp | 52 ++++++++----------- .../napi/src/hilog/src/hilog_napi_base.cpp | 29 +++++------ interfaces/native/innerkits/libhilog.map | 1 + 8 files changed, 51 insertions(+), 55 deletions(-) diff --git a/frameworks/libhilog/param/include/properties.h b/frameworks/libhilog/param/include/properties.h index afade48..68c5711 100644 --- a/frameworks/libhilog/param/include/properties.h +++ b/frameworks/libhilog/param/include/properties.h @@ -28,6 +28,7 @@ extern "C" { #endif bool IsDebugOn(); +bool IsPrivateModeEnable(); bool IsPrivateSwitchOn(); #ifdef __cplusplus diff --git a/frameworks/libhilog/param/properties.cpp b/frameworks/libhilog/param/properties.cpp index cf3d7e9..0c5d9b0 100644 --- a/frameworks/libhilog/param/properties.cpp +++ b/frameworks/libhilog/param/properties.cpp @@ -340,6 +340,11 @@ bool IsDebuggableHap() return true; } +bool IsPrivateModeEnable() +{ + return !IsDebugOn() && !IsDebuggableHap() && IsPrivateSwitchOn(); +} + uint16_t GetGlobalLogLevel() { uint16_t globalLevel = GetGlobalLevel(); diff --git a/interfaces/ets/ani/hilog/include/ani_util.h b/interfaces/ets/ani/hilog/include/ani_util.h index d136c27..78dc581 100644 --- a/interfaces/ets/ani/hilog/include/ani_util.h +++ b/interfaces/ets/ani/hilog/include/ani_util.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef ANI_UTIL_H -#define ANI_UTIL_H +#ifndef HILOG_ANI_UTIL_H +#define HILOG_ANI_UTIL_H #include #include @@ -45,4 +45,4 @@ public: } // namespace HiviewDFX } // namespace OHOS -#endif //ANI_UTIL_H +#endif //HILOG_ANI_UTIL_H diff --git a/interfaces/ets/ani/hilog/include/hilog_ani_base.h b/interfaces/ets/ani/hilog/include/hilog_ani_base.h index 89445c6..6a9795d 100644 --- a/interfaces/ets/ani/hilog/include/hilog_ani_base.h +++ b/interfaces/ets/ani/hilog/include/hilog_ani_base.h @@ -26,9 +26,9 @@ using AniParam = struct { std::string val; }; -typedef struct { - uint32_t pos; - uint32_t count; +typedef struct LogContentPosition { + uint32_t pos = 0; + uint32_t count = 0; } LogContentPosition; class HilogAniBase { @@ -44,7 +44,7 @@ public: static void SetMinLogLevel(ani_env *env, ani_enum_item level); private: - static void parseAniValue(ani_env *env, ani_ref element, std::vector ¶ms); + static void ParseAniValue(ani_env *env, ani_ref element, std::vector ¶ms); }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/ets/ani/hilog/src/ani_util.cpp b/interfaces/ets/ani/hilog/src/ani_util.cpp index 24952cb..1388336 100644 --- a/interfaces/ets/ani/hilog/src/ani_util.cpp +++ b/interfaces/ets/ani/hilog/src/ani_util.cpp @@ -13,8 +13,11 @@ * limitations under the License. */ +#include "ani_util.h" + #include #include + #include "hilog/log.h" #include "hilog/log_c.h" #include "hilog/log_cpp.h" @@ -22,7 +25,6 @@ #include "properties.h" #include "securec.h" -#include "ani_util.h" namespace OHOS { namespace HiviewDFX { const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOG_ANI_UTIL" }; diff --git a/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp b/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp index f3c94e5..9b82dc2 100644 --- a/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp +++ b/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp @@ -13,8 +13,11 @@ * limitations under the License. */ +#include "hilog_ani_base.h" + #include #include + #include "hilog/log.h" #include "hilog/log_c.h" #include "hilog/log_cpp.h" @@ -22,8 +25,6 @@ #include "properties.h" #include "securec.h" -#include "hilog_ani_base.h" - namespace OHOS { namespace HiviewDFX { const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOG_ETS" }; @@ -45,7 +46,6 @@ static void HandleFormatFlags(const std::string& formatStr, uint32_t& pos, bool& formatStr.substr(pos + PROPERTY_POS, PRIVATE_LEN) == "private") { pos += (PRIVATE_LEN + PROPERTY_POS); } - return; } static void ProcessLogContent(const std::string &formatStr, const std::vector ¶ms, bool isPriv, @@ -62,7 +62,7 @@ static void ProcessLogContent(const std::string &formatStr, const std::vector& params, std::string& logContent) { - std::string& ret = logContent; if (params.empty()) { - ret += formatStr; + logContent += formatStr; return; } auto size = params.size(); auto len = formatStr.size(); LogContentPosition contentPos; - contentPos.pos = 0; - contentPos.count = 0; - bool debug = true; + bool isPrivateEnable = true; #if not (defined(__WINDOWS__) || defined(__MAC__) || defined(__LINUX__)) - debug = IsDebugOn(); + isPrivateEnable = IsPrivateModeEnable(); #endif - bool priv = (!debug) && IsPrivateSwitchOn(); for (; contentPos.pos < len; ++contentPos.pos) { bool showPriv = true; if (contentPos.count >= size) { break; } if (formatStr[contentPos.pos] != '%') { - ret += formatStr[contentPos.pos]; + logContent += formatStr[contentPos.pos]; continue; } HandleFormatFlags(formatStr, contentPos.pos, showPriv); - bool isPriv = priv && showPriv; - ProcessLogContent(formatStr, params, isPriv, contentPos, ret); + bool isPriv = isPrivateEnable && showPriv; + ProcessLogContent(formatStr, params, isPriv, contentPos, logContent); } if (contentPos.pos < len) { - ret += formatStr.substr(contentPos.pos, len - contentPos.pos); + logContent += formatStr.substr(contentPos.pos, len - contentPos.pos); } - return; } void HilogAniBase::HilogImpl(ani_env *env, ani_double domain, ani_string tag, ani_string format, ani_array args, int level, bool isAppLog) { - int32_t domainVal = static_cast(domain); - std::string tagString = AniUtil::AniStringToStdString(env, tag); - std::string fmtString = AniUtil::AniStringToStdString(env, format); - ani_size length = 0; if (ANI_OK != env->Array_GetLength(args, &length)) { - HiLog::Info(LABEL, "Get array length failed"); + HiLog::Warn(LABEL, "Get array length failed"); return; } if (MIN_NUMBER > length || MAX_NUMBER < length) { - HiLog::Info(LABEL, "%{public}s", "Argc mismatch"); + HiLog::Warn(LABEL, "Argc mismatch, length:%{public}zu", length); return; } std::string logContent; @@ -151,18 +141,20 @@ void HilogAniBase::HilogImpl(ani_env *env, ani_double domain, ani_string tag, for (ani_size i = 0; i < length; i++) { ani_ref element; if (ANI_OK != env->Array_Get_Ref(static_cast(args), i, &element)) { - HiLog::Info(LABEL, "Get element at index %{public}zu from array failed", i); + HiLog::Warn(LABEL, "Get element at index %{public}zu from array failed", i); return; } - parseAniValue(env, element, params); + ParseAniValue(env, element, params); } + std::string fmtString = AniUtil::AniStringToStdString(env, format); ParseLogContent(fmtString, params, logContent); + int32_t domainVal = static_cast(domain); + std::string tagString = AniUtil::AniStringToStdString(env, tag); HiLogPrint((isAppLog ? LOG_APP : LOG_CORE), static_cast(level), domainVal, tagString.c_str(), "%{public}s", logContent.c_str()); - return; } -void HilogAniBase::parseAniValue(ani_env *env, ani_ref element, std::vector& params) +void HilogAniBase::ParseAniValue(ani_env *env, ani_ref element, std::vector& params) { AniParam res; AniArgsType type = AniUtil::AniArgGetType(env, static_cast(element)); @@ -177,7 +169,6 @@ void HilogAniBase::parseAniValue(ani_env *env, ani_ref element, std::vector(levelVal)); - return; } } // namespace HiviewDFX } // namespace OHOS 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 4de4ba4..7bbfab2 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 @@ -40,27 +40,25 @@ static const string PRIV_STR = ""; void ParseLogContent(string& formatStr, vector& params, string& logContent) { - string& ret = logContent; if (params.empty()) { - ret += formatStr; + logContent += formatStr; return; } auto size = params.size(); auto len = formatStr.size(); uint32_t pos = 0; uint32_t count = 0; - bool debug = true; + bool isPrivateEnable = true; #if not (defined(__WINDOWS__) || defined(__MAC__) || defined(__LINUX__)) - debug = IsDebugOn(); + isPrivateEnable = IsPrivateModeEnable(); #endif - bool priv = (!debug) && IsPrivateSwitchOn(); for (; pos < len; ++pos) { bool showPriv = true; if (count >= size) { break; } if (formatStr[pos] != '%') { - ret += formatStr[pos]; + logContent += formatStr[pos]; continue; } @@ -80,41 +78,40 @@ void ParseLogContent(string& formatStr, vector& params, string& logCo case 'd': case 'i': if (params[count].type == napi_number || params[count].type == napi_bigint) { - ret += (priv && showPriv) ? PRIV_STR : params[count].val; + logContent += (isPrivateEnable && showPriv) ? PRIV_STR : params[count].val; } - count++; + ++count; ++pos; break; case 's': if (params[count].type == napi_string || params[count].type == napi_undefined || params[count].type == napi_boolean || params[count].type == napi_null) { - ret += (priv && showPriv) ? PRIV_STR : params[count].val; + logContent += (isPrivateEnable && showPriv) ? PRIV_STR : params[count].val; } - count++; + ++count; ++pos; break; case 'O': case 'o': if (params[count].type == napi_object || params[count].type == napi_function || params[count].type == napi_undefined || params[count].type == napi_null) { - ret += (priv && showPriv) ? PRIV_STR : params[count].val; + logContent += (isPrivateEnable && showPriv) ? PRIV_STR : params[count].val; } - count++; + ++count; ++pos; break; case '%': - ret += formatStr[pos]; + logContent += formatStr[pos]; ++pos; break; default: - ret += formatStr[pos]; + logContent += formatStr[pos]; break; } } if (pos < len) { - ret += formatStr.substr(pos, len - pos); + logContent += formatStr.substr(pos, len - pos); } - return; } napi_value HilogNapiBase::IsLoggable(napi_env env, napi_callback_info info) diff --git a/interfaces/native/innerkits/libhilog.map b/interfaces/native/innerkits/libhilog.map index 023acf7..7f844b4 100644 --- a/interfaces/native/innerkits/libhilog.map +++ b/interfaces/native/innerkits/libhilog.map @@ -4,6 +4,7 @@ HiLogPrintArgs; HiLogPrint; HiLogIsLoggable; + IsPrivateModeEnable; IsPrivateSwitchOn; IsDebugOn; HiLogRegisterGetIdFun; -- Gitee