+++ /dev/null
-/*\r
- * Amazon FreeRTOS Common V1.0.0\r
- * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://aws.amazon.com/freertos\r
- * http://www.FreeRTOS.org\r
- */\r
-\r
-/**\r
- * @file iot_logging.c\r
- * @brief Implementation of logging functions from iot_logging.h\r
- */\r
-\r
-/* The config header is always included first. */\r
-#include "iot_config.h"\r
-\r
-/* Standard includes. */\r
-#include <stdarg.h>\r
-#include <stdio.h>\r
-#include <string.h>\r
-\r
-/* Platform clock include. */\r
-#include "platform/iot_clock.h"\r
-\r
-/* Logging includes. */\r
-#include "private/iot_logging.h"\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/* This implementation assumes the following values for the log level constants.\r
- * Ensure that the values have not been modified. */\r
-#if IOT_LOG_NONE != 0\r
- #error "IOT_LOG_NONE must be 0."\r
-#endif\r
-#if IOT_LOG_ERROR != 1\r
- #error "IOT_LOG_ERROR must be 1."\r
-#endif\r
-#if IOT_LOG_WARN != 2\r
- #error "IOT_LOG_WARN must be 2."\r
-#endif\r
-#if IOT_LOG_INFO != 3\r
- #error "IOT_LOG_INFO must be 3."\r
-#endif\r
-#if IOT_LOG_DEBUG != 4\r
- #error "IOT_LOG_DEBUG must be 4."\r
-#endif\r
-\r
-/**\r
- * @def IotLogging_Puts( message )\r
- * @brief Function the logging library uses to print a line.\r
- *\r
- * This function can be set by using a define. By default, the standard library\r
- * [puts](http://pubs.opengroup.org/onlinepubs/9699919799/functions/puts.html)\r
- * function is used.\r
- */\r
-#ifndef IotLogging_Puts\r
- #define IotLogging_Puts puts\r
-#endif\r
-\r
-/*\r
- * Provide default values for undefined memory allocation functions based on\r
- * the usage of dynamic memory allocation.\r
- */\r
-#if IOT_STATIC_MEMORY_ONLY == 1\r
- /* Static memory allocation header. */\r
- #include "private/iot_static_memory.h"\r
-\r
-/**\r
- * @brief Allocate a new logging buffer. This function must have the same\r
- * signature as [malloc](http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html).\r
- */\r
- #ifndef IotLogging_Malloc\r
- #define IotLogging_Malloc Iot_MallocMessageBuffer\r
- #endif\r
-\r
-/**\r
- * @brief Free a logging buffer. This function must have the same signature\r
- * as [free](http://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html).\r
- */\r
- #ifndef IotLogging_Free\r
- #define IotLogging_Free Iot_FreeMessageBuffer\r
- #endif\r
-\r
-/**\r
- * @brief Get the size of a logging buffer. Statically-allocated buffers\r
- * should all have the same size.\r
- */\r
- #ifndef IotLogging_StaticBufferSize\r
- #define IotLogging_StaticBufferSize Iot_MessageBufferSize\r
- #endif\r
-#else /* if IOT_STATIC_MEMORY_ONLY == 1 */\r
- #ifndef IotLogging_Malloc\r
- #include <stdlib.h>\r
- #define IotLogging_Malloc malloc\r
- #endif\r
-\r
- #ifndef IotLogging_Free\r
- #include <stdlib.h>\r
- #define IotLogging_Free free\r
- #endif\r
-#endif /* if IOT_STATIC_MEMORY_ONLY == 1 */\r
-\r
-/**\r
- * @brief A guess of the maximum length of a timestring.\r
- *\r
- * There's no way for this logging library to know the length of a timestring\r
- * before it's generated. Therefore, the logging library will assume a maximum\r
- * length of any timestring it may get. This value should be generous enough\r
- * to accommodate the vast majority of timestrings.\r
- *\r
- * @see @ref platform_clock_function_gettimestring\r
- */\r
-#define MAX_TIMESTRING_LENGTH ( 64 )\r
-\r
-/**\r
- * @brief The longest string in #_pLogLevelStrings (below), plus 3 to accommodate\r
- * `[]` and a null-terminator.\r
- */\r
-#define MAX_LOG_LEVEL_LENGTH ( 8 )\r
-\r
-/**\r
- * @brief How many bytes @ref logging_function_genericprintbuffer should output on\r
- * each line.\r
- */\r
-#define BYTES_PER_LINE ( 16 )\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/**\r
- * @brief Lookup table for log levels.\r
- *\r
- * Converts one of the @ref logging_constants_levels to a string.\r
- */\r
-static const char * const _pLogLevelStrings[ 5 ] =\r
-{\r
- "", /* IOT_LOG_NONE */\r
- "ERROR", /* IOT_LOG_ERROR */\r
- "WARN ", /* IOT_LOG_WARN */\r
- "INFO ", /* IOT_LOG_INFO */\r
- "DEBUG" /* IOT_LOG_DEBUG */\r
-};\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-#if !defined( IOT_STATIC_MEMORY_ONLY ) || ( IOT_STATIC_MEMORY_ONLY == 0 )\r
- static bool _reallocLoggingBuffer( void ** pOldBuffer,\r
- size_t newSize,\r
- size_t oldSize )\r
- {\r
- bool status = false;\r
-\r
- /* Allocate a new, larger buffer. */\r
- void * pNewBuffer = IotLogging_Malloc( newSize );\r
-\r
- /* Ensure that memory allocation succeeded. */\r
- if( pNewBuffer != NULL )\r
- {\r
- /* Copy the data from the old buffer to the new buffer. */\r
- ( void ) memcpy( pNewBuffer, *pOldBuffer, oldSize );\r
-\r
- /* Free the old buffer and update the pointer. */\r
- IotLogging_Free( *pOldBuffer );\r
- *pOldBuffer = pNewBuffer;\r
-\r
- status = true;\r
- }\r
-\r
- return status;\r
- }\r
-#endif /* if !defined( IOT_STATIC_MEMORY_ONLY ) || ( IOT_STATIC_MEMORY_ONLY == 0 ) */\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-void IotLog_Generic( int libraryLogSetting,\r
- const char * const pLibraryName,\r
- int messageLevel,\r
- const IotLogConfig_t * const pLogConfig,\r
- const char * const pFormat,\r
- ... )\r
-{\r
- int requiredMessageSize = 0;\r
- size_t bufferSize = 0,\r
- bufferPosition = 0, timestringLength = 0;\r
- char * pLoggingBuffer = NULL;\r
- va_list args;\r
-\r
- /* If the library's log level setting is lower than the message level,\r
- * return without doing anything. */\r
- if( ( messageLevel == 0 ) || ( messageLevel > libraryLogSetting ) )\r
- {\r
- return;\r
- }\r
-\r
- if( ( pLogConfig == NULL ) || ( pLogConfig->hideLogLevel == false ) )\r
- {\r
- /* Add length of log level if requested. */\r
- bufferSize += MAX_LOG_LEVEL_LENGTH;\r
- }\r
-\r
- /* Estimate the amount of buffer needed for this log message. */\r
- if( ( pLogConfig == NULL ) || ( pLogConfig->hideLibraryName == false ) )\r
- {\r
- /* Add size of library name if requested. Add 2 to accommodate "[]". */\r
- bufferSize += strlen( pLibraryName ) + 2;\r
- }\r
-\r
- if( ( pLogConfig == NULL ) || ( pLogConfig->hideTimestring == false ) )\r
- {\r
- /* Add length of timestring if requested. */\r
- bufferSize += MAX_TIMESTRING_LENGTH;\r
- }\r
-\r
- /* Add 64 as an initial (arbitrary) guess for the length of the message. */\r
- bufferSize += 64;\r
-\r
- /* In static memory mode, check that the log message will fit in the a\r
- * static buffer. */\r
- #if IOT_STATIC_MEMORY_ONLY == 1\r
- if( bufferSize >= IotLogging_StaticBufferSize() )\r
- {\r
- /* If the static buffers are likely too small to fit the log message,\r
- * return. */\r
- return;\r
- }\r
-\r
- /* Otherwise, update the buffer size to the size of a static buffer. */\r
- bufferSize = IotLogging_StaticBufferSize();\r
- #endif\r
-\r
- /* Allocate memory for the logging buffer. */\r
- pLoggingBuffer = ( char * ) IotLogging_Malloc( bufferSize );\r
-\r
- if( pLoggingBuffer == NULL )\r
- {\r
- return;\r
- }\r
-\r
- /* Print the message log level if requested. */\r
- if( ( pLogConfig == NULL ) || ( pLogConfig->hideLogLevel == false ) )\r
- {\r
- /* Ensure that message level is valid. */\r
- if( ( messageLevel >= IOT_LOG_NONE ) && ( messageLevel <= IOT_LOG_DEBUG ) )\r
- {\r
- /* Add the log level string to the logging buffer. */\r
- requiredMessageSize = snprintf( pLoggingBuffer + bufferPosition,\r
- bufferSize - bufferPosition,\r
- "[%s]",\r
- _pLogLevelStrings[ messageLevel ] );\r
-\r
- /* Check for encoding errors. */\r
- if( requiredMessageSize <= 0 )\r
- {\r
- IotLogging_Free( pLoggingBuffer );\r
-\r
- return;\r
- }\r
-\r
- /* Update the buffer position. */\r
- bufferPosition += ( size_t ) requiredMessageSize;\r
- }\r
- }\r
-\r
- /* Print the library name if requested. */\r
- if( ( pLogConfig == NULL ) || ( pLogConfig->hideLibraryName == false ) )\r
- {\r
- /* Add the library name to the logging buffer. */\r
- requiredMessageSize = snprintf( pLoggingBuffer + bufferPosition,\r
- bufferSize - bufferPosition,\r
- "[%s]",\r
- pLibraryName );\r
-\r
- /* Check for encoding errors. */\r
- if( requiredMessageSize <= 0 )\r
- {\r
- IotLogging_Free( pLoggingBuffer );\r
-\r
- return;\r
- }\r
-\r
- /* Update the buffer position. */\r
- bufferPosition += ( size_t ) requiredMessageSize;\r
- }\r
-\r
- /* Print the timestring if requested. */\r
- if( ( pLogConfig == NULL ) || ( pLogConfig->hideTimestring == false ) )\r
- {\r
- /* Add the opening '[' enclosing the timestring. */\r
- pLoggingBuffer[ bufferPosition ] = '[';\r
- bufferPosition++;\r
-\r
- /* Generate the timestring and add it to the buffer. */\r
- if( IotClock_GetTimestring( pLoggingBuffer + bufferPosition,\r
- bufferSize - bufferPosition,\r
- ×tringLength ) == true )\r
- {\r
- /* If the timestring was successfully generated, add the closing "]". */\r
- bufferPosition += timestringLength;\r
- pLoggingBuffer[ bufferPosition ] = ']';\r
- bufferPosition++;\r
- }\r
- else\r
- {\r
- /* Sufficient memory for a timestring should have been allocated. A timestring\r
- * probably failed to generate due to a clock read error; remove the opening '['\r
- * from the logging buffer. */\r
- bufferPosition--;\r
- pLoggingBuffer[ bufferPosition ] = '\0';\r
- }\r
- }\r
-\r
- /* Add a padding space between the last closing ']' and the message, unless\r
- * the logging buffer is empty. */\r
- if( bufferPosition > 0 )\r
- {\r
- pLoggingBuffer[ bufferPosition ] = ' ';\r
- bufferPosition++;\r
- }\r
-\r
- va_start( args, pFormat );\r
-\r
- /* Add the log message to the logging buffer. */\r
- requiredMessageSize = vsnprintf( pLoggingBuffer + bufferPosition,\r
- bufferSize - bufferPosition,\r
- pFormat,\r
- args );\r
-\r
- va_end( args );\r
-\r
- /* If the logging buffer was too small to fit the log message, reallocate\r
- * a larger logging buffer. */\r
- if( ( size_t ) requiredMessageSize >= bufferSize - bufferPosition )\r
- {\r
- #if IOT_STATIC_MEMORY_ONLY == 1\r
-\r
- /* There's no point trying to allocate a larger static buffer. Return\r
- * immediately. */\r
- IotLogging_Free( pLoggingBuffer );\r
-\r
- return;\r
- #else\r
- if( _reallocLoggingBuffer( ( void ** ) &pLoggingBuffer,\r
- ( size_t ) requiredMessageSize + bufferPosition + 1,\r
- bufferSize ) == false )\r
- {\r
- /* If buffer reallocation failed, return. */\r
- IotLogging_Free( pLoggingBuffer );\r
-\r
- return;\r
- }\r
-\r
- /* Reallocation successful, update buffer size. */\r
- bufferSize = ( size_t ) requiredMessageSize + bufferPosition + 1;\r
-\r
- /* Add the log message to the buffer. Now that the buffer has been\r
- * reallocated, this should succeed. */\r
- va_start( args, pFormat );\r
- requiredMessageSize = vsnprintf( pLoggingBuffer + bufferPosition,\r
- bufferSize - bufferPosition,\r
- pFormat,\r
- args );\r
- va_end( args );\r
- #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */\r
- }\r
-\r
- /* Check for encoding errors. */\r
- if( requiredMessageSize <= 0 )\r
- {\r
- IotLogging_Free( pLoggingBuffer );\r
-\r
- return;\r
- }\r
-\r
- /* Print the logging buffer to stdout. */\r
- IotLogging_Puts( pLoggingBuffer );\r
-\r
- /* Free the logging buffer. */\r
- IotLogging_Free( pLoggingBuffer );\r
-}\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-void IotLog_GenericPrintBuffer( const char * const pLibraryName,\r
- const char * const pHeader,\r
- const uint8_t * const pBuffer,\r
- size_t bufferSize )\r
-{\r
- size_t i = 0, offset = 0;\r
-\r
- /* Allocate memory to hold each line of the log message. Since each byte\r
- * of pBuffer is printed in 4 characters (2 digits, a space, and a null-\r
- * terminator), the size of each line is 4 * BYTES_PER_LINE. */\r
- char * pMessageBuffer = IotLogging_Malloc( 4 * BYTES_PER_LINE );\r
-\r
- /* Exit if no memory is available. */\r
- if( pMessageBuffer == NULL )\r
- {\r
- return;\r
- }\r
-\r
- /* Print pHeader before printing pBuffer. */\r
- if( pHeader != NULL )\r
- {\r
- IotLog_Generic( IOT_LOG_DEBUG,\r
- pLibraryName,\r
- IOT_LOG_DEBUG,\r
- NULL,\r
- pHeader );\r
- }\r
-\r
- /* Print each byte in pBuffer. */\r
- for( i = 0; i < bufferSize; i++ )\r
- {\r
- /* Print a line if BYTES_PER_LINE is reached. But don't print a line\r
- * at the beginning (when i=0). */\r
- if( ( i % BYTES_PER_LINE == 0 ) && ( i != 0 ) )\r
- {\r
- IotLogging_Puts( pMessageBuffer );\r
-\r
- /* Reset offset so that pMessageBuffer is filled from the beginning. */\r
- offset = 0;\r
- }\r
-\r
- /* Print a single byte into pMessageBuffer. */\r
- ( void ) snprintf( pMessageBuffer + offset, 4, "%02x ", pBuffer[ i ] );\r
-\r
- /* Move the offset where the next character is printed. */\r
- offset += 3;\r
- }\r
-\r
- /* Print the final line of bytes. This line isn't printed by the for-loop above. */\r
- IotLogging_Puts( pMessageBuffer );\r
-\r
- /* Free memory used by this function. */\r
- IotLogging_Free( pMessageBuffer );\r
-}\r
-\r
-/*-----------------------------------------------------------*/\r