Skip to content

Commit

Permalink
[logging] add new otLogPlat() APIs with sub-module name (openthread…
Browse files Browse the repository at this point in the history
…#9516)

This commit adds new public OT logging APIs `otLogPlat()` and
`otLogPlatArgs()`, which allow the caller to specify a platform
sub-module name to be included in the emitted log. This makes it
easier to distinguish logs from the platform layer and filter the
logs.

This commit also updates the `RadioSpinel` class to use the new API,
ensuring that its logs use the "P-RadioSpinel" module name.
  • Loading branch information
abtink authored Oct 17, 2023
1 parent 1d81283 commit cd5768b
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 77 deletions.
2 changes: 1 addition & 1 deletion include/openthread/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (366)
#define OPENTHREAD_API_VERSION (367)

/**
* @addtogroup api-instance
Expand Down
39 changes: 39 additions & 0 deletions include/openthread/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,45 @@ void otDumpInfoPlat(const char *aText, const void *aData, uint16_t aDataLength);
*/
void otDumpDebgPlat(const char *aText, const void *aData, uint16_t aDataLength);

/**
* Emits a log message at given log level using a platform module name.
*
* This is is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
* level is below @p aLogLevel , this function does not emit any log message.
*
* The @p aPlatModuleName name is used to determine the log module name in the emitted log message, following the
* `P-{PlatModuleName}---` format. This means that the prefix string "P-" is added to indicate that this is a platform
* sub-module, followed by the next 12 characters of the @p PlatModuleName string, with padded hyphens `-` at the end
* to ensure that the region name is 14 characters long.
* @param[in] aLogLevel The log level.
* @param[in] aPlatModuleName The platform sub-module name.
* @param[in] aFormat The format string.
* @param[in] ... Arguments for the format specification.
*
*/
void otLogPlat(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, ...)
OT_TOOL_PRINTF_STYLE_FORMAT_ARG_CHECK(3, 4);

/**
* Emits a log message at given log level using a platform module name.
*
* This is is intended for use by platform. If `OPENTHREAD_CONFIG_LOG_PLATFORM` is not set or the current log
* level is below @p aLogLevel , this function does not emit any log message.
*
* The @p aPlatModuleName name is used to determine the log module name in the emitted log message, following the
* `P-{PlatModuleName}---` format. This means that the prefix string "P-" is added to indicate that this is a platform
* sub-module, followed by the next 12 characters of the @p PlatModuleName string, with padded hyphens `-` at the end
* to ensure that the region name is 14 characters long.
*
* @param[in] aLogLevel The log level.
* @param[in] aPlatModuleName The platform sub-module name.
* @param[in] aFormat The format string.
* @param[in] aArgs Arguments for the format specification.
*
*/
void otLogPlatArgs(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, va_list aArgs);

/**
* Emits a log message at a given log level.
*
Expand Down
33 changes: 33 additions & 0 deletions src/core/api/logging_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "common/instance.hpp"
#include "common/locator_getters.hpp"
#include "common/log.hpp"
#include "common/string.hpp"

using namespace ot;

Expand Down Expand Up @@ -179,6 +180,38 @@ void otDumpDebgPlat(const char *aText, const void *aData, uint16_t aDataLength)
#endif
}

void otLogPlat(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, ...)
{
#if OPENTHREAD_CONFIG_LOG_PLATFORM
va_list args;

va_start(args, aFormat);
otLogPlatArgs(aLogLevel, aPlatModuleName, aFormat, args);
va_end(args);
#else
OT_UNUSED_VARIABLE(aLogLevel);
OT_UNUSED_VARIABLE(aPlatModuleName);
OT_UNUSED_VARIABLE(aFormat);
#endif
}

void otLogPlatArgs(otLogLevel aLogLevel, const char *aPlatModuleName, const char *aFormat, va_list aArgs)
{
#if OT_SHOULD_LOG && OPENTHREAD_CONFIG_LOG_PLATFORM
String<kMaxLogModuleNameLength> moduleName;

OT_ASSERT(aLogLevel >= kLogLevelNone && aLogLevel <= kLogLevelDebg);

moduleName.Append("P-%s", aPlatModuleName);
Logger::LogVarArgs(moduleName.AsCString(), static_cast<LogLevel>(aLogLevel), aFormat, aArgs);
#else
OT_UNUSED_VARIABLE(aLogLevel);
OT_UNUSED_VARIABLE(aPlatModuleName);
OT_UNUSED_VARIABLE(aFormat);
OT_UNUSED_VARIABLE(aArgs);
#endif
}

void otLogCli(otLogLevel aLogLevel, const char *aFormat, ...)
{
#if OT_SHOULD_LOG && OPENTHREAD_CONFIG_LOG_CLI
Expand Down
Loading

0 comments on commit cd5768b

Please sign in to comment.