Mark Radbourne / Mbed 2 deprecated iothub_client_sample_amqp

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed

Fork of iothub_client_sample_amqp by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xlogging.c Source File

xlogging.c

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include "azure_c_shared_utility/xlogging.h"
00005 #include "azure_c_shared_utility/consolelogger.h"
00006 
00007 #ifndef NO_LOGGING
00008 
00009 
00010 #ifdef WINCE
00011 #include <stdarg.h>
00012 
00013 void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...)
00014 {
00015     va_list args;
00016     va_start(args, format);
00017 
00018     time_t t = time(NULL);
00019 
00020     switch (log_category)
00021     {
00022     case LOG_INFO:
00023         (void)printf("Info: ");
00024         break;
00025     case LOG_ERROR:
00026         (void)printf("Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line);
00027         break;
00028     default:
00029         break;
00030     }
00031 
00032     (void)vprintf(format, args);
00033     va_end(args);
00034 
00035     (void)log_category;
00036     if (options & LOG_LINE)
00037     {
00038         (void)printf("\r\n");
00039     }
00040 }
00041 #endif
00042 
00043 LOGGER_LOG global_log_function = consolelogger_log;
00044 
00045 
00046 void xlogging_set_log_function(LOGGER_LOG log_function)
00047 {
00048     global_log_function = log_function;
00049 }
00050 
00051 LOGGER_LOG xlogging_get_log_function(void)
00052 {
00053     return global_log_function;
00054 }
00055 
00056 /* Print up to 16 bytes per line. */
00057 #define LINE_SIZE 16
00058 
00059 /* Return the printable char for the provided value. */
00060 #define PRINTABLE(c)         ((c >= ' ') && (c <= '~')) ? (char)c : '.'
00061 
00062 /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */
00063 #define HEX_STR(c)           (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A')
00064 
00065 void xlogging_dump_buffer(const void* buf, size_t size)
00066 {
00067     char charBuf[LINE_SIZE + 1];
00068     char hexBuf[LINE_SIZE * 3 + 1];
00069     char countbuf = 0;
00070     const unsigned char* bufAsChar = (const unsigned char*)buf;
00071     const unsigned char* startPos = bufAsChar;
00072     
00073     /* Print the whole buffer. */
00074     for (size_t i = 0; i < size; i++)
00075     {
00076         /* Store the printable value of the char in the charBuf to print. */
00077         charBuf[countbuf] = PRINTABLE(*bufAsChar);
00078 
00079         /* Convert the high nibble to a printable hexadecimal value. */
00080         hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4);
00081 
00082         /* Convert the low nibble to a printable hexadecimal value. */
00083         hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar);
00084 
00085         hexBuf[countbuf * 3 + 2] = ' ';
00086 
00087         countbuf++;
00088         bufAsChar++;
00089         /* If the line is full, print it to start another one. */
00090         if (countbuf == LINE_SIZE)
00091         {
00092             charBuf[countbuf] = '\0';
00093             hexBuf[countbuf * 3] = '\0';
00094             LOG(LOG_TRACE, 0, "%p: %s    %s", startPos, hexBuf, charBuf);
00095             countbuf = 0;
00096             startPos = bufAsChar;
00097         }
00098     }
00099 
00100     /* If the last line does not fit the line size. */
00101     if (countbuf > 0)
00102     {
00103         /* Close the charBuf string. */
00104         charBuf[countbuf] = '\0';
00105 
00106         /* Fill the hexBuf with spaces to keep the charBuf alignment. */
00107         while ((countbuf++) < LINE_SIZE - 1)
00108         {
00109             hexBuf[countbuf * 3] = ' ';
00110             hexBuf[countbuf * 3 + 1] = ' ';
00111             hexBuf[countbuf * 3 + 2] = ' ';
00112         }
00113         hexBuf[countbuf * 3] = '\0';
00114 
00115         /* Print the last line. */
00116         LOG(LOG_TRACE, 0, "%p: %s    %s", startPos, hexBuf, charBuf);
00117     }
00118 }
00119 
00120 #endif