From 9964e5ee3921f00f88d39c35ed1291b028463634 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 27 Oct 2020 16:59:47 -0700 Subject: [PATCH] [logging] add optional `otPlatLogLine()` & use it in NCP/CLI (#5704) This commit adds a new platform function `otPlatLogLine()`. This function is optional and if not implemented by platform layer, a default weak implementation is provided and used by the OpenThread core using `otPlatLog()`.The new function is used by OpenThread core when the feature `OPENTHREAD_CONFIG_LOG_DEFINE_AS_MACRO_ONLY` is not enabled (which is the default behavior). In this case, the OT core itself will prepare a full log line. This commit also adds implementations of the new platform function for the NCP and CLI modules. --- examples/apps/cli/main.c | 10 ++++++---- include/openthread/cli.h | 10 ++++++++++ include/openthread/instance.h | 2 +- include/openthread/platform/logging.h | 16 ++++++++++++++++ src/cli/cli.cpp | 12 ++++++++++++ src/core/common/logging.cpp | 5 +++++ src/ncp/ncp_base.cpp | 10 ++++++++++ 7 files changed, 60 insertions(+), 5 deletions(-) diff --git a/examples/apps/cli/main.c b/examples/apps/cli/main.c index 184cbd79a..376e04d06 100644 --- a/examples/apps/cli/main.c +++ b/examples/apps/cli/main.c @@ -133,13 +133,15 @@ pseudo_reset: #if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { - OT_UNUSED_VARIABLE(aLogLevel); - OT_UNUSED_VARIABLE(aLogRegion); - OT_UNUSED_VARIABLE(aFormat); - va_list ap; va_start(ap, aFormat); otCliPlatLogv(aLogLevel, aLogRegion, aFormat, ap); va_end(ap); } + +void otPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aLogLine) +{ + otCliPlatLogLine(aLogLevel, aLogRegion, aLogLine); +} + #endif diff --git a/include/openthread/cli.h b/include/openthread/cli.h index 8b640427e..5a8f54c43 100644 --- a/include/openthread/cli.h +++ b/include/openthread/cli.h @@ -164,6 +164,16 @@ void otCliAppendResult(otError aError); */ void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs); +/** + * Function to write the OpenThread Log to the CLI console. + * + * @param[in] aLogLevel The log level. + * @param[in] aLogRegion The log region. + * @param[in] aLogLine A pointer to the log line string. + * + */ +void otCliPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aLogLine); + /** * @} * diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 63b7ed9cd..6a69a630a 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 (39) +#define OPENTHREAD_API_VERSION (40) /** * @addtogroup api-instance diff --git a/include/openthread/platform/logging.h b/include/openthread/platform/logging.h index 3744825b9..28676608f 100644 --- a/include/openthread/platform/logging.h +++ b/include/openthread/platform/logging.h @@ -150,6 +150,22 @@ typedef enum otLogRegion */ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...); +/** + * This (optional) platform function outputs a prepared log line. + * + * This platform function is used by OpenThread core when `OPENTHREAD_CONFIG_LOG_DEFINE_AS_MACRO_ONLY` is not enabled + * (in this case, the OT core itself will prepare a full log line). + * + * Note that this function is optional and if not provided by platform layer, a default (weak) implementation is + * provided and used by OpenThread core as `otPlatLog(aLogLevel, aLogResion, "%s", aLogLine)`. + * + * @param[in] aLogLevel The log level. + * @param[in] aLogRegion The log region. + * @param[in] aLogLine A pointer to a log line string. + * + */ +void otPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aLogLine); + /** * @} * diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 13e726913..184395c23 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -4641,6 +4641,18 @@ exit: return; } +extern "C" void otCliPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aLogLine) +{ + OT_UNUSED_VARIABLE(aLogLevel); + OT_UNUSED_VARIABLE(aLogRegion); + + VerifyOrExit(Interpreter::IsInitialized()); + Interpreter::GetInterpreter().OutputLine(aLogLine); + +exit: + return; +} + } // namespace Cli } // namespace ot diff --git a/src/core/common/logging.cpp b/src/core/common/logging.cpp index 7dca10260..a582726fe 100644 --- a/src/core/common/logging.cpp +++ b/src/core/common/logging.cpp @@ -390,6 +390,11 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat } #endif +OT_TOOL_WEAK void otPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aLogLine) +{ + otPlatLog(aLogLevel, aLogRegion, "%s", aLogLine); +} + #ifdef __cplusplus } #endif diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 83a87243e..152c6fe6b 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -2469,4 +2469,14 @@ extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const ch va_end(args); } +extern "C" void otPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aLogLine) +{ + ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance(); + + if (ncp != nullptr) + { + ncp->Log(aLogLevel, aLogRegion, aLogLine); + } +} + #endif // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP)