From 0bb0896c987fb3dbbf9257ecf8ff5414584b3c15 Mon Sep 17 00:00:00 2001 From: kangping Date: Tue, 21 Sep 2021 10:00:46 +0800 Subject: [PATCH] [logging] fix dynamic log level (#7016) Set mLoggingLevel as static member. --- src/core/api/logging_api.cpp | 6 +++--- src/core/common/instance.cpp | 7 ++++--- src/core/common/instance.hpp | 10 +++++----- src/core/thread/mesh_forwarder.cpp | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/core/api/logging_api.cpp b/src/core/api/logging_api.cpp index 3aff8aa17..b00147c52 100644 --- a/src/core/api/logging_api.cpp +++ b/src/core/api/logging_api.cpp @@ -41,8 +41,8 @@ using namespace ot; otLogLevel otLoggingGetLevel(void) { -#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE && !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE - return Instance::Get().GetLogLevel(); +#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE + return Instance::GetLogLevel(); #else return static_cast(OPENTHREAD_CONFIG_LOG_LEVEL); #endif @@ -55,7 +55,7 @@ otError otLoggingSetLevel(otLogLevel aLogLevel) if (aLogLevel <= OT_LOG_LEVEL_DEBG && aLogLevel >= OT_LOG_LEVEL_NONE) { - Instance::Get().SetLogLevel(aLogLevel); + Instance::SetLogLevel(aLogLevel); } else { diff --git a/src/core/common/instance.cpp b/src/core/common/instance.cpp index dc1142172..fb543e4e4 100644 --- a/src/core/common/instance.cpp +++ b/src/core/common/instance.cpp @@ -58,6 +58,10 @@ bool Instance::sDnsNameCompressionEnabled = true; #endif #endif +#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE +otLogLevel Instance::sLogLevel = static_cast(OPENTHREAD_CONFIG_LOG_LEVEL_INIT); +#endif + Instance::Instance(void) : mTimerMilliScheduler(*this) #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE @@ -109,9 +113,6 @@ Instance::Instance(void) #if OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE , mLinkRaw(*this) #endif -#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE - , mLogLevel(static_cast(OPENTHREAD_CONFIG_LOG_LEVEL_INIT)) -#endif #if OPENTHREAD_ENABLE_VENDOR_EXTENSION , mExtension(Extension::ExtensionBase::Init(*this)) #endif diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index 15a2a9bd5..7436ca930 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -208,10 +208,10 @@ public: * @returns The log level. * */ - otLogLevel GetLogLevel(void) const + static otLogLevel GetLogLevel(void) #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE { - return mLogLevel; + return sLogLevel; } #else { @@ -226,10 +226,10 @@ public: * @param[in] aLogLevel A log level. * */ - void SetLogLevel(otLogLevel aLogLevel) + static void SetLogLevel(otLogLevel aLogLevel) { OT_ASSERT(aLogLevel <= OT_LOG_LEVEL_DEBG && aLogLevel >= OT_LOG_LEVEL_NONE); - mLogLevel = aLogLevel; + sLogLevel = aLogLevel; } #endif @@ -429,7 +429,7 @@ private: #endif // OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE - otLogLevel mLogLevel; + static otLogLevel sLogLevel; #endif #if OPENTHREAD_ENABLE_VENDOR_EXTENSION Extension::ExtensionBase &mExtension; diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index 567d4eda3..56b2b05cf 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -1816,7 +1816,7 @@ void MeshForwarder::LogMessage(MessageAction aAction, break; } - VerifyOrExit(GetInstance().GetLogLevel() >= logLevel); + VerifyOrExit(Instance::GetLogLevel() >= logLevel); switch (aMessage.GetType()) {