mirror of
https://github.com/espressif/openthread.git
synced 2026-06-05 21:14:49 +00:00
[platform] implement otPlatLogOutput platform API (#12762)
This commit provides the platform-level implementation for the instance-aware logging API `otPlatLogOutput()`. This API is used when `OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE` is enabled, allowing the platform to receive the `otInstance` pointer with each log line. The new API is implemented across: - The simulation platform logging. - The POSIX platform using `syslog()`. - The NCP base to route logs to the NCP host. - The CLI logging module. - Unit tests and mock platforms. The `OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE` configuration is also enabled for Toranj simulations to support multi-instance log testing.
This commit is contained in:
committed by
GitHub
parent
b5e2393c7d
commit
c923900de0
@@ -90,6 +90,25 @@ void platformLoggingDeinit(void)
|
||||
}
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
|
||||
void otPlatLogOutput(otInstance *aInstance, otLogLevel aLogLevel, const char *aLogLine)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aInstance);
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
|
||||
if (sLogFile == NULL)
|
||||
{
|
||||
syslog(LOG_CRIT, "[%lu] %s", (unsigned long)gNodeId, aLogLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(sLogFile, "%s\r\n", aLogLine);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
@@ -118,9 +137,11 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
#else
|
||||
#endif // OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
|
||||
#else // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
|
||||
void platformLoggingInit(const char *aName) { OT_UNUSED_VARIABLE(aName); }
|
||||
void platformLoggingDeinit(void) {}
|
||||
|
||||
#endif // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
#endif
|
||||
|
||||
@@ -120,6 +120,15 @@ otError otPlatDebugUart_logfile(const char *filename)
|
||||
}
|
||||
|
||||
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART)
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *aInstance, otLogLevel aLogLevel, const char *aLogLine)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aInstance);
|
||||
otPlatLog(aLogLevel, OT_LOG_REGION_CORE, "%s", aLogLine);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* this should not be a WEAK function */
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
|
||||
@@ -41,6 +41,14 @@
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
extern "C" void otPlatLogOutput(otInstance *aInstance, otLogLevel aLogLevel, const char *aLogLine)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aInstance);
|
||||
otPlatLog(aLogLevel, OT_LOG_REGION_CORE, "%s", aLogLine);
|
||||
}
|
||||
#endif
|
||||
|
||||
extern "C" OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
@@ -2847,6 +2847,22 @@ otError otNcpStreamWrite(int aStreamId, const uint8_t *aDataPtr, int aDataLen)
|
||||
|
||||
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP)
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
|
||||
extern "C" void otPlatLogOutput(otInstance *aInstance, otLogLevel aLogLevel, const char *aLogLine)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aInstance);
|
||||
|
||||
ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance();
|
||||
|
||||
if (ncp != nullptr)
|
||||
{
|
||||
ncp->Log(aLogLevel, OT_LOG_REGION_CORE, aLogLine);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
va_list args;
|
||||
@@ -2866,4 +2882,6 @@ extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const ch
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
|
||||
#endif // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP)
|
||||
|
||||
@@ -36,40 +36,53 @@
|
||||
#include <openthread/platform/logging.h>
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
static int ConvertLogLevelToSyslogLevel(otLogLevel aLogLevel)
|
||||
{
|
||||
int level = LOG_DEBUG;
|
||||
|
||||
switch (aLogLevel)
|
||||
{
|
||||
case OT_LOG_LEVEL_NONE:
|
||||
aLogLevel = LOG_ALERT;
|
||||
level = LOG_ALERT;
|
||||
break;
|
||||
case OT_LOG_LEVEL_CRIT:
|
||||
aLogLevel = LOG_CRIT;
|
||||
level = LOG_CRIT;
|
||||
break;
|
||||
case OT_LOG_LEVEL_WARN:
|
||||
aLogLevel = LOG_WARNING;
|
||||
level = LOG_WARNING;
|
||||
break;
|
||||
case OT_LOG_LEVEL_NOTE:
|
||||
aLogLevel = LOG_NOTICE;
|
||||
level = LOG_NOTICE;
|
||||
break;
|
||||
case OT_LOG_LEVEL_INFO:
|
||||
aLogLevel = LOG_INFO;
|
||||
level = LOG_INFO;
|
||||
break;
|
||||
case OT_LOG_LEVEL_DEBG:
|
||||
aLogLevel = LOG_DEBUG;
|
||||
level = LOG_DEBUG;
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
aLogLevel = LOG_DEBUG;
|
||||
break;
|
||||
}
|
||||
|
||||
return level;
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
OT_TOOL_WEAK void otPlatLogOutput(otInstance *, otLogLevel aLogLevel, const char *aLogLine)
|
||||
{
|
||||
syslog(ConvertLogLevelToSyslogLevel(aLogLevel), "%s", aLogLine);
|
||||
}
|
||||
#else
|
||||
OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, aFormat);
|
||||
vsyslog(aLogLevel, aFormat, args);
|
||||
vsyslog(ConvertLogLevelToSyslogLevel(aLogLevel), aFormat, args);
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
|
||||
@@ -496,6 +496,8 @@ void otPlatDiagRadioReceived(otInstance *, otRadioFrame *, otError) {}
|
||||
|
||||
void otPlatDiagAlarmCallback(otInstance *) {}
|
||||
|
||||
OT_TOOL_WEAK void otPlatLogOutput(otInstance *, otLogLevel, const char *) {}
|
||||
|
||||
OT_TOOL_WEAK void otPlatLog(otLogLevel, otLogRegion, const char *, ...) {}
|
||||
|
||||
void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
|
||||
|
||||
@@ -89,6 +89,8 @@
|
||||
|
||||
#define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
|
||||
#define OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE 1
|
||||
|
||||
#define OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE 1
|
||||
|
||||
#define OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_LEVEL OT_LOG_LEVEL_INFO
|
||||
|
||||
@@ -69,11 +69,11 @@ void AdvanceNowTo(uint32_t aNewNow);
|
||||
extern "C" {
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
@@ -83,6 +83,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------------------------------------------
|
||||
// otPlatAlarams
|
||||
|
||||
@@ -138,11 +138,11 @@ void otPlatFree(void *aPtr)
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
@@ -152,6 +152,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // extern "C"
|
||||
|
||||
|
||||
@@ -139,11 +139,11 @@ void otPlatFree(void *aPtr)
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
@@ -153,6 +153,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // extern "C"
|
||||
|
||||
|
||||
+10
-10
@@ -1677,26 +1677,26 @@ static void SendEmtryPtrQueryWithKnownAnswers(const char *aName, const KnownAnsw
|
||||
|
||||
extern "C" {
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
OT_UNUSED_VARIABLE(aFormat);
|
||||
|
||||
#if ENABLE_TEST_LOG
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
va_start(args, aFormat);
|
||||
vprintf(aFormat, args);
|
||||
va_end(args);
|
||||
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // ENABLE_TEST_LOG
|
||||
|
||||
//----------------------------------------------------------------------------------------------------------------------
|
||||
// `otPlatAlarm`
|
||||
|
||||
@@ -280,6 +280,8 @@ OT_TOOL_WEAK otPlatResetReason otPlatGetResetReason(otInstance *) { return OT_PL
|
||||
|
||||
OT_TOOL_WEAK void otPlatWakeHost(void) {}
|
||||
|
||||
OT_TOOL_WEAK void otPlatLogOutput(otInstance *, otLogLevel, const char *) {}
|
||||
|
||||
OT_TOOL_WEAK void otPlatLog(otLogLevel, otLogRegion, const char *, ...) {}
|
||||
|
||||
OT_TOOL_WEAK void otPlatSettingsInit(otInstance *, const uint16_t *, uint16_t) {}
|
||||
|
||||
@@ -242,11 +242,11 @@ void DiscoverNat64Prefix(const Ip6::Prefix &aPrefix);
|
||||
extern "C" {
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
@@ -256,6 +256,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------------------------------------------
|
||||
// `otPlatRadio
|
||||
|
||||
@@ -458,11 +458,11 @@ void otPlatFree(void *aPtr)
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
@@ -472,6 +472,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // extern "C"
|
||||
|
||||
|
||||
@@ -134,11 +134,11 @@ void otPlatFree(void *aPtr)
|
||||
#endif
|
||||
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
#if OPENTHREAD_CONFIG_LOG_INSTANCE_AWARE_API_ENABLE
|
||||
void otPlatLogOutput(otInstance *, otLogLevel, const char *aLogLine) { printf(" %s\n", aLogLine); }
|
||||
#else
|
||||
void otPlatLog(otLogLevel, otLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
va_list args;
|
||||
|
||||
printf(" ");
|
||||
@@ -148,6 +148,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
printf("\n");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // extern "C"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user