summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/common/logging/iot_logging.c
blob: aac8d3181266b55fc76ee4a7c6178c23b7c08c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Amazon FreeRTOS Common V1.0.0
 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://aws.amazon.com/freertos
 * http://www.FreeRTOS.org
 */

/**
 * @file iot_logging.c
 * @brief Implementation of logging functions from iot_logging.h
 */

/* The config header is always included first. */
#include "iot_config.h"

/* Standard includes. */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

/* Platform clock include. */
#include "platform/iot_clock.h"

/* Logging includes. */
#include "private/iot_logging.h"

/*-----------------------------------------------------------*/

/* This implementation assumes the following values for the log level constants.
 * Ensure that the values have not been modified. */
#if IOT_LOG_NONE != 0
    #error "IOT_LOG_NONE must be 0."
#endif
#if IOT_LOG_ERROR != 1
    #error "IOT_LOG_ERROR must be 1."
#endif
#if IOT_LOG_WARN != 2
    #error "IOT_LOG_WARN must be 2."
#endif
#if IOT_LOG_INFO != 3
    #error "IOT_LOG_INFO must be 3."
#endif
#if IOT_LOG_DEBUG != 4
    #error "IOT_LOG_DEBUG must be 4."
#endif

/**
 * @def IotLogging_Puts( message )
 * @brief Function the logging library uses to print a line.
 *
 * This function can be set by using a define. By default, the standard library
 * [puts](http://pubs.opengroup.org/onlinepubs/9699919799/functions/puts.html)
 * function is used.
 */
#ifndef IotLogging_Puts
    #define IotLogging_Puts    puts
#endif

/*
 * Provide default values for undefined memory allocation functions based on
 * the usage of dynamic memory allocation.
 */
#if IOT_STATIC_MEMORY_ONLY == 1
    /* Static memory allocation header. */
    #include "private/iot_static_memory.h"

/**
 * @brief Allocate a new logging buffer. This function must have the same
 * signature as [malloc](http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html).
 */
    #ifndef IotLogging_Malloc
        #define IotLogging_Malloc    Iot_MallocMessageBuffer
    #endif

/**
 * @brief Free a logging buffer. This function must have the same signature
 * as [free](http://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html).
 */
    #ifndef IotLogging_Free
        #define IotLogging_Free    Iot_FreeMessageBuffer
    #endif

/**
 * @brief Get the size of a logging buffer. Statically-allocated buffers
 * should all have the same size.
 */
    #ifndef IotLogging_StaticBufferSize
        #define IotLogging_StaticBufferSize    Iot_MessageBufferSize
    #endif
#else /* if IOT_STATIC_MEMORY_ONLY == 1 */
    #ifndef IotLogging_Malloc
        #include <stdlib.h>
        #define IotLogging_Malloc    malloc
    #endif

    #ifndef IotLogging_Free
        #include <stdlib.h>
        #define IotLogging_Free    free
    #endif
#endif /* if IOT_STATIC_MEMORY_ONLY == 1 */

/**
 * @brief 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 @ref platform_clock_function_gettimestring
 */
#define MAX_TIMESTRING_LENGTH    ( 64 )

/**
 * @brief The longest string in #_pLogLevelStrings (below), plus 3 to accommodate
 * `[]` and a null-terminator.
 */
#define MAX_LOG_LEVEL_LENGTH     ( 8 )

/**
 * @brief How many bytes @ref logging_function_genericprintbuffer should output on
 * each line.
 */
#define BYTES_PER_LINE           ( 16 )

/*-----------------------------------------------------------*/

/**
 * @brief Lookup table for log levels.
 *
 * Converts one of the @ref logging_constants_levels to a string.
 */
static const char * const _pLogLevelStrings[ 5 ] =
{
    "",      /* IOT_LOG_NONE */
    "ERROR", /* IOT_LOG_ERROR */
    "WARN ", /* IOT_LOG_WARN */
    "INFO ", /* IOT_LOG_INFO */
    "DEBUG"  /* IOT_LOG_DEBUG */
};

/*-----------------------------------------------------------*/

#if !defined( IOT_STATIC_MEMORY_ONLY ) || ( IOT_STATIC_MEMORY_ONLY == 0 )
    static bool _reallocLoggingBuffer( void ** pOldBuffer,
                                       size_t newSize,
                                       size_t oldSize )
    {
        bool status = false;

        /* Allocate a new, larger buffer. */
        void * pNewBuffer = IotLogging_Malloc( newSize );

        /* Ensure that memory allocation succeeded. */
        if( pNewBuffer != NULL )
        {
            /* Copy the data from the old buffer to the new buffer. */
            ( void ) memcpy( pNewBuffer, *pOldBuffer, oldSize );

            /* Free the old buffer and update the pointer. */
            IotLogging_Free( *pOldBuffer );
            *pOldBuffer = pNewBuffer;

            status = true;
        }

        return status;
    }
#endif /* if !defined( IOT_STATIC_MEMORY_ONLY ) || ( IOT_STATIC_MEMORY_ONLY == 0 ) */

/*-----------------------------------------------------------*/

void IotLog_Generic( int libraryLogSetting,
                     const char * const pLibraryName,
                     int messageLevel,
                     const IotLogConfig_t * const pLogConfig,
                     const char * const pFormat,
                     ... )
{
    int requiredMessageSize = 0;
    size_t bufferSize = 0,
           bufferPosition = 0, timestringLength = 0;
    char * pLoggingBuffer = NULL;
    va_list args;

    /* If the library's log level setting is lower than the message level,
     * return without doing anything. */
    if( ( messageLevel == 0 ) || ( messageLevel > libraryLogSetting ) )
    {
        return;
    }

    if( ( pLogConfig == NULL ) || ( pLogConfig->hideLogLevel == false ) )
    {
        /* Add length of log level if requested. */
        bufferSize += MAX_LOG_LEVEL_LENGTH;
    }

    /* Estimate the amount of buffer needed for this log message. */
    if( ( pLogConfig == NULL ) || ( pLogConfig->hideLibraryName == false ) )
    {
        /* Add size of library name if requested. Add 2 to accommodate "[]". */
        bufferSize += strlen( pLibraryName ) + 2;
    }

    if( ( pLogConfig == NULL ) || ( pLogConfig->hideTimestring == false ) )
    {
        /* Add length of timestring if requested. */
        bufferSize += MAX_TIMESTRING_LENGTH;
    }

    /* Add 64 as an initial (arbitrary) guess for the length of the message. */
    bufferSize += 64;

    /* In static memory mode, check that the log message will fit in the a
     * static buffer. */
    #if IOT_STATIC_MEMORY_ONLY == 1
        if( bufferSize >= IotLogging_StaticBufferSize() )
        {
            /* If the static buffers are likely too small to fit the log message,
             * return. */
            return;
        }

        /* Otherwise, update the buffer size to the size of a static buffer. */
        bufferSize = IotLogging_StaticBufferSize();
    #endif

    /* Allocate memory for the logging buffer. */
    pLoggingBuffer = ( char * ) IotLogging_Malloc( bufferSize );

    if( pLoggingBuffer == NULL )
    {
        return;
    }

    /* Print the message log level if requested. */
    if( ( pLogConfig == NULL ) || ( pLogConfig->hideLogLevel == false ) )
    {
        /* Ensure that message level is valid. */
        if( ( messageLevel >= IOT_LOG_NONE ) && ( messageLevel <= IOT_LOG_DEBUG ) )
        {
            /* Add the log level string to the logging buffer. */
            requiredMessageSize = snprintf( pLoggingBuffer + bufferPosition,
                                            bufferSize - bufferPosition,
                                            "[%s]",
                                            _pLogLevelStrings[ messageLevel ] );

            /* Check for encoding errors. */
            if( requiredMessageSize <= 0 )
            {
                IotLogging_Free( pLoggingBuffer );

                return;
            }

            /* Update the buffer position. */
            bufferPosition += ( size_t ) requiredMessageSize;
        }
    }

    /* Print the library name if requested. */
    if( ( pLogConfig == NULL ) || ( pLogConfig->hideLibraryName == false ) )
    {
        /* Add the library name to the logging buffer. */
        requiredMessageSize = snprintf( pLoggingBuffer + bufferPosition,
                                        bufferSize - bufferPosition,
                                        "[%s]",
                                        pLibraryName );

        /* Check for encoding errors. */
        if( requiredMessageSize <= 0 )
        {
            IotLogging_Free( pLoggingBuffer );

            return;
        }

        /* Update the buffer position. */
        bufferPosition += ( size_t ) requiredMessageSize;
    }

    /* Print the timestring if requested. */
    if( ( pLogConfig == NULL ) || ( pLogConfig->hideTimestring == false ) )
    {
        /* Add the opening '[' enclosing the timestring. */
        pLoggingBuffer[ bufferPosition ] = '[';
        bufferPosition++;

        /* Generate the timestring and add it to the buffer. */
        if( IotClock_GetTimestring( pLoggingBuffer + bufferPosition,
                                    bufferSize - bufferPosition,
                                    &timestringLength ) == true )
        {
            /* If the timestring was successfully generated, add the closing "]". */
            bufferPosition += timestringLength;
            pLoggingBuffer[ bufferPosition ] = ']';
            bufferPosition++;
        }
        else
        {
            /* Sufficient memory for a timestring should have been allocated. A timestring
             * probably failed to generate due to a clock read error; remove the opening '['
             * from the logging buffer. */
            bufferPosition--;
            pLoggingBuffer[ bufferPosition ] = '\0';
        }
    }

    /* Add a padding space between the last closing ']' and the message, unless
     * the logging buffer is empty. */
    if( bufferPosition > 0 )
    {
        pLoggingBuffer[ bufferPosition ] = ' ';
        bufferPosition++;
    }

    va_start( args, pFormat );

    /* Add the log message to the logging buffer. */
    requiredMessageSize = vsnprintf( pLoggingBuffer + bufferPosition,
                                     bufferSize - bufferPosition,
                                     pFormat,
                                     args );

    va_end( args );

    /* If the logging buffer was too small to fit the log message, reallocate
     * a larger logging buffer. */
    if( ( size_t ) requiredMessageSize >= bufferSize - bufferPosition )
    {
        #if IOT_STATIC_MEMORY_ONLY == 1

            /* There's no point trying to allocate a larger static buffer. Return
             * immediately. */
            IotLogging_Free( pLoggingBuffer );

            return;
        #else
            if( _reallocLoggingBuffer( ( void ** ) &pLoggingBuffer,
                                       ( size_t ) requiredMessageSize + bufferPosition + 1,
                                       bufferSize ) == false )
            {
                /* If buffer reallocation failed, return. */
                IotLogging_Free( pLoggingBuffer );

                return;
            }

            /* Reallocation successful, update buffer size. */
            bufferSize = ( size_t ) requiredMessageSize + bufferPosition + 1;

            /* Add the log message to the buffer. Now that the buffer has been
             * reallocated, this should succeed. */
            va_start( args, pFormat );
            requiredMessageSize = vsnprintf( pLoggingBuffer + bufferPosition,
                                             bufferSize - bufferPosition,
                                             pFormat,
                                             args );
            va_end( args );
        #endif /* if IOT_STATIC_MEMORY_ONLY == 1 */
    }

    /* Check for encoding errors. */
    if( requiredMessageSize <= 0 )
    {
        IotLogging_Free( pLoggingBuffer );

        return;
    }

    /* Print the logging buffer to stdout. */
    IotLogging_Puts( pLoggingBuffer );

    /* Free the logging buffer. */
    IotLogging_Free( pLoggingBuffer );
}

/*-----------------------------------------------------------*/

void IotLog_GenericPrintBuffer( const char * const pLibraryName,
                                const char * const pHeader,
                                const uint8_t * const pBuffer,
                                size_t bufferSize )
{
    size_t i = 0, offset = 0;

    /* Allocate memory to hold each line of the log message. Since each byte
     * of pBuffer is printed in 4 characters (2 digits, a space, and a null-
     * terminator), the size of each line is 4 * BYTES_PER_LINE. */
    char * pMessageBuffer = IotLogging_Malloc( 4 * BYTES_PER_LINE );

    /* Exit if no memory is available. */
    if( pMessageBuffer == NULL )
    {
        return;
    }

    /* Print pHeader before printing pBuffer. */
    if( pHeader != NULL )
    {
        IotLog_Generic( IOT_LOG_DEBUG,
                        pLibraryName,
                        IOT_LOG_DEBUG,
                        NULL,
                        pHeader );
    }

    /* Print each byte in pBuffer. */
    for( i = 0; i < bufferSize; i++ )
    {
        /* Print a line if BYTES_PER_LINE is reached. But don't print a line
         * at the beginning (when i=0). */
        if( ( i % BYTES_PER_LINE == 0 ) && ( i != 0 ) )
        {
            IotLogging_Puts( pMessageBuffer );

            /* Reset offset so that pMessageBuffer is filled from the beginning. */
            offset = 0;
        }

        /* Print a single byte into pMessageBuffer. */
        ( void ) snprintf( pMessageBuffer + offset, 4, "%02x ", pBuffer[ i ] );

        /* Move the offset where the next character is printed. */
        offset += 3;
    }

    /* Print the final line of bytes. This line isn't printed by the for-loop above. */
    IotLogging_Puts( pMessageBuffer );

    /* Free memory used by this function. */
    IotLogging_Free( pMessageBuffer );
}

/*-----------------------------------------------------------*/