AWS IoT Device SDK C: Logging
Generate and print log messages
Return to main page ↑
iot_logging.c File Reference

Implementation of logging functions from iot_logging.h. More...

#include "iot_config.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "platform/iot_clock.h"
#include "iot_logging.h"
#include "iot_static_memory.h"

Macros

#define IotLogging_Puts   puts
 Function the logging library uses to print a line. More...
 
#define IotLogging_Malloc   Iot_MallocMessageBuffer
 Allocate a new logging buffer. This function must have the same signature as malloc.
 
#define IotLogging_Free   Iot_FreeMessageBuffer
 Free a logging buffer. This function must have the same signature as free.
 
#define IotLogging_StaticBufferSize   Iot_MessageBufferSize
 Get the size of a logging buffer. Statically-allocated buffers should all have the same size.
 
#define MAX_TIMESTRING_LENGTH   ( 64 )
 A guess of the maximum length of a timestring. More...
 
#define MAX_LOG_LEVEL_LENGTH   ( 8 )
 The longest string in _pLogLevelStrings (below), plus 3 to accommodate [] and a null-terminator.
 
#define BYTES_PER_LINE   ( 16 )
 How many bytes IotLog_PrintBuffer should output on each line.
 

Functions

void IotLog_Generic (int32_t libraryLogSetting, const char *const pLibraryName, int32_t messageLevel, const IotLogConfig_t *const pLogConfig, const char *const pFormat,...)
 Generic logging function that prints a single message. More...
 
void IotLog_GenericPrintBuffer (const char *const pLibraryName, const char *const pHeader, const uint8_t *const pBuffer, size_t bufferSize)
 Generic function to log the contents of a buffer as bytes. More...
 

Variables

static const char *const _pLogLevelStrings [5]
 Lookup table for log levels. More...
 

Detailed Description

Implementation of logging functions from iot_logging.h.

Macro Definition Documentation

◆ IotLogging_Puts

#define IotLogging_Puts   puts

Function the logging library uses to print a line.

This function can be set by using a define. By default, the standard library puts function is used.

◆ MAX_TIMESTRING_LENGTH

#define MAX_TIMESTRING_LENGTH   ( 64 )

A guess of the maximum length of a timestring.

There's no way for this logging library to know the length of a timestring before it's generated. Therefore, the logging library will assume a maximum length of any timestring it may get. This value should be generous enough to accommodate the vast majority of timestrings.

See also
IotClock_GetTimestring

Function Documentation

◆ IotLog_Generic()

void IotLog_Generic ( int32_t  libraryLogSetting,
const char *const  pLibraryName,
int32_t  messageLevel,
const IotLogConfig_t *const  pLogConfig,
const char *const  pFormat,
  ... 
)

Generic logging function that prints a single message.

This function is the generic logging function shared across all libraries. The library-specific logging function IotLog is implemented using this function. Like IotLog, this function is only available when LIBRARY_LOG_LEVEL is IOT_LOG_NONE.

In most cases, the library-specific logging function IotLog should be called instead of this function.

Parameters
[in]libraryLogSettingThe log level setting of the library, used to determine if the log message should be printed. Must be one of the Log levels.
[in]pLibraryNameThe library name to print. See LIBRARY_LOG_NAME.
[in]messageLevelThe log level of the this message. See LIBRARY_LOG_LEVEL.
[in]pLogConfigPointer to a IotLogConfig_t. Optional; pass NULL to ignore.
[in]pFormatFormat string for the log message.
[in]...Arguments for format specification.
Returns
No return value. On errors, it prints nothing.

◆ IotLog_GenericPrintBuffer()

void IotLog_GenericPrintBuffer ( const char *const  pLibraryName,
const char *const  pHeader,
const uint8_t *const  pBuffer,
size_t  bufferSize 
)

Generic function to log the contents of a buffer as bytes.

This function is the generic buffer logging function shared across all libraries. The library-specific buffer logging function IotLog_PrintBuffer is implemented using this function. Like IotLog_PrintBuffer, this function is only available when LIBRARY_LOG_LEVEL is IOT_LOG_DEBUG.

In most cases, the library-specific buffer logging function IotLog_PrintBuffer should be called instead of this function.

Parameters
[in]pLibraryNameThe library name to print with the log. See LIBRARY_LOG_NAME.
[in]pHeaderA message to print before printing the buffer.
[in]pBufferThe buffer to print.
[in]bufferSizeThe number of bytes in pBuffer to print.
Returns
No return value. On errors, it prints nothing.
Note
To conserve memory, this function only allocates enough memory for a single line of output. Therefore, in multithreaded systems, its output may appear "fragmented" if other threads are logging simultaneously.

Variable Documentation

◆ _pLogLevelStrings

const char* const _pLogLevelStrings[5]
static
Initial value:
=
{
"",
"ERROR",
"WARN ",
"INFO ",
"DEBUG"
}

Lookup table for log levels.

Converts one of the Log levels to a string.