From 68a643c5b95e4466400dfc02594ffc67bbd3c3e2 Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 28 Dec 2021 12:13:58 +0100 Subject: [PATCH 01/11] Added implementation of PropertyGet PropertySet functions. Added tests of this functionality. Signed-off-by: Karol Grydziuszko Change-Id: I351de9ddbd20a0020ad8b7c9fb391618feb0b813 --- adapter/BUILD.gn | 1 + adapter/properties.cpp | 32 +++++++- test/BUILD.gn | 15 ++++ test/moduletest/common/adapter_test.cpp | 105 ++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 3 deletions(-) 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..3af27de 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include using namespace std; @@ -74,15 +76,39 @@ using ProcessInfo = struct { 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; } - /* use OHOS interface */ + static const char* emptyStr = ""; + auto result = GetParameter(key.c_str(), emptyStr, value, len); + if (result < 0) { + if (result == EC_INVALID) { + std::cerr << "PropertyGet(): Invalid arguments.\n"; + } + else { + std::cerr << "PropertyGet(): Error: " << result << "\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() diff --git a/test/BUILD.gn b/test/BUILD.gn index a88c8fb..5c9351b 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -41,3 +41,18 @@ 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", + ] + + include_dirs = [ "//base/hiviewdfx/hilog/adapter" ] +} \ No newline at end of file diff --git a/test/moduletest/common/adapter_test.cpp b/test/moduletest/common/adapter_test.cpp new file mode 100644 index 0000000..d8aef22 --- /dev/null +++ b/test/moduletest/common/adapter_test.cpp @@ -0,0 +1,105 @@ +/* + * 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 + +using namespace testing::ext; + +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()); +} -- Gitee From 0bafbd7a70f5165c65159072ed1774f232d09dba Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Wed, 29 Dec 2021 09:09:30 +0100 Subject: [PATCH 02/11] Implemented cache in adapter properties Signed-off-by: Karol Grydziuszko Change-Id: I65b1f0829bfb2367a983ce9a8e04ca73d56f09f7 --- adapter/BUILD.gn | 5 +- adapter/properties.cpp | 376 +++++++++--------- .../innerkits/include/hilog_base/log_base.h | 6 +- test/BUILD.gn | 2 + test/moduletest/common/adapter_test.cpp | 95 +++++ 5 files changed, 294 insertions(+), 190 deletions(-) diff --git a/adapter/BUILD.gn b/adapter/BUILD.gn index 604454a..c8dc21e 100644 --- a/adapter/BUILD.gn +++ b/adapter/BUILD.gn @@ -33,5 +33,8 @@ ohos_shared_library("libhilog_os_adapter") { "system", "updater", ] - external_deps = [ "startup_l2:syspara" ] + external_deps = [ + "startup_l2:syspara", + "startup_l2:syspara_watchagent", + ] } diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 3af27de..ae25415 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -15,23 +15,26 @@ #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 @@ -50,33 +53,131 @@ 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: + 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_needUpdate(false) + { + m_key = GetPropertyName(m_propType) + suffix; + PropertyGet(m_key, m_rawData.data(), m_rawData.size()); + m_value = m_converter(m_rawData, m_defaultValue); + auto res = WatchParameter(m_key.c_str(), CacheData::onDataChanged, this); + if (res) { + std::cerr << "CacheData. Property: " << m_key << " can't be watched! "; + std::cerr << "Result: " << res << "\n"; + } + } + + ~CacheData() + { + // This will clear all watchers for particular property key! + // I assume that thers is only one watcher for this property name + // otherwise function WatchParameter should not be used. + WatchParameter(m_key.c_str(), nullptr, nullptr); + } + + static void onDataChanged(const char *key, const char *newValue, void* ctx) + { + if (!newValue) { + std::cerr << "CacheData. Invalid data on change!\n"; + return; + } + if (!ctx) { + std::cerr << "CacheData. Invalid context!\n"; + return; + } + auto obj = reinterpret_cast*>(ctx); + obj->onDataChanged(key, newValue); + } + + T getValue() + { + PropertyTypeLocker locker(m_propType); + if (locker.isLocked()) { + bool expectedUpdate = true; // compare_exchange_strong expects reference value + if (m_needUpdate.compare_exchange_strong(expectedUpdate, false)) { + PropertyGet(m_key, m_rawData.data(), m_rawData.size()); + m_value = m_converter(m_rawData, m_defaultValue); + } + return m_value; + } + RawPropertyData tempData = {0}; + PropertyGet(m_key, tempData.data(), tempData.size()-1); + return m_converter(tempData, m_defaultValue); + } +private: + void onDataChanged(const char *key, const char *newValue) + { + assert(m_key == key); + PropertyTypeLocker locker(m_propType); + if (locker.isLocked()) { + auto len = strlen(newValue); + if (len + 1 > HILOG_PROP_VALUE_MAX) { + std::cerr << "CacheData. New value is too long!\n"; + return; + } + std::memcpy(m_rawData.data(), newValue, len); + m_rawData[len] = 0; + m_value = m_converter(m_rawData, m_defaultValue); + m_needUpdate.store(false); + } + else { + m_needUpdate.store(true); + } + } + + RawPropertyData m_rawData = {0}; + T m_value; + const T m_defaultValue; + const uint32_t m_propType; + std::string m_key; + DataConverter m_converter; + std::atomic_bool m_needUpdate; }; +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; } @@ -95,7 +196,7 @@ void PropertyGet(const string &key, char *value, int len) void PropertySet(const string &key, const char* value) { auto len = value ? strlen(value) : 0; - if (len >= HILOG_PROP_VALUE_MAX) { + if (len > HILOG_PROP_VALUE_MAX) { std::cerr << "PropertyGet(): len exceed maximum.\n"; return; } @@ -214,50 +315,15 @@ static void UnlockByProp(uint32_t propType) } } -static void RefreshCacheBuf(PropertyCache *cache, const char *key) -{ - /* 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) +static bool textToBool(const RawPropertyData& data, bool defaultVal) { - 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() @@ -267,173 +333,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/test/BUILD.gn b/test/BUILD.gn index 5c9351b..b484712 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -54,5 +54,7 @@ ohos_moduletest("HiLogAdapterTest") { "//third_party/googletest:gtest_main", ] + external_deps = [ "hilog_native:libhilog" ] + include_dirs = [ "//base/hiviewdfx/hilog/adapter" ] } \ No newline at end of file diff --git a/test/moduletest/common/adapter_test.cpp b/test/moduletest/common/adapter_test.cpp index d8aef22..1f2ca73 100644 --- a/test/moduletest/common/adapter_test.cpp +++ b/test/moduletest/common/adapter_test.cpp @@ -16,11 +16,14 @@ #include #include #include +#include #include #include +#include using namespace testing::ext; +using namespace std::chrono_literals; class PropertiesTest : public testing::Test { public: @@ -103,3 +106,95 @@ HWTEST_F(PropertiesTest, PropertiesNoKeyTest, TestSize.Level1) 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"); + + std::this_thread::sleep_for(1000ms); + + 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"); + + std::cout << "Wait 3s\n"; + std::this_thread::sleep_for(3000ms); + + 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"); + std::cout << "Wait 1s\n"; + std::this_thread::sleep_for(1000ms); + EXPECT_TRUE(IsDebugOn()); + + PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "false"); + PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true"); + std::cout << "Wait 1s\n"; + std::this_thread::sleep_for(1000ms); + 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]); + std::this_thread::sleep_for(500ms); + EXPECT_EQ(GetGlobalLevel(), expected[i]); + } + PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), "z"); + std::this_thread::sleep_for(500ms); + 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]); + std::this_thread::sleep_for(500ms); + EXPECT_EQ(GetDomainLevel(domain), expected[i]); + } + PropertySet(GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + std::to_string(domain), "z"); + std::this_thread::sleep_for(500ms); + 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]); + std::this_thread::sleep_for(500ms); + EXPECT_EQ(GetTagLevel(tag), expected[i]); + } + PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, "z"); + std::this_thread::sleep_for(500ms); + EXPECT_EQ(GetTagLevel(tag), LOG_LEVEL_MIN); +} + -- Gitee From b65ac0be7d382d781cf6b3133cb9d072bf7a62ab Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 4 Jan 2022 12:23:06 +0100 Subject: [PATCH 03/11] Changed implementation of parameters adapter to not use param watcher. Signed-off-by: Karol Grydziuszko Change-Id: I5e81e3c2e421ae5dd1b4159552562da9e859327a --- adapter/BUILD.gn | 5 +- adapter/properties.cpp | 113 +++++++++++------------- test/moduletest/common/adapter_test.cpp | 16 ---- 3 files changed, 51 insertions(+), 83 deletions(-) diff --git a/adapter/BUILD.gn b/adapter/BUILD.gn index c8dc21e..604454a 100644 --- a/adapter/BUILD.gn +++ b/adapter/BUILD.gn @@ -33,8 +33,5 @@ ohos_shared_library("libhilog_os_adapter") { "system", "updater", ] - external_deps = [ - "startup_l2:syspara", - "startup_l2:syspara_watchagent", - ] + external_deps = [ "startup_l2:syspara" ] } diff --git a/adapter/properties.cpp b/adapter/properties.cpp index ae25415..0766f4b 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -92,83 +92,68 @@ public: , m_defaultValue(defaultValue) , m_propType(propType) , m_converter(converter) - , m_needUpdate(false) { m_key = GetPropertyName(m_propType) + suffix; - PropertyGet(m_key, m_rawData.data(), m_rawData.size()); - m_value = m_converter(m_rawData, m_defaultValue); - auto res = WatchParameter(m_key.c_str(), CacheData::onDataChanged, this); - if (res) { - std::cerr << "CacheData. Property: " << m_key << " can't be watched! "; - std::cerr << "Result: " << res << "\n"; - } - } - - ~CacheData() - { - // This will clear all watchers for particular property key! - // I assume that thers is only one watcher for this property name - // otherwise function WatchParameter should not be used. - WatchParameter(m_key.c_str(), nullptr, nullptr); } - static void onDataChanged(const char *key, const char *newValue, void* ctx) - { - if (!newValue) { - std::cerr << "CacheData. Invalid data on change!\n"; - return; - } - if (!ctx) { - std::cerr << "CacheData. Invalid context!\n"; - return; - } - auto obj = reinterpret_cast*>(ctx); - obj->onDataChanged(key, newValue); - } - T getValue() { + if (m_handle == -1) { + auto handle = FindParameter(m_key.c_str()); + if (handle == -1) { + // std::cout << "CacheData -> FindParameter -> Can't find handle for key: " << m_key << "\n"; + return m_defaultValue; + } + m_handle = handle; + } + auto currentCommit = GetParameterCommitId(m_handle); PropertyTypeLocker locker(m_propType); if (locker.isLocked()) { - bool expectedUpdate = true; // compare_exchange_strong expects reference value - if (m_needUpdate.compare_exchange_strong(expectedUpdate, false)) { - PropertyGet(m_key, m_rawData.data(), m_rawData.size()); - m_value = m_converter(m_rawData, m_defaultValue); + if (currentCommit != m_commit) { + updateValue(); + m_commit = currentCommit; } return m_value; } - RawPropertyData tempData = {0}; - PropertyGet(m_key, tempData.data(), tempData.size()-1); - return m_converter(tempData, m_defaultValue); + else { + return getDirectValue(); + } } private: - void onDataChanged(const char *key, const char *newValue) - { - assert(m_key == key); - PropertyTypeLocker locker(m_propType); - if (locker.isLocked()) { - auto len = strlen(newValue); - if (len + 1 > HILOG_PROP_VALUE_MAX) { - std::cerr << "CacheData. New value is too long!\n"; - return; - } - std::memcpy(m_rawData.data(), newValue, len); - m_rawData[len] = 0; - m_value = m_converter(m_rawData, m_defaultValue); - m_needUpdate.store(false); + 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; } - else { - m_needUpdate.store(true); + 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; - std::atomic_bool m_needUpdate; }; using SwitchCache = CacheData; @@ -181,15 +166,17 @@ void PropertyGet(const string &key, char *value, int len) std::cerr << "PropertyGet(): len exceed maximum.\n"; return; } - static const char* emptyStr = ""; - auto result = GetParameter(key.c_str(), emptyStr, value, len); - if (result < 0) { - if (result == EC_INVALID) { - std::cerr << "PropertyGet(): Invalid arguments.\n"; - } - else { - std::cerr << "PropertyGet(): Error: " << result << "\n"; - } + + auto handle = FindParameter(key.c_str()); + if (handle == -1) { + //std::cout << "PropertyGet() -> FindParameter() -> Can't find handle for key:" << key << "\n"; + return; + } + + 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"; } } diff --git a/test/moduletest/common/adapter_test.cpp b/test/moduletest/common/adapter_test.cpp index 1f2ca73..9a57673 100644 --- a/test/moduletest/common/adapter_test.cpp +++ b/test/moduletest/common/adapter_test.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include @@ -116,8 +115,6 @@ HWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1) PropertySet(GetPropertyName(PROP_KMSG), "true"); PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true"); - std::this_thread::sleep_for(1000ms); - EXPECT_TRUE(IsDebugOn()); EXPECT_TRUE(IsSingleDebugOn()); EXPECT_TRUE(IsPersistDebugOn()); @@ -133,9 +130,6 @@ HWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1) PropertySet(GetPropertyName(PROP_KMSG), "false"); PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "false"); - std::cout << "Wait 3s\n"; - std::this_thread::sleep_for(3000ms); - EXPECT_FALSE(IsDebugOn()); EXPECT_FALSE(IsSingleDebugOn()); EXPECT_FALSE(IsPersistDebugOn()); @@ -146,14 +140,10 @@ HWTEST_F(PropertiesTest, SwitchTest, TestSize.Level1) PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "true"); PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "false"); - std::cout << "Wait 1s\n"; - std::this_thread::sleep_for(1000ms); EXPECT_TRUE(IsDebugOn()); PropertySet(GetPropertyName(PROP_SINGLE_DEBUG), "false"); PropertySet(GetPropertyName(PROP_PERSIST_DEBUG), "true"); - std::cout << "Wait 1s\n"; - std::this_thread::sleep_for(1000ms); EXPECT_TRUE(IsDebugOn()); } @@ -170,31 +160,25 @@ HWTEST_F(PropertiesTest, LevelTest, TestSize.Level1) for (size_t i = 0; i < charLevels.size(); ++i) { PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), charLevels[i]); - std::this_thread::sleep_for(500ms); EXPECT_EQ(GetGlobalLevel(), expected[i]); } PropertySet(GetPropertyName(PROP_GLOBAL_LOG_LEVEL), "z"); - std::this_thread::sleep_for(500ms); 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]); - std::this_thread::sleep_for(500ms); EXPECT_EQ(GetDomainLevel(domain), expected[i]); } PropertySet(GetPropertyName(PROP_DOMAIN_LOG_LEVEL) + std::to_string(domain), "z"); - std::this_thread::sleep_for(500ms); 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]); - std::this_thread::sleep_for(500ms); EXPECT_EQ(GetTagLevel(tag), expected[i]); } PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, "z"); - std::this_thread::sleep_for(500ms); EXPECT_EQ(GetTagLevel(tag), LOG_LEVEL_MIN); } -- Gitee From b0cf161d7803db2a866b3469638f3bd078b3b94a Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 4 Jan 2022 14:53:22 +0100 Subject: [PATCH 04/11] Removed unused code Signed-off-by: Karol Grydziuszko Change-Id: If00df335724b83fef3b3b6c540fc55e1d6fb637f --- adapter/properties.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 0766f4b..446fe29 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -101,7 +101,6 @@ public: if (m_handle == -1) { auto handle = FindParameter(m_key.c_str()); if (handle == -1) { - // std::cout << "CacheData -> FindParameter -> Can't find handle for key: " << m_key << "\n"; return m_defaultValue; } m_handle = handle; @@ -169,7 +168,6 @@ void PropertyGet(const string &key, char *value, int len) auto handle = FindParameter(key.c_str()); if (handle == -1) { - //std::cout << "PropertyGet() -> FindParameter() -> Can't find handle for key:" << key << "\n"; return; } -- Gitee From 4817ba493593450020ba271a00cbff59d6a5472e Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 4 Jan 2022 14:54:03 +0100 Subject: [PATCH 05/11] Added hilog.para and hilog.para.dac Signed-off-by: Karol Grydziuszko Change-Id: I0bc9a495f8cd6723a5fe4bfeb5725d2770a8cd57 --- services/hilogd/etc/hilog.para | 26 ++++++++++++++++++++++++++ services/hilogd/etc/hilog.para.dac | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 services/hilogd/etc/hilog.para create mode 100755 services/hilogd/etc/hilog.para.dac diff --git a/services/hilogd/etc/hilog.para b/services/hilogd/etc/hilog.para new file mode 100755 index 0000000..efad8c4 --- /dev/null +++ b/services/hilogd/etc/hilog.para @@ -0,0 +1,26 @@ +# 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 +hilog.loggable.domain*=d +hilog.loggable.tag*=d + + diff --git a/services/hilogd/etc/hilog.para.dac b/services/hilogd/etc/hilog.para.dac new file mode 100755 index 0000000..34d132e --- /dev/null +++ b/services/hilogd/etc/hilog.para.dac @@ -0,0 +1,24 @@ +# 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 root:root:0777 +hilog.debug.on root:root:0777 +persist.sys.hilog.kmsg.on root:root:0777 +persist.sys.hilog.debug.on root:root:0777 +hilog.flowctrl.pid.on root:root:0777 +hilog.flowctrl.domain.on root:root:0777 + +hilog.loggable.global* root:root:0777 +hilog.loggable.domain* root:root:0777 +hilog.loggable.tag* root:root:0777 \ No newline at end of file -- Gitee From 74e1e4540c4996b9c287a09ddcc51680af758c2b Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 4 Jan 2022 15:19:17 +0100 Subject: [PATCH 06/11] Fixed BUILD.gn format check. Signed-off-by: Karol Grydziuszko Change-Id: I7059ac68895344430926f6158d90eacfdf071ec9 --- test/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/BUILD.gn b/test/BUILD.gn index b484712..32bcf8a 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -57,4 +57,4 @@ ohos_moduletest("HiLogAdapterTest") { external_deps = [ "hilog_native:libhilog" ] include_dirs = [ "//base/hiviewdfx/hilog/adapter" ] -} \ No newline at end of file +} -- Gitee From 0767e70d5a552962d168967e625d3beb409e7744 Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 4 Jan 2022 16:10:41 +0100 Subject: [PATCH 07/11] Fixed code check. Signed-off-by: Karol Grydziuszko Change-Id: Ic527e37aab359ac585e670ba41eb644ee24f2f5f --- adapter/properties.cpp | 51 +++++++++++++------------ test/moduletest/common/adapter_test.cpp | 3 ++ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index 446fe29..d654fbc 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -56,8 +56,7 @@ static pthread_mutex_t g_kmsgLock = PTHREAD_MUTEX_INITIALIZER; static int LockByProp(uint32_t propType); static void UnlockByProp(uint32_t propType); -class PropertyTypeLocker -{ +class PropertyTypeLocker { public: PropertyTypeLocker(uint32_t propType) : m_propType(propType) @@ -66,14 +65,17 @@ public: m_isLocked = !LockByProp(m_propType); } - ~PropertyTypeLocker() + ~PropertyTypeLocker() { if (m_isLocked) { UnlockByProp(m_propType); } } - bool isLocked() const { return m_isLocked; } + bool isLocked() const + { + return m_isLocked; + } private: uint32_t m_propType; bool m_isLocked; @@ -82,8 +84,7 @@ private: using RawPropertyData = std::array; template -class CacheData -{ +class CacheData { public: using DataConverter = std::function; @@ -113,13 +114,13 @@ public: m_commit = currentCommit; } return m_value; - } - else { + } else { return getDirectValue(); } } private: - bool getRawValue(char *value, unsigned int len) { + 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; @@ -129,7 +130,8 @@ private: return true; } - T getDirectValue() { + T getDirectValue() + { RawPropertyData tempData; if (!getRawValue(tempData.data(), tempData.size())) { return m_defaultValue; @@ -137,7 +139,8 @@ private: return m_converter(tempData, m_defaultValue); } - void updateValue() { + void updateValue() + { if (!getRawValue(m_rawData.data(), m_rawData.size())) { m_value = m_defaultValue; return; @@ -190,8 +193,7 @@ void PropertySet(const string &key, const char* value) if (result < 0) { if (result == EC_INVALID) { std::cerr << "PropertySet(): Invalid arguments.\n"; - } - else { + } else { std::cerr << "PropertySet(): Error: " << result << "\n"; } } @@ -300,12 +302,11 @@ static void UnlockByProp(uint32_t propType) } } -static bool textToBool(const RawPropertyData& data, bool defaultVal) +static bool textToBool(const RawPropertyData& data, bool defaultVal) { if (!strcmp(data.data(), "true")) { return true; - } - else if (!strcmp(data.data(), "false")) { + } else if (!strcmp(data.data(), "false")) { return false; } return defaultVal; @@ -352,14 +353,14 @@ bool IsKmsgSwitchOn() return switchCache->getValue(); } -static uint16_t textToLogLevel(const RawPropertyData& data, uint16_t defaultVal) +static uint16_t textToLogLevel(const RawPropertyData& data, uint16_t defaultVal) { - 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}, + 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()) { @@ -387,7 +388,7 @@ uint16_t GetDomainLevel(uint32_t domain) InsertLock lock(*mtx); 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, + LogLevelCache* levelCache = new LogLevelCache(textToLogLevel, LOG_LEVEL_MIN, PROP_DOMAIN_LOG_LEVEL, to_string(domain)); auto result = domainMap->insert({ domain, levelCache }); if (!result.second) { @@ -415,7 +416,7 @@ uint16_t GetTagLevel(const string& tag) 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 }); + auto result = tagMap->insert( { tag, levelCache } ); if (!result.second) { std::cerr << "Can't insert new LogLevelCache for tag: " << tag << "\n"; return LOG_LEVEL_MIN; diff --git a/test/moduletest/common/adapter_test.cpp b/test/moduletest/common/adapter_test.cpp index 9a57673..0e1c9bb 100644 --- a/test/moduletest/common/adapter_test.cpp +++ b/test/moduletest/common/adapter_test.cpp @@ -24,6 +24,8 @@ using namespace testing::ext; using namespace std::chrono_literals; +namespace { + class PropertiesTest : public testing::Test { public: static void SetUpTestCase() {} @@ -182,3 +184,4 @@ HWTEST_F(PropertiesTest, LevelTest, TestSize.Level1) EXPECT_EQ(GetTagLevel(tag), LOG_LEVEL_MIN); } +} // namespace -- Gitee From 858cfd2012dd7f8d082670fe49d49f8650a94263 Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Tue, 4 Jan 2022 16:46:30 +0100 Subject: [PATCH 08/11] Fixed code check. Signed-off-by: Karol Grydziuszko Change-Id: Ied1a47f397ca2b30369307acb2b1d0c76300ec1c --- adapter/properties.cpp | 10 +++++----- test/moduletest/common/adapter_test.cpp | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/adapter/properties.cpp b/adapter/properties.cpp index d654fbc..0cfd1ca 100644 --- a/adapter/properties.cpp +++ b/adapter/properties.cpp @@ -14,7 +14,6 @@ */ #include "properties.h" -#include "hilog/log.h" #include #include @@ -35,6 +34,7 @@ #include #include +#include #include #include @@ -58,7 +58,7 @@ static void UnlockByProp(uint32_t propType); class PropertyTypeLocker { public: - PropertyTypeLocker(uint32_t propType) + explicit PropertyTypeLocker(uint32_t propType) : m_propType(propType) , m_isLocked(false) { @@ -357,8 +357,8 @@ static uint16_t textToLogLevel(const RawPropertyData& data, uint16_t defaultVal) { static const std::unordered_map logLevels = { { 'd', LOG_DEBUG }, { 'D', LOG_DEBUG }, - { 'i', LOG_INFO }, { 'I', LOG_INFO }, - { 'w', LOG_WARN }, { 'W', LOG_WARN }, + { 'i', LOG_INFO }, { 'I', LOG_INFO }, + { 'w', LOG_WARN }, { 'W', LOG_WARN }, { 'e', LOG_ERROR }, { 'E', LOG_ERROR }, { 'f', LOG_FATAL }, { 'F', LOG_FATAL }, }; @@ -416,7 +416,7 @@ uint16_t GetTagLevel(const string& tag) 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 } ); + auto result = tagMap->insert({ tag, levelCache }); if (!result.second) { std::cerr << "Can't insert new LogLevelCache for tag: " << tag << "\n"; return LOG_LEVEL_MIN; diff --git a/test/moduletest/common/adapter_test.cpp b/test/moduletest/common/adapter_test.cpp index 0e1c9bb..336dd62 100644 --- a/test/moduletest/common/adapter_test.cpp +++ b/test/moduletest/common/adapter_test.cpp @@ -25,7 +25,6 @@ using namespace testing::ext; using namespace std::chrono_literals; namespace { - class PropertiesTest : public testing::Test { public: static void SetUpTestCase() {} @@ -183,5 +182,4 @@ HWTEST_F(PropertiesTest, LevelTest, TestSize.Level1) PropertySet(GetPropertyName(PROP_TAG_LOG_LEVEL) + tag, "z"); EXPECT_EQ(GetTagLevel(tag), LOG_LEVEL_MIN); } - } // namespace -- Gitee From 2681dfe0dcf11fed36555f176850447cd88f92c7 Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Mon, 10 Jan 2022 09:03:37 +0100 Subject: [PATCH 09/11] Changed hilog.para and hilog.para.dac Signed-off-by: Karol Grydziuszko Change-Id: I26dd8e8a03c3f5bef2d4092f1a27216a896d5ae9 --- services/hilogd/etc/hilog.para | 6 +----- services/hilogd/etc/hilog.para.dac | 12 ++---------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/services/hilogd/etc/hilog.para b/services/hilogd/etc/hilog.para index efad8c4..6d55603 100755 --- a/services/hilogd/etc/hilog.para +++ b/services/hilogd/etc/hilog.para @@ -19,8 +19,4 @@ persist.sys.hilog.debug.on=false hilog.flowctrl.pid.on=false hilog.flowctrl.domain.on=false -hilog.loggable.global*=d -hilog.loggable.domain*=d -hilog.loggable.tag*=d - - +hilog.loggable.global=d diff --git a/services/hilogd/etc/hilog.para.dac b/services/hilogd/etc/hilog.para.dac index 34d132e..a970b98 100755 --- a/services/hilogd/etc/hilog.para.dac +++ b/services/hilogd/etc/hilog.para.dac @@ -12,13 +12,5 @@ # limitations under the License. -hilog.private.on root:root:0777 -hilog.debug.on root:root:0777 -persist.sys.hilog.kmsg.on root:root:0777 -persist.sys.hilog.debug.on root:root:0777 -hilog.flowctrl.pid.on root:root:0777 -hilog.flowctrl.domain.on root:root:0777 - -hilog.loggable.global* root:root:0777 -hilog.loggable.domain* root:root:0777 -hilog.loggable.tag* root:root:0777 \ No newline at end of file +hilog.="shell:log:755" +persist.sys.hilog.="shell:log:755" -- Gitee From e00b2ea0b943584896da6eb7994b5f87aa31b584 Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Mon, 10 Jan 2022 10:10:52 +0100 Subject: [PATCH 10/11] Fixed copying of hilog.para and hilog.para.dac Signed-off-by: Karol Grydziuszko Change-Id: I363c0dc6c7744b6d34141acb6ed429b44021d4c9 --- services/hilogd/etc/BUILD.gn | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/services/hilogd/etc/BUILD.gn b/services/hilogd/etc/BUILD.gn index 1b1f0c1..5f997ad 100644 --- a/services/hilogd/etc/BUILD.gn +++ b/services/hilogd/etc/BUILD.gn @@ -17,6 +17,8 @@ group("hilogd_etc") { deps = [ ":hilog_domains.conf", ":hilogd.cfg", + ":hilog.para", + ":hilog.para.dac" ] } @@ -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" +} -- Gitee From e529620cf915e89e17091b19a2f95e50760310eb Mon Sep 17 00:00:00 2001 From: Karol Grydziuszko Date: Mon, 10 Jan 2022 10:33:47 +0100 Subject: [PATCH 11/11] Fixed build.gn check. Signed-off-by: Karol Grydziuszko Change-Id: I4f29b982a5f3371d027390ac1b3e98f344473836 --- services/hilogd/etc/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/etc/BUILD.gn b/services/hilogd/etc/BUILD.gn index 5f997ad..95420b7 100644 --- a/services/hilogd/etc/BUILD.gn +++ b/services/hilogd/etc/BUILD.gn @@ -15,10 +15,10 @@ import("//build/ohos.gni") group("hilogd_etc") { deps = [ + ":hilog.para", + ":hilog.para.dac", ":hilog_domains.conf", ":hilogd.cfg", - ":hilog.para", - ":hilog.para.dac" ] } -- Gitee