diff --git a/configure.ac b/configure.ac index 46cf4535c..871d24905 100644 --- a/configure.ac +++ b/configure.ac @@ -657,34 +657,34 @@ AM_CONDITIONAL([OPENTHREAD_ENABLE_LEGACY], [test "${enable_legacy}" = "yes"]) AC_DEFINE_UNQUOTED([OPENTHREAD_ENABLE_LEGACY],[${OPENTHREAD_ENABLE_LEGACY}],[Define to 1 if you want to use legacy network support]) # -# Cli Logging +# Default Logging # -AC_ARG_ENABLE(cli_logging, - [AS_HELP_STRING([--enable-cli-logging],[Enable cli logging support @<:@default=no@:>@.])], +AC_ARG_ENABLE(default_logging, + [AS_HELP_STRING([--enable-default-logging],[Enable default logging support @<:@default=no@:>@.])], [ case "${enableval}" in no|yes) - enable_cli_logging=${enableval} + enable_default_logging=${enableval} ;; *) - AC_MSG_ERROR([Invalid value ${enable_cli_logging} for --enable-cli-logging]) + AC_MSG_ERROR([Invalid value ${enable_default_logging} for --enable-default-logging]) ;; esac ], - [enable_cli_logging=no]) + [enable_default_logging=no]) -if test "$enable_cli_logging" = "yes"; then - OPENTHREAD_ENABLE_CLI_LOGGING=1 +if test "$enable_default_logging" = "yes"; then + OPENTHREAD_ENABLE_DEFAULT_LOGGING=1 else - OPENTHREAD_ENABLE_CLI_LOGGING=0 + OPENTHREAD_ENABLE_DEFAULT_LOGGING=0 fi -AC_SUBST(OPENTHREAD_ENABLE_CLI_LOGGING) -AM_CONDITIONAL([OPENTHREAD_ENABLE_CLI_LOGGING], [test "${enable_cli_logging}" = "yes"]) -AC_DEFINE_UNQUOTED([OPENTHREAD_ENABLE_CLI_LOGGING],[${OPENTHREAD_ENABLE_CLI_LOGGING}],[Define to 1 if you want to enable cli logging]) +AC_SUBST(OPENTHREAD_ENABLE_DEFAULT_LOGGING) +AM_CONDITIONAL([OPENTHREAD_ENABLE_DEFAULT_LOGGING], [test "${enable_default_logging}" = "yes"]) +AC_DEFINE_UNQUOTED([OPENTHREAD_ENABLE_DEFAULT_LOGGING],[${OPENTHREAD_ENABLE_DEFAULT_LOGGING}],[Define to 1 if you want to enable default logging]) # # Log for certification test @@ -1066,7 +1066,7 @@ AC_MSG_NOTICE([ OpenThread MAC Whitelist support : ${enable_mac_whitelist} OpenThread Diagnostics support : ${enable_diag} OpenThread Legacy network support : ${enable_legacy} - OpenThread Cli logging support : ${enable_cli_logging} + OpenThread Default logging support : ${enable_default_logging} OpenThread Certification log support : ${enable_cert_log} OpenThread DHCPv6 Server support : ${enable_dhcp6_server} OpenThread DHCPv6 Client support : ${enable_dhcp6_client} diff --git a/examples/Makefile-cc2538 b/examples/Makefile-cc2538 index bd7d5a9d5..a3649d0b0 100644 --- a/examples/Makefile-cc2538 +++ b/examples/Makefile-cc2538 @@ -45,14 +45,11 @@ configure_OPTIONS = \ --enable-cli \ --enable-ncp \ --enable-diag \ + --enable-default-logging \ --with-examples=cc2538 \ --with-platform-info=CC2538 \ $(NULL) -ifeq ($(CLI_LOGGING),1) -configure_OPTIONS += --enable-cli-logging -endif - ifeq ($(CERT_LOG),1) configure_OPTIONS += --enable-cert-log endif diff --git a/examples/platforms/cc2538/Makefile.am b/examples/platforms/cc2538/Makefile.am index 6e11d9cd8..3ec32f0d2 100644 --- a/examples/platforms/cc2538/Makefile.am +++ b/examples/platforms/cc2538/Makefile.am @@ -39,7 +39,6 @@ libopenthread_cc2538_a_CPPFLAGS = \ libopenthread_cc2538_a_SOURCES = \ alarm.c \ flash.c \ - logging.c \ misc.c \ platform.c \ radio.c \ diff --git a/examples/platforms/cc2538/logging.c b/examples/platforms/cc2538/logging.c deleted file mode 100644 index 7354494e8..000000000 --- a/examples/platforms/cc2538/logging.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2016, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * This file implements the OpenThread platform abstraction for logging. - * - */ - -#ifdef OPENTHREAD_CONFIG_FILE -#include OPENTHREAD_CONFIG_FILE -#else -#include -#endif - - -#include -#if OPENTHREAD_ENABLE_CLI_LOGGING -#include -#include -#include -#include -#include -#include - -#include -#include -#endif - -void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) -{ -#if OPENTHREAD_ENABLE_CLI_LOGGING - va_list args; - va_start(args, aFormat); - otCliLog(aLogLevel, aLogRegion, aFormat, args); - va_end(args); -#else - (void)aLogLevel; - (void)aLogRegion; - (void)aFormat; -#endif // OPENTHREAD_ENABLE_CLI_LOGGING -} diff --git a/examples/platforms/posix/logging.c b/examples/platforms/posix/logging.c index 7d234fd47..7ef82d376 100644 --- a/examples/platforms/posix/logging.c +++ b/examples/platforms/posix/logging.c @@ -62,79 +62,6 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat LOG_PRINTF("%s.%06d ", timeString, (uint32_t)tv.tv_usec); - switch (aLogLevel) - { - case kLogLevelNone: - LOG_PRINTF("NONE "); - break; - - case kLogLevelCrit: - LOG_PRINTF("CRIT "); - break; - - case kLogLevelWarn: - LOG_PRINTF("WARN "); - break; - - case kLogLevelInfo: - LOG_PRINTF("INFO "); - break; - - case kLogLevelDebg: - LOG_PRINTF("DEBG "); - break; - } - - switch (aLogRegion) - { - case kLogRegionApi: - LOG_PRINTF("API "); - break; - - case kLogRegionMle: - LOG_PRINTF("MLE "); - break; - - case kLogRegionArp: - LOG_PRINTF("ARP "); - break; - - case kLogRegionNetData: - LOG_PRINTF("NETD "); - break; - - case kLogRegionIp6: - LOG_PRINTF("IPV6 "); - break; - - case kLogRegionIcmp: - LOG_PRINTF("ICMP "); - break; - - case kLogRegionMac: - LOG_PRINTF("MAC "); - break; - - case kLogRegionMem: - LOG_PRINTF("MEM "); - break; - - case kLogRegionNcp: - LOG_PRINTF("NCP "); - break; - - case kLogRegionMeshCoP: - LOG_PRINTF("MCOP "); - break; - - case kLogRegionNetDiag: - LOG_PRINTF("NDG "); - break; - - case kLogRegionPlatform: - LOG_PRINTF("PLAT "); - } - va_start(args, aFormat); charsWritten = vsnprintf(&logString[offset], sizeof(logString) - offset, aFormat, args); va_end(args); @@ -143,5 +70,8 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat exit: fprintf(stderr, "%s\r\n", logString); + + (void)aLogLevel; + (void)aLogRegion; } diff --git a/include/cli/cli-uart.h b/include/cli/cli-uart.h index 0ad8a402a..2131a3d05 100644 --- a/include/cli/cli-uart.h +++ b/include/cli/cli-uart.h @@ -37,7 +37,6 @@ #include #include -#include #ifdef __cplusplus extern "C" { @@ -51,19 +50,6 @@ extern "C" { */ void otCliUartInit(otInstance *aInstance); -#if OPENTHREAD_ENABLE_CLI_LOGGING -/** - * This method delivers formatted log to the client. - * - * @param[in] aLogLevel The log level. - * @param[in] aLogRegion The log region. - * @param[in] aFormat A pointer to the format string. - * @param[in] aAp Arguments pointer for the format specification. - * - */ -void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aAp); -#endif - #ifdef __cplusplus } // extern "C" #endif diff --git a/include/openthread-windows-config.h b/include/openthread-windows-config.h index 87b425a7a..f2dc0aa49 100644 --- a/include/openthread-windows-config.h +++ b/include/openthread-windows-config.h @@ -26,6 +26,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ +/* Define to 1 if you want to enable default logging */ +#define OPENTHREAD_ENABLE_DEFAULT_LOGGING 1 + /* Define to 1 to enable the commissioner role. */ #define OPENTHREAD_ENABLE_COMMISSIONER 1 diff --git a/src/cli/cli_uart.cpp b/src/cli/cli_uart.cpp index 5406cb7eb..f165f7831 100644 --- a/src/cli/cli_uart.cpp +++ b/src/cli/cli_uart.cpp @@ -228,96 +228,30 @@ void Uart::SendDoneTask(void) Send(); } -#if OPENTHREAD_ENABLE_CLI_LOGGING +#if OPENTHREAD_ENABLE_DEFAULT_LOGGING #ifdef __cplusplus extern "C" { #endif -void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aAp) +void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { if (NULL == Uart::sUartServer) { return; } - switch (aLogLevel) - { - case kLogLevelNone: - Uart::sUartServer->OutputFormat("NONE "); - break; - - case kLogLevelCrit: - Uart::sUartServer->OutputFormat("CRIT "); - break; - - case kLogLevelWarn: - Uart::sUartServer->OutputFormat("WARN "); - break; - - case kLogLevelInfo: - Uart::sUartServer->OutputFormat("INFO "); - break; - - case kLogLevelDebg: - Uart::sUartServer->OutputFormat("DEBG "); - break; - - default: - return; - } - - switch (aLogRegion) - { - case kLogRegionApi: - Uart::sUartServer->OutputFormat("API "); - break; - - case kLogRegionMle: - Uart::sUartServer->OutputFormat("MLE "); - break; - - case kLogRegionArp: - Uart::sUartServer->OutputFormat("ARP "); - break; - - case kLogRegionNetData: - Uart::sUartServer->OutputFormat("NETD "); - break; - - case kLogRegionIp6: - Uart::sUartServer->OutputFormat("IPV6 "); - break; - - case kLogRegionIcmp: - Uart::sUartServer->OutputFormat("ICMP "); - break; - - case kLogRegionMac: - Uart::sUartServer->OutputFormat("MAC "); - break; - - case kLogRegionMem: - Uart::sUartServer->OutputFormat("MEM "); - break; - - case kLogRegionNcp: - Uart::sUartServer->OutputFormat("NCP "); - break; - - case kLogRegionMeshCoP: - Uart::sUartServer->OutputFormat("MCOP "); - break; - - default: - return; - } - - Uart::sUartServer->OutputFormatV(aFormat, aAp); + va_list args; + va_start(args, aFormat); + Uart::sUartServer->OutputFormatV(aFormat, args); Uart::sUartServer->OutputFormat("\r\n"); + va_end(args); + + (void)aLogLevel; + (void)aLogRegion; } #ifdef __cplusplus } // extern "C" #endif -#endif // OPENTHREAD_ENABLE_CLI_LOGGING +#endif // OPENTHREAD_ENABLE_DEFAULT_LOGGING } // namespace Cli } // namespace Thread diff --git a/src/core/common/logging.cpp b/src/core/common/logging.cpp index 4e01310be..1c7919b26 100644 --- a/src/core/common/logging.cpp +++ b/src/core/common/logging.cpp @@ -148,6 +148,42 @@ void otDump(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aId, const otLogDump("%s", buf); } +#ifdef OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL +const char *otLogLevelToString(otLogLevel aLevel) +{ + const char *retval; + + switch (aLevel) + { + case kLogLevelNone: + retval = "NONE"; + break; + + case kLogLevelCrit: + retval = "CRIT"; + break; + + case kLogLevelWarn: + retval = "WARN"; + break; + + case kLogLevelInfo: + retval = "INFO"; + break; + + case kLogLevelDebg: + retval = "DEBG"; + break; + + default: + retval = "----"; + break; + } + + return retval; +} +#endif // OPENTHREAD_CONFIG_LOG_PREPEND_REGION + #ifdef OPENTHREAD_CONFIG_LOG_PREPEND_REGION const char *otLogRegionToString(otLogRegion aRegion) { diff --git a/src/core/common/logging.hpp b/src/core/common/logging.hpp index 5672407b2..666ca454b 100644 --- a/src/core/common/logging.hpp +++ b/src/core/common/logging.hpp @@ -1197,6 +1197,18 @@ extern "C" { */ void otDump(otLogLevel aLevel, otLogRegion aRegion, const char *aId, const void *aBuf, const size_t aLength); +#ifdef OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL +/** +* This method converts the log level value into a string +* +* @param[in] aLevel The log level. +* +* @returns A const char pointer to the C string corresponding to the log level. +* +*/ +const char *otLogLevelToString(otLogLevel aLevel); +#endif + #ifdef OPENTHREAD_CONFIG_LOG_PREPEND_REGION /** * This method converts the log region value into a string @@ -1207,10 +1219,48 @@ void otDump(otLogLevel aLevel, otLogRegion aRegion, const char *aId, const void * */ const char *otLogRegionToString(otLogRegion aRegion); +#endif + +#ifdef OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL + +#ifdef OPENTHREAD_CONFIG_LOG_PREPEND_REGION /** * Local/private macro to format the log message */ +#define _otLogFormatter(aLogLevel, aRegion, aFormat, ...) \ + otPlatLog( \ + aLogLevel, \ + aRegion, \ + "[%s]%s: " aFormat OPENTHREAD_CONFIG_LOG_SUFFIX, \ + otLogLevelToString(aLogLevel), \ + otLogRegionToString(aRegion), \ + ## __VA_ARGS__ \ + ) + +#else // OPENTHREAD_CONFIG_LOG_PREPEND_REGION + +/** +* Local/private macro to format the log message +*/ +#define _otLogFormatter(aLogLevel, aRegion, aFormat, ...) \ + otPlatLog( \ + aLogLevel, \ + aRegion, \ + "[%s]: " aFormat OPENTHREAD_CONFIG_LOG_SUFFIX, \ + otLogLevelToString(aLogLevel), \ + ## __VA_ARGS__ \ + ) + +#endif + +#else // OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL + +#ifdef OPENTHREAD_CONFIG_LOG_PREPEND_REGION + +/** +* Local/private macro to format the log message +*/ #define _otLogFormatter(aLogLevel, aRegion, aFormat, ...) \ otPlatLog( \ aLogLevel, \ @@ -1223,8 +1273,8 @@ const char *otLogRegionToString(otLogRegion aRegion); #else // OPENTHREAD_CONFIG_LOG_PREPEND_REGION /** - * Local/private macro to format the log message - */ +* Local/private macro to format the log message +*/ #define _otLogFormatter(aLogLevel, aRegion, aFormat, ...) \ otPlatLog( \ aLogLevel, \ @@ -1233,6 +1283,8 @@ const char *otLogRegionToString(otLogRegion aRegion); ## __VA_ARGS__ \ ) +#endif + #endif // OPENTHREAD_CONFIG_LOG_PREPEND_REGION #ifdef __cplusplus diff --git a/src/core/openthread-core-default-config.h b/src/core/openthread-core-default-config.h index fdb250fef..d089d2790 100644 --- a/src/core/openthread-core-default-config.h +++ b/src/core/openthread-core-default-config.h @@ -390,11 +390,19 @@ //#define OPENTHREAD_CONFIG_LOG_PLATFORM /** - * @def OPENTHREAD_CONFIG_LOG_PREPREND_REGION + * @def OPENTHREAD_CONFIG_LOG_PREPREND_LEVEL * - * Define to prepend the log region to all log messages + * Define to prepend the log level to all log messages * */ +#define OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL + +/** +* @def OPENTHREAD_CONFIG_LOG_PREPREND_REGION +* +* Define to prepend the log region to all log messages +* +*/ #define OPENTHREAD_CONFIG_LOG_PREPEND_REGION /** diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 3c0c02312..fc8de78f0 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -5376,13 +5376,20 @@ ThreadError otNcpStreamWrite(int aStreamId, const uint8_t* aDataPtr, int aDataLe aStreamId = SPINEL_PROP_STREAM_DEBUG; } - return Thread::sNcpContext->SendPropertyUpdate( - SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, - SPINEL_CMD_PROP_VALUE_IS, - static_cast(aStreamId), - aDataPtr, - static_cast(aDataLen) - ); + if (Thread::sNcpContext) + { + return Thread::sNcpContext->SendPropertyUpdate( + SPINEL_HEADER_FLAG | SPINEL_HEADER_IID_0, + SPINEL_CMD_PROP_VALUE_IS, + static_cast(aStreamId), + aDataPtr, + static_cast(aDataLen) + ); + } + else + { + return kThreadError_InvalidState; + } } // ---------------------------------------------------------------------------- diff --git a/src/ncp/ncp_uart.cpp b/src/ncp/ncp_uart.cpp index 07022a695..f45d9dab8 100644 --- a/src/ncp/ncp_uart.cpp +++ b/src/ncp/ncp_uart.cpp @@ -266,16 +266,25 @@ void NcpUart::HandleError(ThreadError aError, uint8_t *aBuf, uint16_t aBufLength otNcpStreamWrite(0, reinterpret_cast(hexbuf + 1), static_cast(strlen(hexbuf) - 1)); } -#if OPENTHREAD_ENABLE_CLI_LOGGING +#if OPENTHREAD_ENABLE_DEFAULT_LOGGING #ifdef __cplusplus extern "C" { #endif -void otCliLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aAp) +void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) { + char logString[128]; + int charsWritten; + va_list args; + + va_start(args, aFormat); + if ((charsWritten = vsnprintf(logString, sizeof(logString), aFormat, args)) > 0) + { + otNcpStreamWrite(0, reinterpret_cast(logString), charsWritten); + } + va_end(args); + (void)aLogLevel; (void)aLogRegion; - (void)aFormat; - (void)aAp; } #ifdef __cplusplus } // extern "C"