Nigel Rantor / azure_c_shared_utility

Fork of azure_c_shared_utility 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, 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 AZ_LOG_INFO:
00023         (void)printf("Info: ");
00024         break;
00025     case AZ_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 void xlogging_set_log_function(LOGGER_LOG log_function)
00046 {
00047     global_log_function = log_function;
00048 }
00049 
00050 LOGGER_LOG xlogging_get_log_function(void)
00051 {
00052     return global_log_function;
00053 }
00054 
00055 #if (defined(_MSC_VER)) && (!(defined WINCE))
00056 
00057 LOGGER_LOG_GETLASTERROR global_log_function_GetLastError = consolelogger_log_with_GetLastError;
00058 
00059 void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function_GetLastError)
00060 {
00061     global_log_function_GetLastError = log_function_GetLastError;
00062 }
00063 
00064 LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void)
00065 {
00066     return global_log_function_GetLastError;
00067 }
00068 #endif
00069 
00070 /* Print up to 16 bytes per line. */
00071 #define LINE_SIZE 16
00072 
00073 /* Return the printable char for the provided value. */
00074 #define PRINTABLE(c)         ((c >= ' ') && (c <= '~')) ? (char)c : '.'
00075 
00076 /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */
00077 #define HEX_STR(c)           (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A')
00078 
00079 void xlogging_dump_buffer(const void* buf, size_t size)
00080 {
00081     char charBuf[LINE_SIZE + 1];
00082     char hexBuf[LINE_SIZE * 3 + 1];
00083     size_t countbuf = 0;
00084     const unsigned char* bufAsChar = (const unsigned char*)buf;
00085     const unsigned char* startPos = bufAsChar;
00086     
00087     /* Print the whole buffer. */
00088     size_t i = 0;
00089     for (i = 0; i < size; i++)
00090     {
00091         /* Store the printable value of the char in the charBuf to print. */
00092         charBuf[countbuf] = PRINTABLE(*bufAsChar);
00093 
00094         /* Convert the high nibble to a printable hexadecimal value. */
00095         hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4);
00096 
00097         /* Convert the low nibble to a printable hexadecimal value. */
00098         hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar);
00099 
00100         hexBuf[countbuf * 3 + 2] = ' ';
00101 
00102         countbuf++;
00103         bufAsChar++;
00104         /* If the line is full, print it to start another one. */
00105         if (countbuf == LINE_SIZE)
00106         {
00107             charBuf[countbuf] = '\0';
00108             hexBuf[countbuf * 3] = '\0';
00109             LOG(AZ_LOG_TRACE, 0, "%p: %s    %s", startPos, hexBuf, charBuf);
00110             countbuf = 0;
00111             startPos = bufAsChar;
00112         }
00113     }
00114 
00115     /* If the last line does not fit the line size. */
00116     if (countbuf > 0)
00117     {
00118         /* Close the charBuf string. */
00119         charBuf[countbuf] = '\0';
00120 
00121         /* Fill the hexBuf with spaces to keep the charBuf alignment. */
00122         while ((countbuf++) < LINE_SIZE - 1)
00123         {
00124             hexBuf[countbuf * 3] = ' ';
00125             hexBuf[countbuf * 3 + 1] = ' ';
00126             hexBuf[countbuf * 3 + 2] = ' ';
00127         }
00128         hexBuf[countbuf * 3] = '\0';
00129 
00130         /* Print the last line. */
00131         LOG(AZ_LOG_TRACE, 0, "%p: %s    %s", startPos, hexBuf, charBuf);
00132     }
00133 }
00134 
00135 #endif