Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
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 #ifdef WIN32 00010 #include "windows.h" 00011 #endif // WIN32 00012 00013 00014 #ifdef WINCE 00015 #include <stdarg.h> 00016 00017 void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...) 00018 { 00019 va_list args; 00020 va_start(args, format); 00021 00022 time_t t = time(NULL); 00023 00024 switch (log_category) 00025 { 00026 case AZ_LOG_INFO: 00027 (void)printf("Info: "); 00028 break; 00029 case AZ_LOG_ERROR: 00030 (void)printf("Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line); 00031 break; 00032 default: 00033 break; 00034 } 00035 00036 (void)vprintf(format, args); 00037 va_end(args); 00038 00039 (void)log_category; 00040 if (options & LOG_LINE) 00041 { 00042 (void)printf("\r\n"); 00043 } 00044 } 00045 #endif // WINCE 00046 00047 LOGGER_LOG global_log_function = consolelogger_log; 00048 00049 void xlogging_set_log_function(LOGGER_LOG log_function) 00050 { 00051 global_log_function = log_function; 00052 } 00053 00054 LOGGER_LOG xlogging_get_log_function(void) 00055 { 00056 return global_log_function; 00057 } 00058 00059 #if (defined(_MSC_VER)) && (!(defined WINCE)) 00060 00061 LOGGER_LOG_GETLASTERROR global_log_function_GetLastError = consolelogger_log_with_GetLastError; 00062 00063 void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function_GetLastError) 00064 { 00065 global_log_function_GetLastError = log_function_GetLastError; 00066 } 00067 00068 LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void) 00069 { 00070 return global_log_function_GetLastError; 00071 } 00072 #endif 00073 00074 /* Print up to 16 bytes per line. */ 00075 #define LINE_SIZE 16 00076 00077 /* Return the printable char for the provided value. */ 00078 #define PRINTABLE(c) ((c >= ' ') && (c <= '~')) ? (char)c : '.' 00079 00080 /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */ 00081 #define HEX_STR(c) (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A') 00082 00083 void LogBinary(const char* comment, const void* data, size_t size) 00084 { 00085 char charBuf[LINE_SIZE + 1]; 00086 char hexBuf[LINE_SIZE * 3 + 1]; 00087 size_t countbuf = 0; 00088 size_t i = 0; 00089 const unsigned char* bufAsChar = (const unsigned char*)data; 00090 const unsigned char* startPos = bufAsChar; 00091 00092 LOG(AZ_LOG_TRACE, LOG_LINE, "%s %zu bytes", comment, size); 00093 00094 /* Print the whole buffer. */ 00095 for (i = 0; i < size; i++) 00096 { 00097 /* Store the printable value of the char in the charBuf to print. */ 00098 charBuf[countbuf] = PRINTABLE(*bufAsChar); 00099 00100 /* Convert the high nibble to a printable hexadecimal value. */ 00101 hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4); 00102 00103 /* Convert the low nibble to a printable hexadecimal value. */ 00104 hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar); 00105 00106 hexBuf[countbuf * 3 + 2] = ' '; 00107 00108 countbuf++; 00109 bufAsChar++; 00110 /* If the line is full, print it to start another one. */ 00111 if (countbuf == LINE_SIZE) 00112 { 00113 charBuf[countbuf] = '\0'; 00114 hexBuf[countbuf * 3] = '\0'; 00115 LOG(AZ_LOG_TRACE, LOG_LINE, "%p: %s %s", startPos, hexBuf, charBuf); 00116 countbuf = 0; 00117 startPos = bufAsChar; 00118 } 00119 } 00120 00121 /* If the last line does not fit the line size. */ 00122 if (countbuf > 0) 00123 { 00124 /* Close the charBuf string. */ 00125 charBuf[countbuf] = '\0'; 00126 00127 /* Fill the hexBuf with spaces to keep the charBuf alignment. */ 00128 while ((countbuf++) < LINE_SIZE - 1) 00129 { 00130 hexBuf[countbuf * 3] = ' '; 00131 hexBuf[countbuf * 3 + 1] = ' '; 00132 hexBuf[countbuf * 3 + 2] = ' '; 00133 } 00134 hexBuf[countbuf * 3] = '\0'; 00135 00136 /* Print the last line. */ 00137 LOG(AZ_LOG_TRACE, LOG_LINE, "%p: %s %s", startPos, hexBuf, charBuf); 00138 } 00139 } 00140 00141 #ifdef WIN32 00142 00143 #ifdef WINCE 00144 void xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(int errorMessageID) 00145 { 00146 (void)errorMessageID; 00147 } 00148 #else // WINCE 00149 void xlogging_LogErrorWinHTTPWithGetLastErrorAsStringFormatter(int errorMessageID) 00150 { 00151 char messageBuffer[MESSAGE_BUFFER_SIZE]; 00152 if (errorMessageID == 0) 00153 { 00154 LogError("GetLastError() returned 0. Make sure you are calling this right after the code that failed. "); 00155 } 00156 else 00157 { 00158 int size = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 00159 GetModuleHandle("WinHttp"), errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), messageBuffer, MESSAGE_BUFFER_SIZE, NULL); 00160 if (size == 0) 00161 { 00162 size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), messageBuffer, MESSAGE_BUFFER_SIZE, NULL); 00163 if (size == 0) 00164 { 00165 LogError("GetLastError Code: %d. ", errorMessageID); 00166 } 00167 else 00168 { 00169 LogError("GetLastError: %s.", messageBuffer); 00170 } 00171 } 00172 else 00173 { 00174 LogError("GetLastError: %s.", messageBuffer); 00175 } 00176 } 00177 } 00178 #endif // WINCE 00179 00180 #endif // WIN32 00181 00182 00183 #endif // NO_LOGGING 00184 00185 00186
Generated on Wed Jul 13 2022 23:38:02 by
1.7.2