diff --git a/src/posix/platform/logging.c b/src/posix/platform/logging.c index 789a8509c..f4fe6d590 100644 --- a/src/posix/platform/logging.c +++ b/src/posix/platform/logging.c @@ -28,7 +28,6 @@ #include "platform-posix.h" #include -#include #include #include @@ -42,26 +41,36 @@ #include "code_utils.h" -// Macro to append content to end of the log string. +#define LOGGING_MAX_LOG_STRING_SIZE 512 -#define LOG_PRINTF(...) \ - charsWritten = snprintf(&logString[offset], sizeof(logString) - offset, __VA_ARGS__); \ - otEXPECT_ACTION(charsWritten >= 0, logString[offset] = 0); \ - offset += (unsigned int)charsWritten; \ - otEXPECT_ACTION(offset < sizeof(logString), logString[sizeof(logString) - 1] = 0) +void platformLoggingInit(const char *aName) +{ +#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) || \ + (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NCP_SPINEL) + + openlog(aName, LOG_PID, LOG_USER); + setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE)); + +#else + (void)aName; +#endif +} #if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) || \ (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NCP_SPINEL) OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { - char logString[512]; + char logString[LOGGING_MAX_LOG_STRING_SIZE]; unsigned int offset; int charsWritten; va_list args; offset = 0; - LOG_PRINTF("[%" PRIx64 "] ", NODE_ID); + charsWritten = snprintf(&logString[offset], sizeof(logString), "[%" PRIx64 "] ", NODE_ID); + otEXPECT_ACTION(charsWritten >= 0, logString[offset] = 0); + offset += (unsigned int)charsWritten; + otEXPECT_ACTION(offset < sizeof(logString), logString[sizeof(logString) - 1] = 0); va_start(args, aFormat); charsWritten = vsnprintf(&logString[offset], sizeof(logString) - offset, aFormat, args); @@ -70,11 +79,7 @@ OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const otEXPECT_ACTION(charsWritten >= 0, logString[offset] = 0); exit: -#ifndef _WIN32 syslog(LOG_CRIT, "%s", logString); -#else - printf("%s\r\n", logString); -#endif (void)aLogLevel; (void)aLogRegion; diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 61bf7b331..bfbefc16a 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -190,6 +190,14 @@ void platformRadioProcess(otInstance *aInstance, fd_set *aReadFdSet, fd_set *aWr */ void platformRandomInit(void); +/** + * This function initializes the logging service used by OpenThread. + * + * @param[in] aName A name string which will be prefixed to each log line. + * + */ +void platformLoggingInit(const char *aName); + /** * This function updates the file descriptor sets with file descriptors used by the UART driver. * diff --git a/src/posix/platform/system.c b/src/posix/platform/system.c index 442100c15..00d5b9e9c 100644 --- a/src/posix/platform/system.c +++ b/src/posix/platform/system.c @@ -41,7 +41,6 @@ #include #include #include -#include #include #include @@ -100,8 +99,7 @@ void otSysInit(int aArgCount, char *aArgVector[]) radioConfig = aArgVector[i + 1]; } - openlog(basename(aArgVector[0]), LOG_PID, LOG_USER); - setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE)); + platformLoggingInit(basename(aArgVector[0])); #if OPENTHREAD_POSIX_VIRTUAL_TIME otSimInit();