From 97e4e38fdb3aaa5ab5c2fa1150874d5a04240cfc Mon Sep 17 00:00:00 2001 From: jiangdayuan Date: Wed, 12 Jan 2022 18:40:11 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E5=9B=9E=E9=80=80=20'Pull=20Request?= =?UTF-8?q?=20!104=20:=20Property=20Get/Set=20implementation'"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 701b0fee6ac02efd51170525a449bd1303ac544f. Signed-off-by: jiangdayuan --- adapter/BUILD.gn | 1 + adapter/properties.cpp | 392 +++++++++--------- .../innerkits/include/hilog_base/log_base.h | 6 +- services/hilogd/etc/BUILD.gn | 16 + services/hilogd/etc/hilog.para | 22 + services/hilogd/etc/hilog.para.dac | 16 + test/BUILD.gn | 17 + test/moduletest/common/adapter_test.cpp | 185 +++++++++ 8 files changed, 464 insertions(+), 191 deletions(-) create mode 100755 services/hilogd/etc/hilog.para create mode 100755 services/hilogd/etc/hilog.para.dac create mode 100644 test/moduletest/common/adapter_test.cpp diff --git a/adapter/BUILD.gn b/adapter/BUILD.gn index 56fdf3c..604454a 100644 --- a/adapter/BUILD.gn +++ b/adapter/BUILD.gn @@ -33,4 +33,5 @@ ohos_shared_library("libhilog_os_adapter") { "system", "updater", ] + external_deps = [ "startup_l2:syspara" ] } diff --git a/adapter/properties.cpp b/adapter/properties.cpp index f87ff5e..0cfd1ca 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -14,24 +14,29 @@ */ #include "properties.h" -#include "hilog/log.h" -#include -#include -#include -#include -#include -#include + +#include #include -#include +#include #include #include #include +#include #include +#include +#include +#include #include -#include +#include +#include +#include #include +#include #include -#include + +#include +#include +#include using namespace std; @@ -48,41 +53,150 @@ static pthread_mutex_t g_processFlowLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_domainFlowLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_kmsgLock = PTHREAD_MUTEX_INITIALIZER; -using PropertyCache = struct { - const void* pinfo; - uint32_t serial; - char propertyValue[HILOG_PROP_VALUE_MAX]; -}; +static int LockByProp(uint32_t propType); +static void UnlockByProp(uint32_t propType); -using SwitchCache = struct { - PropertyCache cache; - bool isOn; -}; +class PropertyTypeLocker { +public: + explicit PropertyTypeLocker(uint32_t propType) + : m_propType(propType) + , m_isLocked(false) + { + m_isLocked = !LockByProp(m_propType); + } + + ~PropertyTypeLocker() + { + if (m_isLocked) { + UnlockByProp(m_propType); + } + } -using LogLevelCache = struct { - PropertyCache cache; - uint16_t logLevel; + bool isLocked() const + { + return m_isLocked; + } +private: + uint32_t m_propType; + bool m_isLocked; }; -using ProcessInfo = struct { - PropertyCache cache; - string propertyKey; - string process; - string processHashPre; - uint32_t processQuota; +using RawPropertyData = std::array; + +template +class CacheData { +public: + using DataConverter = std::function; + + CacheData(DataConverter converter, const T& defaultValue, uint32_t propType, const std::string& suffix = "") + : m_value(defaultValue) + , m_defaultValue(defaultValue) + , m_propType(propType) + , m_converter(converter) + { + m_key = GetPropertyName(m_propType) + suffix; + } + + T getValue() + { + if (m_handle == -1) { + auto handle = FindParameter(m_key.c_str()); + if (handle == -1) { + return m_defaultValue; + } + m_handle = handle; + } + auto currentCommit = GetParameterCommitId(m_handle); + PropertyTypeLocker locker(m_propType); + if (locker.isLocked()) { + if (currentCommit != m_commit) { + updateValue(); + m_commit = currentCommit; + } + return m_value; + } else { + return getDirectValue(); + } + } +private: + bool getRawValue(char *value, unsigned int len) + { + auto res = GetParameterValue(m_handle, value, len); + if (res < 0) { + std::cerr << "CacheData -> GetParameterValue -> Can't get value for key: " << m_key; + std::cerr << " handle: " << m_handle << " Result: " << res << "\n"; + return false; + } + return true; + } + + T getDirectValue() + { + RawPropertyData tempData; + if (!getRawValue(tempData.data(), tempData.size())) { + return m_defaultValue; + } + return m_converter(tempData, m_defaultValue); + } + + void updateValue() + { + if (!getRawValue(m_rawData.data(), m_rawData.size())) { + m_value = m_defaultValue; + return; + } + m_value = m_converter(m_rawData, m_defaultValue); + } + + RawPropertyData m_rawData = {0}; + unsigned int m_handle = -1; + unsigned int m_commit = -1; + T m_value; + const T m_defaultValue; + const uint32_t m_propType; + std::string m_key; + DataConverter m_converter; }; +using SwitchCache = CacheData; +using LogLevelCache = CacheData; + + void PropertyGet(const string &key, char *value, int len) { - if (len < HILOG_PROP_VALUE_MAX) { + if (len > HILOG_PROP_VALUE_MAX) { + std::cerr << "PropertyGet(): len exceed maximum.\n"; + return; + } + + auto handle = FindParameter(key.c_str()); + if (handle == -1) { return; } - /* use OHOS interface */ + + auto res = GetParameterValue(handle, value, len); + if (res < 0) { + std::cerr << "PropertyGet() -> GetParameterValue -> Can't get value for key: " << key; + std::cerr << " handle: " << handle << " Result: " << res << "\n"; + } } void PropertySet(const string &key, const char* value) { - /* use OHOS interface */ + auto len = value ? strlen(value) : 0; + if (len > HILOG_PROP_VALUE_MAX) { + std::cerr << "PropertyGet(): len exceed maximum.\n"; + return; + } + + auto result = SetParameter(key.c_str(), value); + if (result < 0) { + if (result == EC_INVALID) { + std::cerr << "PropertySet(): Invalid arguments.\n"; + } else { + std::cerr << "PropertySet(): Error: " << result << "\n"; + } + } } string GetProgName() @@ -188,50 +302,14 @@ static void UnlockByProp(uint32_t propType) } } -static void RefreshCacheBuf(PropertyCache *cache, const char *key) +static bool textToBool(const RawPropertyData& data, bool defaultVal) { - /* use OHOS interface */ -} - -static bool CheckCache(const PropertyCache *cache) -{ - return true; - /* use OHOS interface */ -} - -static bool GetSwitchCache(bool isFirst, SwitchCache& switchCache, uint32_t propType, bool defaultValue) -{ - int notLocked; - string key = GetPropertyName(propType); - - if (isFirst || CheckCache(&switchCache.cache)) { - notLocked = LockByProp(propType); - if (!notLocked) { - RefreshCacheBuf(&switchCache.cache, key.c_str()); - if (strcmp(switchCache.cache.propertyValue, "true") == 0) { - switchCache.isOn = true; - } else if (strcmp(switchCache.cache.propertyValue, "false") == 0) { - switchCache.isOn = false; - } else { - switchCache.isOn = defaultValue; - } - UnlockByProp(propType); - return switchCache.isOn; - } else { - SwitchCache tmpCache = {{nullptr, 0xffffffff, ""}, defaultValue}; - RefreshCacheBuf(&tmpCache.cache, key.c_str()); - if (strcmp(tmpCache.cache.propertyValue, "true") == 0) { - tmpCache.isOn = true; - } else if (strcmp(tmpCache.cache.propertyValue, "false") == 0) { - tmpCache.isOn = false; - } else { - tmpCache.isOn = defaultValue; - } - return tmpCache.isOn; - } - } else { - return switchCache.isOn; + if (!strcmp(data.data(), "true")) { + return true; + } else if (!strcmp(data.data(), "false")) { + return false; } + return defaultVal; } bool IsDebugOn() @@ -241,173 +319,111 @@ bool IsDebugOn() bool IsSingleDebugOn() { - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_SINGLE_DEBUG, false); + static auto *switchCache = new SwitchCache(textToBool, false, PROP_SINGLE_DEBUG); + return switchCache->getValue(); } bool IsPersistDebugOn() { - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_PERSIST_DEBUG, false); + static auto *switchCache = new SwitchCache(textToBool, false, PROP_PERSIST_DEBUG); + return switchCache->getValue(); } bool IsPrivateSwitchOn() { - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, true}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_PRIVATE, true); + static auto *switchCache = new SwitchCache(textToBool, true, PROP_PRIVATE); + return switchCache->getValue(); } bool IsProcessSwitchOn() { - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_PROCESS_FLOWCTRL, false); + static auto *switchCache = new SwitchCache(textToBool, false, PROP_PROCESS_FLOWCTRL); + return switchCache->getValue(); } bool IsDomainSwitchOn() { - static SwitchCache *switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_DOMAIN_FLOWCTRL, false); + static auto *switchCache = new SwitchCache(textToBool, false, PROP_DOMAIN_FLOWCTRL); + return switchCache->getValue(); } bool IsKmsgSwitchOn() { - static SwitchCache* switchCache = new SwitchCache {{nullptr, 0xffffffff, ""}, false}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - bool isFirst = !isFirstFlag.test_and_set(); - return GetSwitchCache(isFirst, *switchCache, PROP_KMSG, false); + static auto *switchCache = new SwitchCache(textToBool, false, PROP_KMSG); + return switchCache->getValue(); } -static uint16_t GetCacheLevel(char propertyChar) +static uint16_t textToLogLevel(const RawPropertyData& data, uint16_t defaultVal) { - uint16_t cacheLevel = LOG_LEVEL_MIN; - switch (propertyChar) { - case 'D': - case 'd': - cacheLevel = LOG_DEBUG; - break; - case 'I': - case 'i': - cacheLevel = LOG_INFO; - break; - case 'W': - case 'w': - cacheLevel = LOG_WARN; - break; - case 'E': - case 'e': - cacheLevel = LOG_ERROR; - break; - case 'F': - case 'f': - cacheLevel = LOG_FATAL; - break; - default: - break; + static const std::unordered_map logLevels = { + { 'd', LOG_DEBUG }, { 'D', LOG_DEBUG }, + { 'i', LOG_INFO }, { 'I', LOG_INFO }, + { 'w', LOG_WARN }, { 'W', LOG_WARN }, + { 'e', LOG_ERROR }, { 'E', LOG_ERROR }, + { 'f', LOG_FATAL }, { 'F', LOG_FATAL }, + }; + auto it = logLevels.find(data[0]); + if (it != logLevels.end()) { + return it->second; } - return cacheLevel; + return LOG_LEVEL_MIN; } uint16_t GetGlobalLevel() { - string key = GetPropertyName(PROP_GLOBAL_LOG_LEVEL); - static LogLevelCache *levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - static atomic_flag isFirstFlag = ATOMIC_FLAG_INIT; - int notLocked; - - if (!isFirstFlag.test_and_set() || CheckCache(&levelCache->cache)) { - notLocked = LockByProp(PROP_GLOBAL_LOG_LEVEL); - if (!notLocked) { - RefreshCacheBuf(&levelCache->cache, key.c_str()); - levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); - UnlockByProp(PROP_GLOBAL_LOG_LEVEL); - return levelCache->logLevel; - } else { - LogLevelCache tmpCache = {{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - RefreshCacheBuf(&tmpCache.cache, key.c_str()); - tmpCache.logLevel = GetCacheLevel(tmpCache.cache.propertyValue[0]); - return tmpCache.logLevel; - } - } else { - return levelCache->logLevel; - } + static auto *logLevelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_GLOBAL_LOG_LEVEL); + return logLevelCache->getValue(); } uint16_t GetDomainLevel(uint32_t domain) { - static unordered_map *domainMap = new unordered_map(); - unordered_map::iterator it; - string key = GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + to_string(domain); - + static auto *domainMap = new std::unordered_map(); static shared_timed_mutex* mtx = new shared_timed_mutex; + std::decay::type::iterator it; { ReadLock lock(*mtx); it = domainMap->find(domain); } if (it == domainMap->end()) { // new domain InsertLock lock(*mtx); - LogLevelCache* levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - RefreshCacheBuf(&levelCache->cache, key.c_str()); - levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); - uint16_t lvl = levelCache->logLevel; - pair::iterator, bool> ret = domainMap->insert({ domain, levelCache }); - if (!ret.second) { - delete levelCache; - levelCache = nullptr; - } - return lvl; - } else { // existed domain - if (CheckCache(&it->second->cache)) { // changed - InsertLock lock(*mtx); - RefreshCacheBuf(&it->second->cache, key.c_str()); - it->second->logLevel = GetCacheLevel(it->second->cache.propertyValue[0]); - return it->second->logLevel; - } else { // not changed - return it->second->logLevel; + it = domainMap->find(domain); // secured for two thread went across above condition + if (it == domainMap->end()) { + LogLevelCache* levelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_DOMAIN_LOG_LEVEL, + to_string(domain)); + auto result = domainMap->insert({ domain, levelCache }); + if (!result.second) { + std::cerr << "Can't insert new LogLevelCache for domain: " << domain << "\n"; + return LOG_LEVEL_MIN; + } + it = result.first; } } + LogLevelCache* levelCache = it->second; + return levelCache->getValue(); } uint16_t GetTagLevel(const string& tag) { - static unordered_map *tagMap = new unordered_map(); - unordered_map::iterator it; - string key = GetPropertyName(PROP_TAG_LOG_LEVEL) + tag; - + static auto *tagMap = new std::unordered_map(); static shared_timed_mutex* mtx = new shared_timed_mutex; + std::decay::type::iterator it; { ReadLock lock(*mtx); it = tagMap->find(tag); } if (it == tagMap->end()) { // new tag InsertLock lock(*mtx); - LogLevelCache* levelCache = new LogLevelCache{{nullptr, 0xffffffff, ""}, LOG_LEVEL_MIN}; - RefreshCacheBuf(&levelCache->cache, key.c_str()); - levelCache->logLevel = GetCacheLevel(levelCache->cache.propertyValue[0]); - uint16_t lvl = levelCache->logLevel; - pair::iterator, bool> ret = tagMap->insert({ tag, levelCache }); - if (!ret.second) { - delete(levelCache); - levelCache = nullptr; - } - return lvl; - } else { // existed tag - if (CheckCache(&it->second->cache)) { // changed - InsertLock lock(*mtx); - RefreshCacheBuf(&it->second->cache, key.c_str()); - it->second->logLevel = GetCacheLevel(it->second->cache.propertyValue[0]); - return it->second->logLevel; - } else { // not changed - return it->second->logLevel; + it = tagMap->find(tag); // secured for two thread went across above condition + if (it == tagMap->end()) { + LogLevelCache* levelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_TAG_LOG_LEVEL, tag); + auto result = tagMap->insert({ tag, levelCache }); + if (!result.second) { + std::cerr << "Can't insert new LogLevelCache for tag: " << tag << "\n"; + return LOG_LEVEL_MIN; + } + it = result.first; } } + LogLevelCache* levelCache = it->second; + return levelCache->getValue(); } diff --git a/interfaces/native/innerkits/include/hilog_base/log_base.h b/interfaces/native/innerkits/include/hilog_base/log_base.h index d51e2eb..9ac15c4 100644 --- a/interfaces/native/innerkits/include/hilog_base/log_base.h +++ b/interfaces/native/innerkits/include/hilog_base/log_base.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef HIVIEWDFX_HILOG_C_H -#define HIVIEWDFX_HILOG_C_H +#ifndef HIVIEWDFX_HILOG_BASE_C_H +#define HIVIEWDFX_HILOG_BASE_C_H #include #include @@ -74,4 +74,4 @@ bool HiLogBaseIsLoggable(unsigned int domain, const char *tag, LogLevel level); } #endif -#endif // HIVIEWDFX_HILOG_C_H +#endif // HIVIEWDFX_HILOG_BASE_C_H diff --git a/services/hilogd/etc/BUILD.gn b/services/hilogd/etc/BUILD.gn index 1b1f0c1..95420b7 100644 --- a/services/hilogd/etc/BUILD.gn +++ b/services/hilogd/etc/BUILD.gn @@ -15,6 +15,8 @@ import("//build/ohos.gni") group("hilogd_etc") { deps = [ + ":hilog.para", + ":hilog.para.dac", ":hilog_domains.conf", ":hilogd.cfg", ] @@ -32,3 +34,17 @@ ohos_prebuilt_etc("hilogd.cfg") { part_name = "hilog_service" subsystem_name = "hiviewdfx" } + +ohos_prebuilt_etc("hilog.para") { + source = "hilog.para" + relative_install_dir = "param" + part_name = "hilog_service" + subsystem_name = "hiviewdfx" +} + +ohos_prebuilt_etc("hilog.para.dac") { + source = "hilog.para.dac" + relative_install_dir = "param" + part_name = "hilog_service" + subsystem_name = "hiviewdfx" +} diff --git a/services/hilogd/etc/hilog.para b/services/hilogd/etc/hilog.para new file mode 100755 index 0000000..6d55603 --- /dev/null +++ b/services/hilogd/etc/hilog.para @@ -0,0 +1,22 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +hilog.private.on=true +hilog.debug.on=false +persist.sys.hilog.kmsg.on=false +persist.sys.hilog.debug.on=false +hilog.flowctrl.pid.on=false +hilog.flowctrl.domain.on=false + +hilog.loggable.global=d diff --git a/services/hilogd/etc/hilog.para.dac b/services/hilogd/etc/hilog.para.dac new file mode 100755 index 0000000..a970b98 --- /dev/null +++ b/services/hilogd/etc/hilog.para.dac @@ -0,0 +1,16 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +hilog.="shell:log:755" +persist.sys.hilog.="shell:log:755" diff --git a/test/BUILD.gn b/test/BUILD.gn index a88c8fb..32bcf8a 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -41,3 +41,20 @@ ohos_moduletest("HiLogNDKTest") { "//base/hiviewdfx/hilog/frameworks/native/include", ] } + +ohos_moduletest("HiLogAdapterTest") { + module_out_path = module_output_path + + sources = [ "moduletest/common/adapter_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ "hilog_native:libhilog" ] + + include_dirs = [ "//base/hiviewdfx/hilog/adapter" ] +} diff --git a/test/moduletest/common/adapter_test.cpp b/test/moduletest/common/adapter_test.cpp new file mode 100644 index 0000000..336dd62 --- /dev/null +++ b/test/moduletest/common/adapter_test.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include +#include + +using namespace testing::ext; +using namespace std::chrono_literals; + +namespace { +class PropertiesTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + + +HWTEST_F(PropertiesTest, PropertiesTest1, TestSize.Level1) +{ + std::string key = "my_property"; + std::string value = "1234"; + PropertySet(key, value.data()); + + std::array prop = {0}; + PropertyGet(key, prop.data(), prop.size()); + + EXPECT_EQ(std::string(prop.data()), value); +} + +HWTEST_F(PropertiesTest, PropertiesTest2, TestSize.Level1) +{ + std::string key = "my_property"; + for (int i = 0; i < 10; ++i) { + std::string value = std::to_string(i); + PropertySet(key, value.data()); + + std::array prop = {0}; + PropertyGet(key, prop.data(), prop.size()); + + EXPECT_EQ(std::string(prop.data()), value); + } +} + +HWTEST_F(PropertiesTest, PropertiesInvalidParamsTest, TestSize.Level1) +{ + std::string key = "my_property"; + std::string value = "5678"; + PropertySet(key, value.data()); + + PropertyGet(key, nullptr, 0); + PropertySet(key, nullptr); + + std::array prop = {0}; + PropertyGet(key, prop.data(), prop.size()); + + EXPECT_EQ(std::string(prop.data()), value); +} + +HWTEST_F(PropertiesTest, PropertiesTooLongBufferTest, TestSize.Level1) +{ + std::string key = "my_property"; + std::string value = "5678"; + PropertySet(key, value.data()); + + std::array prop1 = {0}; + PropertyGet(key, prop1.data(), prop1.size()); + + std::string tooLongText = "0123456789"; + while (tooLongText.length() < HILOG_PROP_VALUE_MAX) { + tooLongText += tooLongText; + } + PropertySet(key, tooLongText.data()); + + std::array prop = {0}; + PropertyGet(key, prop.data(), prop.size()); + std::string currentValue = prop.data(); + EXPECT_EQ(currentValue, value); + EXPECT_NE(tooLongText, value); +} + +HWTEST_F(PropertiesTest, PropertiesNoKeyTest, TestSize.Level1) +{ + std::string key = "PropertiesNoKeyTest"; + + std::array prop = {0}; + PropertyGet(key, prop.data(), prop.size()); + + EXPECT_TRUE(std::string(prop.data()).empty()); +} + +HWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1) +{ + PropertySet(GetPropertyName(PROP_PRIVATE), "true"); + PropertySet(GetPropertyName(PROP_PROCESS_FLOWCTRL), "true"); + PropertySet(GetPropertyName(PROP_DOMAIN_FLOWCTRL), "true"); + PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "true"); + PropertySet(GetPropertyName(PROP_KMSG), "true"); + PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true"); + + EXPECT_TRUE(IsDebugOn()); + EXPECT_TRUE(IsSingleDebugOn()); + EXPECT_TRUE(IsPersistDebugOn()); + EXPECT_TRUE(IsPrivateSwitchOn()); + EXPECT_TRUE(IsProcessSwitchOn()); + EXPECT_TRUE(IsDomainSwitchOn()); + EXPECT_TRUE(IsKmsgSwitchOn()); + + PropertySet(GetPropertyName(PROP_PRIVATE), "false"); + PropertySet(GetPropertyName(PROP_PROCESS_FLOWCTRL), "false"); + PropertySet(GetPropertyName(PROP_DOMAIN_FLOWCTRL), "false"); + PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "false"); + PropertySet(GetPropertyName(PROP_KMSG), "false"); + PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "false"); + + EXPECT_FALSE(IsDebugOn()); + EXPECT_FALSE(IsSingleDebugOn()); + EXPECT_FALSE(IsPersistDebugOn()); + EXPECT_FALSE(IsPrivateSwitchOn()); + EXPECT_FALSE(IsProcessSwitchOn()); + EXPECT_FALSE(IsDomainSwitchOn()); + EXPECT_FALSE(IsKmsgSwitchOn()); + + PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "true"); + PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "false"); + EXPECT_TRUE(IsDebugOn()); + + PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "false"); + PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true"); + EXPECT_TRUE(IsDebugOn()); +} + +HWTEST_F(PropertiesTest, LevelTest, TestSize.Level1) +{ + static const std::array charLevels = {"d", "D", "f", "F", "e", "E", "w", "W", "i", "I"}; + static const std::array expected = { + LOG_DEBUG, LOG_DEBUG, + LOG_FATAL, LOG_FATAL, + LOG_ERROR, LOG_ERROR, + LOG_WARN, LOG_WARN, + LOG_INFO, LOG_INFO, + }; + + for (size_t i = 0; i < charLevels.size(); ++i) { + PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), charLevels[i]); + EXPECT_EQ(GetGlobalLevel(), expected[i]); + } + PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), "z"); + EXPECT_EQ(GetGlobalLevel(), LOG_LEVEL_MIN); + + uint32_t domain = 12345; + for (size_t i = 0; i < charLevels.size(); ++i) { + PropertySet(GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + std::to_string(domain), charLevels[i]); + EXPECT_EQ(GetDomainLevel(domain), expected[i]); + } + PropertySet(GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + std::to_string(domain), "z"); + EXPECT_EQ(GetDomainLevel(domain), LOG_LEVEL_MIN); + + std::string tag = "test_tag"; + for (size_t i = 0; i < charLevels.size(); ++i) { + PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, charLevels[i]); + EXPECT_EQ(GetTagLevel(tag), expected[i]); + } + PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, "z"); + EXPECT_EQ(GetTagLevel(tag), LOG_LEVEL_MIN); +} +} // namespace -- Gitee