Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Tue Sep 11 11:15:08 2018 -0700
Revision:
48:81866008bba4
Parent:
45:1119d0f2c4d8
1.2.9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 6:c55b013dfc2a 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 6:c55b013dfc2a 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 6:c55b013dfc2a 3
Azure.IoT Build 6:c55b013dfc2a 4 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 6:c55b013dfc2a 5 #include "azure_c_shared_utility/consolelogger.h"
Azure.IoT Build 6:c55b013dfc2a 6
AzureIoTClient 7:1af47e3a19b6 7 #ifndef NO_LOGGING
AzureIoTClient 7:1af47e3a19b6 8
AzureIoTClient 48:81866008bba4 9 #ifdef WIN32
AzureIoTClient 48:81866008bba4 10 #include "windows.h"
AzureIoTClient 48:81866008bba4 11 #endif // WIN32
AzureIoTClient 48:81866008bba4 12
AzureIoTClient 7:1af47e3a19b6 13
AzureIoTClient 7:1af47e3a19b6 14 #ifdef WINCE
AzureIoTClient 7:1af47e3a19b6 15 #include <stdarg.h>
AzureIoTClient 7:1af47e3a19b6 16
AzureIoTClient 27:8656a313842b 17 void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...)
AzureIoTClient 7:1af47e3a19b6 18 {
AzureIoTClient 45:1119d0f2c4d8 19 va_list args;
AzureIoTClient 45:1119d0f2c4d8 20 va_start(args, format);
AzureIoTClient 7:1af47e3a19b6 21
AzureIoTClient 45:1119d0f2c4d8 22 time_t t = time(NULL);
AzureIoTClient 7:1af47e3a19b6 23
AzureIoTClient 45:1119d0f2c4d8 24 switch (log_category)
AzureIoTClient 45:1119d0f2c4d8 25 {
AzureIoTClient 45:1119d0f2c4d8 26 case AZ_LOG_INFO:
AzureIoTClient 45:1119d0f2c4d8 27 (void)printf("Info: ");
AzureIoTClient 45:1119d0f2c4d8 28 break;
AzureIoTClient 45:1119d0f2c4d8 29 case AZ_LOG_ERROR:
AzureIoTClient 45:1119d0f2c4d8 30 (void)printf("Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line);
AzureIoTClient 45:1119d0f2c4d8 31 break;
AzureIoTClient 45:1119d0f2c4d8 32 default:
AzureIoTClient 45:1119d0f2c4d8 33 break;
AzureIoTClient 45:1119d0f2c4d8 34 }
AzureIoTClient 7:1af47e3a19b6 35
AzureIoTClient 45:1119d0f2c4d8 36 (void)vprintf(format, args);
AzureIoTClient 45:1119d0f2c4d8 37 va_end(args);
AzureIoTClient 7:1af47e3a19b6 38
AzureIoTClient 45:1119d0f2c4d8 39 (void)log_category;
AzureIoTClient 45:1119d0f2c4d8 40 if (options & LOG_LINE)
AzureIoTClient 45:1119d0f2c4d8 41 {
AzureIoTClient 45:1119d0f2c4d8 42 (void)printf("\r\n");
AzureIoTClient 45:1119d0f2c4d8 43 }
AzureIoTClient 7:1af47e3a19b6 44 }
AzureIoTClient 48:81866008bba4 45 #endif // WINCE
AzureIoTClient 7:1af47e3a19b6 46
AzureIoTClient 7:1af47e3a19b6 47 LOGGER_LOG global_log_function = consolelogger_log;
AzureIoTClient 7:1af47e3a19b6 48
Azure.IoT Build 6:c55b013dfc2a 49 void xlogging_set_log_function(LOGGER_LOG log_function)
Azure.IoT Build 6:c55b013dfc2a 50 {
Azure.IoT Build 6:c55b013dfc2a 51 global_log_function = log_function;
Azure.IoT Build 6:c55b013dfc2a 52 }
Azure.IoT Build 6:c55b013dfc2a 53
Azure.IoT Build 6:c55b013dfc2a 54 LOGGER_LOG xlogging_get_log_function(void)
Azure.IoT Build 6:c55b013dfc2a 55 {
Azure.IoT Build 6:c55b013dfc2a 56 return global_log_function;
Azure.IoT Build 6:c55b013dfc2a 57 }
AzureIoTClient 7:1af47e3a19b6 58
AzureIoTClient 27:8656a313842b 59 #if (defined(_MSC_VER)) && (!(defined WINCE))
AzureIoTClient 27:8656a313842b 60
AzureIoTClient 27:8656a313842b 61 LOGGER_LOG_GETLASTERROR global_log_function_GetLastError = consolelogger_log_with_GetLastError;
AzureIoTClient 27:8656a313842b 62
AzureIoTClient 27:8656a313842b 63 void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function_GetLastError)
AzureIoTClient 27:8656a313842b 64 {
AzureIoTClient 27:8656a313842b 65 global_log_function_GetLastError = log_function_GetLastError;
AzureIoTClient 27:8656a313842b 66 }
AzureIoTClient 27:8656a313842b 67
AzureIoTClient 27:8656a313842b 68 LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void)
AzureIoTClient 27:8656a313842b 69 {
AzureIoTClient 27:8656a313842b 70 return global_log_function_GetLastError;
AzureIoTClient 27:8656a313842b 71 }
AzureIoTClient 27:8656a313842b 72 #endif
AzureIoTClient 27:8656a313842b 73
AzureIoTClient 41:dc93369d5ed4 74 /* Print up to 16 bytes per line. */
AzureIoTClient 41:dc93369d5ed4 75 #define LINE_SIZE 16
AzureIoTClient 41:dc93369d5ed4 76
AzureIoTClient 41:dc93369d5ed4 77 /* Return the printable char for the provided value. */
AzureIoTClient 41:dc93369d5ed4 78 #define PRINTABLE(c) ((c >= ' ') && (c <= '~')) ? (char)c : '.'
AzureIoTClient 41:dc93369d5ed4 79
AzureIoTClient 41:dc93369d5ed4 80 /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */
AzureIoTClient 41:dc93369d5ed4 81 #define HEX_STR(c) (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A')
AzureIoTClient 41:dc93369d5ed4 82
AzureIoTClient 41:dc93369d5ed4 83 void LogBinary(const char* comment, const void* data, size_t size)
AzureIoTClient 41:dc93369d5ed4 84 {
AzureIoTClient 41:dc93369d5ed4 85 char charBuf[LINE_SIZE + 1];
AzureIoTClient 41:dc93369d5ed4 86 char hexBuf[LINE_SIZE * 3 + 1];
AzureIoTClient 41:dc93369d5ed4 87 size_t countbuf = 0;
AzureIoTClient 41:dc93369d5ed4 88 size_t i = 0;
AzureIoTClient 41:dc93369d5ed4 89 const unsigned char* bufAsChar = (const unsigned char*)data;
AzureIoTClient 41:dc93369d5ed4 90 const unsigned char* startPos = bufAsChar;
AzureIoTClient 41:dc93369d5ed4 91
AzureIoTClient 41:dc93369d5ed4 92 LOG(AZ_LOG_TRACE, LOG_LINE, "%s %zu bytes", comment, size);
AzureIoTClient 41:dc93369d5ed4 93
AzureIoTClient 41:dc93369d5ed4 94 /* Print the whole buffer. */
AzureIoTClient 41:dc93369d5ed4 95 for (i = 0; i < size; i++)
AzureIoTClient 41:dc93369d5ed4 96 {
AzureIoTClient 41:dc93369d5ed4 97 /* Store the printable value of the char in the charBuf to print. */
AzureIoTClient 41:dc93369d5ed4 98 charBuf[countbuf] = PRINTABLE(*bufAsChar);
AzureIoTClient 41:dc93369d5ed4 99
AzureIoTClient 41:dc93369d5ed4 100 /* Convert the high nibble to a printable hexadecimal value. */
AzureIoTClient 41:dc93369d5ed4 101 hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4);
AzureIoTClient 41:dc93369d5ed4 102
AzureIoTClient 41:dc93369d5ed4 103 /* Convert the low nibble to a printable hexadecimal value. */
AzureIoTClient 41:dc93369d5ed4 104 hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar);
AzureIoTClient 41:dc93369d5ed4 105
AzureIoTClient 41:dc93369d5ed4 106 hexBuf[countbuf * 3 + 2] = ' ';
AzureIoTClient 41:dc93369d5ed4 107
AzureIoTClient 41:dc93369d5ed4 108 countbuf++;
AzureIoTClient 41:dc93369d5ed4 109 bufAsChar++;
AzureIoTClient 41:dc93369d5ed4 110 /* If the line is full, print it to start another one. */
AzureIoTClient 41:dc93369d5ed4 111 if (countbuf == LINE_SIZE)
AzureIoTClient 41:dc93369d5ed4 112 {
AzureIoTClient 41:dc93369d5ed4 113 charBuf[countbuf] = '\0';
AzureIoTClient 41:dc93369d5ed4 114 hexBuf[countbuf * 3] = '\0';
AzureIoTClient 41:dc93369d5ed4 115 LOG(AZ_LOG_TRACE, LOG_LINE, "%p: %s %s", startPos, hexBuf, charBuf);
AzureIoTClient 41:dc93369d5ed4 116 countbuf = 0;
AzureIoTClient 41:dc93369d5ed4 117 startPos = bufAsChar;
AzureIoTClient 41:dc93369d5ed4 118 }
AzureIoTClient 41:dc93369d5ed4 119 }
AzureIoTClient 41:dc93369d5ed4 120
AzureIoTClient 41:dc93369d5ed4 121 /* If the last line does not fit the line size. */
AzureIoTClient 41:dc93369d5ed4 122 if (countbuf > 0)
AzureIoTClient 41:dc93369d5ed4 123 {
AzureIoTClient 41:dc93369d5ed4 124 /* Close the charBuf string. */
AzureIoTClient 41:dc93369d5ed4 125 charBuf[countbuf] = '\0';
AzureIoTClient 41:dc93369d5ed4 126
AzureIoTClient 41:dc93369d5ed4 127 /* Fill the hexBuf with spaces to keep the charBuf alignment. */
AzureIoTClient 41:dc93369d5ed4 128 while ((countbuf++) < LINE_SIZE - 1)
AzureIoTClient 41:dc93369d5ed4 129 {
AzureIoTClient 41:dc93369d5ed4 130 hexBuf[countbuf * 3] = ' ';
AzureIoTClient 41:dc93369d5ed4 131 hexBuf[countbuf * 3 + 1] = ' ';
AzureIoTClient 41:dc93369d5ed4 132 hexBuf[countbuf * 3 + 2] = ' ';
AzureIoTClient 41:dc93369d5ed4 133 }
AzureIoTClient 41:dc93369d5ed4 134 hexBuf[countbuf * 3] = '\0';
AzureIoTClient 41:dc93369d5ed4 135
AzureIoTClient 41:dc93369d5ed4 136 /* Print the last line. */
AzureIoTClient 41:dc93369d5ed4 137 LOG(AZ_LOG_TRACE, LOG_LINE, "%p: %s %s", startPos, hexBuf, charBuf);
AzureIoTClient 41:dc93369d5ed4 138 }
AzureIoTClient 41:dc93369d5ed4 139 }
AzureIoTClient 41:dc93369d5ed4 140
AzureIoTClient 48:81866008bba4 141 #ifdef WIN32
AzureIoTClient 48:81866008bba4 142
AzureIoTClient 48:81866008bba4 143 #ifdef WINCE
AzureIoTClient 48:81866008bba4 144 void xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(int errorMessageID)
AzureIoTClient 48:81866008bba4 145 {
AzureIoTClient 48:81866008bba4 146 (void)errorMessageID;
AzureIoTClient 48:81866008bba4 147 }
AzureIoTClient 48:81866008bba4 148 #else // WINCE
AzureIoTClient 48:81866008bba4 149 void xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(int errorMessageID)
AzureIoTClient 48:81866008bba4 150 {
AzureIoTClient 48:81866008bba4 151 char messageBuffer[MESSAGE_BUFFER_SIZE];
AzureIoTClient 48:81866008bba4 152 if (errorMessageID == 0)
AzureIoTClient 48:81866008bba4 153 {
AzureIoTClient 48:81866008bba4 154 LogError("GetLastError() returned 0. Make sure you are calling this right after the code that failed. ");
AzureIoTClient 48:81866008bba4 155 }
AzureIoTClient 48:81866008bba4 156 else
AzureIoTClient 48:81866008bba4 157 {
AzureIoTClient 48:81866008bba4 158 int size = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
AzureIoTClient 48:81866008bba4 159 GetModuleHandle("WinHttp"), errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), messageBuffer, MESSAGE_BUFFER_SIZE, NULL);
AzureIoTClient 48:81866008bba4 160 if (size == 0)
AzureIoTClient 48:81866008bba4 161 {
AzureIoTClient 48:81866008bba4 162 size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), messageBuffer, MESSAGE_BUFFER_SIZE, NULL);
AzureIoTClient 48:81866008bba4 163 if (size == 0)
AzureIoTClient 48:81866008bba4 164 {
AzureIoTClient 48:81866008bba4 165 LogError("GetLastError Code: %d. ", errorMessageID);
AzureIoTClient 48:81866008bba4 166 }
AzureIoTClient 48:81866008bba4 167 else
AzureIoTClient 48:81866008bba4 168 {
AzureIoTClient 48:81866008bba4 169 LogError("GetLastError: %s.", messageBuffer);
AzureIoTClient 48:81866008bba4 170 }
AzureIoTClient 48:81866008bba4 171 }
AzureIoTClient 48:81866008bba4 172 else
AzureIoTClient 48:81866008bba4 173 {
AzureIoTClient 48:81866008bba4 174 LogError("GetLastError: %s.", messageBuffer);
AzureIoTClient 48:81866008bba4 175 }
AzureIoTClient 48:81866008bba4 176 }
AzureIoTClient 48:81866008bba4 177 }
AzureIoTClient 48:81866008bba4 178 #endif // WINCE
AzureIoTClient 48:81866008bba4 179
AzureIoTClient 48:81866008bba4 180 #endif // WIN32
AzureIoTClient 48:81866008bba4 181
AzureIoTClient 48:81866008bba4 182
AzureIoTClient 48:81866008bba4 183 #endif // NO_LOGGING
AzureIoTClient 48:81866008bba4 184
AzureIoTClient 48:81866008bba4 185
AzureIoTClient 48:81866008bba4 186