From 920d1ecc2ea0675588e73c547478c1e4726f82b1 Mon Sep 17 00:00:00 2001 From: buzhenwang Date: Tue, 2 Sep 2025 20:00:26 +0800 Subject: [PATCH] add SetLogLevel Signed-off-by: buzhenwang --- frameworks/hilog_ndk/hilog_ndk.c | 5 +++ frameworks/libhilog/hilog_printf.cpp | 15 +++++++-- interfaces/ets/ani/hilog/ets/@ohos.hilog.ets | 8 +++++ .../ets/ani/hilog/include/hilog_ani_base.h | 3 +- interfaces/ets/ani/hilog/src/hilog_ani.cpp | 1 + .../ets/ani/hilog/src/hilog_ani_base.cpp | 14 ++++++++ .../hilog/include/context/hilog_napi_base.h | 1 + .../js/kits/napi/src/hilog/src/hilog_napi.cpp | 28 ++++++++++++++-- .../napi/src/hilog/src/hilog_napi_base.cpp | 22 +++++++++++++ .../native/innerkits/include/hilog/log_c.h | 27 +++++++++++++++ interfaces/native/innerkits/libhilog.map | 1 + interfaces/native/kits/include/hilog/log.h | 33 +++++++++++++++++++ interfaces/native/kits/libhilog.ndk.json | 4 +++ test/moduletest/common/hilog_ndk_z_test.cpp | 29 ++++++++++++++++ 14 files changed, 186 insertions(+), 5 deletions(-) diff --git a/frameworks/hilog_ndk/hilog_ndk.c b/frameworks/hilog_ndk/hilog_ndk.c index d5d4806..41e694c 100644 --- a/frameworks/hilog_ndk/hilog_ndk.c +++ b/frameworks/hilog_ndk/hilog_ndk.c @@ -76,3 +76,8 @@ void OH_LOG_SetMinLogLevel(LogLevel level) { return HiLogSetAppMinLogLevel(level); } + +void OH_LOG_SetLogLevel(LogLevel level, PreferStrategy prefer) +{ + return HiLogSetAppLogLevel(level, prefer); +} \ No newline at end of file diff --git a/frameworks/libhilog/hilog_printf.cpp b/frameworks/libhilog/hilog_printf.cpp index 06a2f2f..4a7cb65 100644 --- a/frameworks/libhilog/hilog_printf.cpp +++ b/frameworks/libhilog/hilog_printf.cpp @@ -57,6 +57,7 @@ using namespace OHOS::HiviewDFX; static RegisterFunc g_registerFunc = nullptr; static LogCallback g_logCallback = nullptr; static int g_logLevel = LOG_LEVEL_MIN; +static int g_preferStrategy = UNSET_LOGLEVEL; static atomic_int g_hiLogGetIdCallCount = 0; // protected by static lock guard static char g_hiLogLastFatalMessage[MAX_LOG_LEN] = { 0 }; // MAX_lOG_LEN : 1024 @@ -96,8 +97,14 @@ void LOG_SetCallback(LogCallback callback) } void HiLogSetAppMinLogLevel(LogLevel level) +{ + HiLogSetAppLogLevel(level, PREFER_CLOSE_LOG); +} + +void HiLogSetAppLogLevel(LogLevel level, PreferStrategy prefer) { g_logLevel = level; + g_preferStrategy = prefer; } static uint16_t GetFinalLevel(unsigned int domain, const std::string& tag) @@ -390,8 +397,12 @@ static bool IsAppDomain(const unsigned int domain) bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level) { - if (IsAppDomain(domain) && level < g_logLevel) { - return false; + if (IsAppDomain(domain) && g_preferStrategy != UNSET_LOGLEVEL) { + if (g_preferStrategy == PREFER_CLOSE_LOG && level < g_logLevel) { + return false; + } else if (g_preferStrategy == PREFER_OPEN_LOG && level >= g_logLevel) { + return true; + } } if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || (tag == nullptr) || (domain >= DOMAIN_OS_MAX)) { return false; diff --git a/interfaces/ets/ani/hilog/ets/@ohos.hilog.ets b/interfaces/ets/ani/hilog/ets/@ohos.hilog.ets index 15cb6a7..7d911e4 100644 --- a/interfaces/ets/ani/hilog/ets/@ohos.hilog.ets +++ b/interfaces/ets/ani/hilog/ets/@ohos.hilog.ets @@ -37,6 +37,8 @@ export default namespace hilog { export native function setMinLogLevel(level: LogLevel): void; + export native function setLogLevel(level: LogLevel, prefer: PreferStrategy): void; + export enum LogLevel { DEBUG = 3, INFO = 4, @@ -44,4 +46,10 @@ export default namespace hilog { ERROR = 6, FATAL = 7 } + + export enum PreferStrategy { + UNSET_LOGLEVEL = 0, + PREFER_CLOSE_LOG = 1, + PREFER_OPEN_LOG = 2 + } } diff --git a/interfaces/ets/ani/hilog/include/hilog_ani_base.h b/interfaces/ets/ani/hilog/include/hilog_ani_base.h index 6a9795d..2d02f14 100644 --- a/interfaces/ets/ani/hilog/include/hilog_ani_base.h +++ b/interfaces/ets/ani/hilog/include/hilog_ani_base.h @@ -42,7 +42,8 @@ public: static void Fatal(ani_env *env, ani_double domain, ani_string tag, ani_string format, ani_array args); static ani_boolean IsLoggable(ani_env *env, ani_double domain, ani_string tag, ani_enum_item level); static void SetMinLogLevel(ani_env *env, ani_enum_item level); - + static void SetLogLevel(ani_env *env, ani_enum_item level, ani_enum_item prefer); + private: static void ParseAniValue(ani_env *env, ani_ref element, std::vector ¶ms); }; diff --git a/interfaces/ets/ani/hilog/src/hilog_ani.cpp b/interfaces/ets/ani/hilog/src/hilog_ani.cpp index 156f2fa..a050608 100644 --- a/interfaces/ets/ani/hilog/src/hilog_ani.cpp +++ b/interfaces/ets/ani/hilog/src/hilog_ani.cpp @@ -40,6 +40,7 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) ani_native_function {"fatal", nullptr, reinterpret_cast(HilogAniBase::Fatal)}, ani_native_function {"isLoggable", nullptr, reinterpret_cast(HilogAniBase::IsLoggable)}, ani_native_function {"setMinLogLevel", nullptr, reinterpret_cast(HilogAniBase::SetMinLogLevel)}, + ani_native_function {"setLogLevel", nullptr, reinterpret_cast(HilogAniBase::SetLogLevel)}, }; if (ANI_OK != env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size())) { diff --git a/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp b/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp index 9b82dc2..68c656c 100644 --- a/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp +++ b/interfaces/ets/ani/hilog/src/hilog_ani_base.cpp @@ -225,5 +225,19 @@ void HilogAniBase::SetMinLogLevel(ani_env *env, ani_enum_item level) } HiLogSetAppMinLogLevel(static_cast(levelVal)); } + +void HilogAniBase::SetLogLevel(ani_env *env, ani_enum_item level, ani_enum_item prefer) +{ + int32_t levelVal = LOG_LEVEL_MIN; + if (!AniUtil::AniEnumToInt32(env, level, levelVal)) { + return; + } + int32_t preferVal = UNSET_LOGLEVEL; + if (!AniUtil::AniEnumToInt32(env, prefer, preferVal)) { + return; + } + + HiLogSetAppLogLevel(static_cast(levelVal), static_cast(preferVal)); +} } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h index 5e59f3c..371e4ac 100644 --- a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h +++ b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h @@ -43,6 +43,7 @@ public: static napi_value SysLogFatal(napi_env env, napi_callback_info info); static napi_value IsLoggable(napi_env env, napi_callback_info info); static napi_value SetMinLogLevel(napi_env env, napi_callback_info info); + static napi_value SetLogLevel(napi_env env, napi_callback_info info); private: static napi_value parseNapiValue(napi_env env, napi_callback_info info, napi_value element, std::vector& params); diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp index 9349a94..ffbf1e1 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp @@ -27,7 +27,7 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -napi_value LogLevelTypeConstructor(napi_env env, napi_callback_info info) +napi_value TypeConstructor(napi_env env, napi_callback_info info) { napi_value thisArg = nullptr; void* data = nullptr; @@ -62,15 +62,38 @@ static void LogLevelTypeEnumInit(napi_env env, napi_value exports) }; napi_value result = nullptr; - napi_define_class(env, "LogLevel", NAPI_AUTO_LENGTH, LogLevelTypeConstructor, nullptr, + napi_define_class(env, "LogLevel", NAPI_AUTO_LENGTH, TypeConstructor, nullptr, sizeof(descriptors) / sizeof(*descriptors), descriptors, &result); napi_set_named_property(env, exports, "LogLevel", result); } +static void PreferStrategyTypeEnumInit(napi_env env, napi_value exports) +{ + napi_value UNSET_LOGLEVEL = nullptr; + napi_value PREFER_CLOSE_LOG = nullptr; + napi_value PREFER_OPEN_LOG = nullptr; + napi_create_int32(env, PreferStrategy::UNSET_LOGLEVEL, &UNSET_LOGLEVEL); + napi_create_int32(env, PreferStrategy::PREFER_CLOSE_LOG, &PREFER_CLOSE_LOG); + napi_create_int32(env, PreferStrategy::PREFER_OPEN_LOG, &PREFER_OPEN_LOG); + + napi_property_descriptor descriptors[] = { + NVal::DeclareNapiStaticProperty("UNSET_LOGLEVEL", UNSET_LOGLEVEL), + NVal::DeclareNapiStaticProperty("PREFER_CLOSE_LOG", PREFER_CLOSE_LOG), + NVal::DeclareNapiStaticProperty("PREFER_OPEN_LOG", PREFER_OPEN_LOG), + }; + + napi_value result = nullptr; + napi_define_class(env, "PreferStrategy", NAPI_AUTO_LENGTH, TypeConstructor, nullptr, + sizeof(descriptors) / sizeof(*descriptors), descriptors, &result); + + napi_set_named_property(env, exports, "PreferStrategy", result); +} + bool HilogNapi::Export(napi_env env, napi_value exports) { LogLevelTypeEnumInit(env, exports); + PreferStrategyTypeEnumInit(env, exports); return exports_.AddProp({ NVal::DeclareNapiFunction("debug", HilogNapiBase::Debug), NVal::DeclareNapiFunction("info", HilogNapiBase::Info), @@ -84,6 +107,7 @@ bool HilogNapi::Export(napi_env env, napi_value exports) NVal::DeclareNapiFunction("sLogF", HilogNapiBase::SysLogFatal), NVal::DeclareNapiFunction("isLoggable", HilogNapiBase::IsLoggable), NVal::DeclareNapiFunction("setMinLogLevel", HilogNapiBase::SetMinLogLevel), + NVal::DeclareNapiFunction("setLogLevel", HilogNapiBase::SetLogLevel), }); } 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 7bbfab2..92ae958 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 @@ -160,6 +160,28 @@ napi_value HilogNapiBase::SetMinLogLevel(napi_env env, napi_callback_info info) return nullptr; } +napi_value HilogNapiBase::SetLogLevel(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + return nullptr; + } + bool succ = false; + int32_t level = LOG_LEVEL_MIN; + tie(succ, level) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + return nullptr; + } + int32_t prefer = UNSET_LOGLEVEL; + tie(succ, prefer) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!succ) { + return nullptr; + } + + HiLogSetAppLogLevel(static_cast(level), static_cast(prefer)); + return nullptr; +} + napi_value HilogNapiBase::Debug(napi_env env, napi_callback_info info) { return HilogImpl(env, info, LOG_DEBUG, true); diff --git a/interfaces/native/innerkits/include/hilog/log_c.h b/interfaces/native/innerkits/include/hilog/log_c.h index a1982af..8008035 100644 --- a/interfaces/native/innerkits/include/hilog/log_c.h +++ b/interfaces/native/innerkits/include/hilog/log_c.h @@ -75,6 +75,24 @@ typedef enum { LOG_LEVEL_MAX, } LogLevel; +/* Preference strategy to be used in HiLogSetAppLogLevel */ +typedef enum { + /** Used to unset SetLogLevel, then none is set + */ + UNSET_LOGLEVEL = 0, + /** + * The actual lowest log level is determined by + * the maximum level between the new level and the system-controlled level. + * This is equivalent to calling OH_LOG_SetMinLogLevel. + */ + PREFER_CLOSE_LOG = 1, + /** + * The actual lowest log level is determined by + * the minimum level between the new level and the system-controlled level. + */ + PREFER_OPEN_LOG = 2, +} PreferStrategy; + /** * @brief Get log of fatal level */ @@ -178,6 +196,15 @@ void LOG_SetCallback(LogCallback callback); */ void HiLogSetAppMinLogLevel(LogLevel level); +/** + * Sets the lowest log level of the current application process. Different preference strategy can be set. + * + * @param level log level. + * @param prefer preference strategy. See {@link PreferStrategy}. + * @since 21 + */ +void HiLogSetAppLogLevel(LogLevel level, PreferStrategy prefer); + #ifdef __cplusplus } #endif diff --git a/interfaces/native/innerkits/libhilog.map b/interfaces/native/innerkits/libhilog.map index 7f844b4..55d0fde 100644 --- a/interfaces/native/innerkits/libhilog.map +++ b/interfaces/native/innerkits/libhilog.map @@ -13,6 +13,7 @@ GetLastFatalMessage; LOG_SetCallback; HiLogSetAppMinLogLevel; + HiLogSetAppLogLevel; }; extern "C++" { "OHOS::HiviewDFX::HiLog::Info(OHOS::HiviewDFX::HiLogLabel const&, char const*, ...)"; diff --git a/interfaces/native/kits/include/hilog/log.h b/interfaces/native/kits/include/hilog/log.h index fe48b03..7db4a31 100644 --- a/interfaces/native/kits/include/hilog/log.h +++ b/interfaces/native/kits/include/hilog/log.h @@ -128,6 +128,30 @@ typedef enum { LOG_FATAL = 7, } LogLevel; +/** + * @brief Enumerates preference strategy to be used in {@link OH_LOG_SetLogLevel}. + * + * You are advised to select preference strategy based on their respective usage scenarios. + * + * @since 21 + */ +typedef enum { + /** Used to unset SetLogLevel, then none is set + */ + UNSET_LOGLEVEL = 0, + /** + * The actual lowest log level is determined by + * the maximum level between the new level and the system-controlled level. + * This is equivalent to calling OH_LOG_SetMinLogLevel. + */ + PREFER_CLOSE_LOG = 1, + /** + * The actual lowest log level is determined by + * the minimum level between the new level and the system-controlled level. + */ + PREFER_OPEN_LOG = 2, +} PreferStrategy; + /** * @brief Outputs logs. * @@ -346,6 +370,15 @@ void OH_LOG_SetCallback(LogCallback callback); */ void OH_LOG_SetMinLogLevel(LogLevel level); +/** + * @brief Sets the lowest log level of the current application process. Different preference strategy can be set. + * + * @param level log level. + * @param prefer preference strategy. See {@link PreferStrategy}. + * @since 21 + */ +void OH_LOG_SetLogLevel(LogLevel level, PreferStrategy prefer); + #ifdef __cplusplus } #endif diff --git a/interfaces/native/kits/libhilog.ndk.json b/interfaces/native/kits/libhilog.ndk.json index f91e0e2..5227228 100644 --- a/interfaces/native/kits/libhilog.ndk.json +++ b/interfaces/native/kits/libhilog.ndk.json @@ -10,5 +10,9 @@ }, { "name": "OH_LOG_SetMinLogLevel" + }, + { + "first_instroduced":"21", + "name": "OH_LOG_SetLogLevel" } ] diff --git a/test/moduletest/common/hilog_ndk_z_test.cpp b/test/moduletest/common/hilog_ndk_z_test.cpp index b3d0233..27782ad 100644 --- a/test/moduletest/common/hilog_ndk_z_test.cpp +++ b/test/moduletest/common/hilog_ndk_z_test.cpp @@ -331,6 +331,35 @@ HWTEST_F(HiLogNDKZTest, setMinLevelTest, TestSize.Level1) HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); } +/** + * @tc.name: Dfx_HiLogNDKZTest_SetLevelTest + * @tc.desc: hilog setLevelTest + * @tc.type: FUNC + */ +HWTEST_F(HiLogNDKZTest, setLevelTest, TestSize.Level1) +{ + HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); + OH_LOG_SetLogLevel(LOG_WARN, PREFER_CLOSE_LOG); + HiLogWriteFailedTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); + OH_LOG_SetLogLevel(LOG_DEBUG, PREFER_CLOSE_LOG); + HiLogWriteFailedTest(DEBUG_METHOD, SOME_LOGS, LOG_C_METHODS); + HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); +} + +/** + * @tc.name: Dfx_HiLogNDKZTest_SetLevelTest + * @tc.desc: hilog setLevelTest2 + * @tc.type: FUNC + */ +HWTEST_F(HiLogNDKZTest, setLevelTest2, TestSize.Level1) +{ + HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); + OH_LOG_SetLogLevel(LOG_WARN, PREFER_OPEN_LOG); + HiLogWriteTest(WARN_METHOD, SOME_LOGS, LOG_C_METHODS); + OH_LOG_SetLogLevel(LOG_DEBUG, PREFER_OPEN_LOG); + HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); +} + } // namespace HiLogTest } // namespace HiviewDFX } // namespace OHOS -- Gitee