From d20d54a394588d6604a8cb983ff7c31ecc697d5f Mon Sep 17 00:00:00 2001 From: Buke Po Date: Tue, 27 Sep 2016 01:00:50 +0800 Subject: [PATCH] correct va_list usage (#686) --- include/cli/cli-uart.h | 5 +++-- src/cli/cli_uart.cpp | 16 +++++++++++----- src/cli/cli_uart.hpp | 11 +++++++++++ src/ncp/ncp_uart.cpp | 3 ++- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/include/cli/cli-uart.h b/include/cli/cli-uart.h index 0f6c9b9bd..0ad8a402a 100644 --- a/include/cli/cli-uart.h +++ b/include/cli/cli-uart.h @@ -35,6 +35,7 @@ #ifndef CLI_UART_H_ #define CLI_UART_H_ +#include #include #include @@ -57,10 +58,10 @@ void otCliUartInit(otInstance *aInstance); * @param[in] aLogLevel The log level. * @param[in] aLogRegion The log region. * @param[in] aFormat A pointer to the format string. - * @param[in] ... Arguments for the format specification. + * @param[in] aAp Arguments pointer for the format specification. * */ -void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...); +void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aAp); #endif #ifdef __cplusplus diff --git a/src/cli/cli_uart.cpp b/src/cli/cli_uart.cpp index d74f24268..3498fdf47 100644 --- a/src/cli/cli_uart.cpp +++ b/src/cli/cli_uart.cpp @@ -179,6 +179,15 @@ int Uart::OutputFormat(const char *fmt, ...) return Output(buf, static_cast(strlen(buf))); } +int Uart::OutputFormatV(const char *aFmt, va_list aAp) +{ + char buf[kMaxLineLength]; + + vsnprintf(buf, sizeof(buf), aFmt, aAp); + + return Output(buf, static_cast(strlen(buf))); +} + void Uart::Send(void) { VerifyOrExit(mSendLength == 0, ;); @@ -219,7 +228,7 @@ void Uart::SendDoneTask(void) #ifdef __cplusplus extern "C" { #endif -void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) +void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aAp) { if (NULL == Uart::sUartServer) { @@ -298,10 +307,7 @@ void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, return; } - va_list args; - va_start(args, aFormat); - Uart::sUartServer->OutputFormat(aFormat, args); - va_end(args); + Uart::sUartServer->OutputFormatV(aFormat, aAp); } #ifdef __cplusplus } // extern "C" diff --git a/src/cli/cli_uart.hpp b/src/cli/cli_uart.hpp index 6ff7a0422..8b9f9e528 100644 --- a/src/cli/cli_uart.hpp +++ b/src/cli/cli_uart.hpp @@ -78,6 +78,17 @@ public: */ int OutputFormat(const char *fmt, ...); + /** + * This method delivers formatted output to the client. + * + * @param[in] aFmt A pointer to the format string. + * @param[in] aAp A variable list of arguments for format. + * + * @returns The number of bytes placed in the output queue. + * + */ + int OutputFormatV(const char *aFmt, va_list aAp); + void ReceiveTask(const uint8_t *aBuf, uint16_t aBufLength); void SendDoneTask(void); diff --git a/src/ncp/ncp_uart.cpp b/src/ncp/ncp_uart.cpp index cd55a3708..f2ed1db91 100644 --- a/src/ncp/ncp_uart.cpp +++ b/src/ncp/ncp_uart.cpp @@ -263,11 +263,12 @@ void NcpUart::HandleError(ThreadError aError, uint8_t *aBuf, uint16_t aBufLength #ifdef __cplusplus extern "C" { #endif -void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) +void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aAp) { (void)aLogLevel; (void)aLogRegion; (void)aFormat; + (void)aAp; } #ifdef __cplusplus } // extern "C"