From 0c754bb09d043169615f71f390fe305d9cf6c852 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Wed, 13 Apr 2022 03:36:00 +0800 Subject: [PATCH] [logging] add log level platform handler (#7545) Adds a log level platform handler that allow platform code to handle OT log level changes. --- include/openthread/instance.h | 2 +- include/openthread/platform/logging.h | 13 +++++++++++++ src/core/common/instance.cpp | 18 ++++++++++++++++++ src/core/common/instance.hpp | 2 +- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 8b2457196..c57b31274 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (201) +#define OPENTHREAD_API_VERSION (202) /** * @addtogroup api-instance diff --git a/include/openthread/platform/logging.h b/include/openthread/platform/logging.h index 8d9d36f11..adf123f44 100644 --- a/include/openthread/platform/logging.h +++ b/include/openthread/platform/logging.h @@ -161,6 +161,19 @@ typedef enum otLogRegion */ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...); +/** + * This function handles OpenThread log level changes. + * + * This platform function is called whenever the OpenThread log level changes. + * This platform function is optional since an empty weak implementation has been provided. + * + * @note Only applicable when `OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE=1`. + * + * @param[in] aLogLevel The new OpenThread log level. + * + */ +void otPlatLogHandleLevelChanged(otLogLevel aLogLevel); + /** * @} * diff --git a/src/core/common/instance.cpp b/src/core/common/instance.cpp index 2262f02a0..122eb4327 100644 --- a/src/core/common/instance.cpp +++ b/src/core/common/instance.cpp @@ -294,4 +294,22 @@ void Instance::GetBufferInfo(BufferInfo &aInfo) #endif // OPENTHREAD_MTD || OPENTHREAD_FTD +#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE + +void Instance::SetLogLevel(LogLevel aLogLevel) +{ + if (aLogLevel != sLogLevel) + { + sLogLevel = aLogLevel; + otPlatLogHandleLevelChanged(sLogLevel); + } +} + +extern "C" OT_TOOL_WEAK void otPlatLogHandleLevelChanged(otLogLevel aLogLevel) +{ + OT_UNUSED_VARIABLE(aLogLevel); +} + +#endif + } // namespace ot diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index 026613e7e..a184a09b1 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -207,7 +207,7 @@ public: * @param[in] aLogLevel A log level. * */ - static void SetLogLevel(LogLevel aLogLevel) { sLogLevel = aLogLevel; } + static void SetLogLevel(LogLevel aLogLevel); #endif /**