Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
xlogging.c@45:1119d0f2c4d8, 2018-06-11 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Mon Jun 11 15:41:10 2018 -0700
- Revision:
- 45:1119d0f2c4d8
- Parent:
- 41:dc93369d5ed4
- Child:
- 48:81866008bba4
1.2.5
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:1af47e3a19b6 | 9 | |
AzureIoTClient | 7:1af47e3a19b6 | 10 | #ifdef WINCE |
AzureIoTClient | 7:1af47e3a19b6 | 11 | #include <stdarg.h> |
AzureIoTClient | 7:1af47e3a19b6 | 12 | |
AzureIoTClient | 27:8656a313842b | 13 | void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, int line, unsigned int options, const char* format, ...) |
AzureIoTClient | 7:1af47e3a19b6 | 14 | { |
AzureIoTClient | 45:1119d0f2c4d8 | 15 | va_list args; |
AzureIoTClient | 45:1119d0f2c4d8 | 16 | va_start(args, format); |
AzureIoTClient | 7:1af47e3a19b6 | 17 | |
AzureIoTClient | 45:1119d0f2c4d8 | 18 | time_t t = time(NULL); |
AzureIoTClient | 7:1af47e3a19b6 | 19 | |
AzureIoTClient | 45:1119d0f2c4d8 | 20 | switch (log_category) |
AzureIoTClient | 45:1119d0f2c4d8 | 21 | { |
AzureIoTClient | 45:1119d0f2c4d8 | 22 | case AZ_LOG_INFO: |
AzureIoTClient | 45:1119d0f2c4d8 | 23 | (void)printf("Info: "); |
AzureIoTClient | 45:1119d0f2c4d8 | 24 | break; |
AzureIoTClient | 45:1119d0f2c4d8 | 25 | case AZ_LOG_ERROR: |
AzureIoTClient | 45:1119d0f2c4d8 | 26 | (void)printf("Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line); |
AzureIoTClient | 45:1119d0f2c4d8 | 27 | break; |
AzureIoTClient | 45:1119d0f2c4d8 | 28 | default: |
AzureIoTClient | 45:1119d0f2c4d8 | 29 | break; |
AzureIoTClient | 45:1119d0f2c4d8 | 30 | } |
AzureIoTClient | 7:1af47e3a19b6 | 31 | |
AzureIoTClient | 45:1119d0f2c4d8 | 32 | (void)vprintf(format, args); |
AzureIoTClient | 45:1119d0f2c4d8 | 33 | va_end(args); |
AzureIoTClient | 7:1af47e3a19b6 | 34 | |
AzureIoTClient | 45:1119d0f2c4d8 | 35 | (void)log_category; |
AzureIoTClient | 45:1119d0f2c4d8 | 36 | if (options & LOG_LINE) |
AzureIoTClient | 45:1119d0f2c4d8 | 37 | { |
AzureIoTClient | 45:1119d0f2c4d8 | 38 | (void)printf("\r\n"); |
AzureIoTClient | 45:1119d0f2c4d8 | 39 | } |
AzureIoTClient | 7:1af47e3a19b6 | 40 | } |
AzureIoTClient | 7:1af47e3a19b6 | 41 | #endif |
AzureIoTClient | 7:1af47e3a19b6 | 42 | |
AzureIoTClient | 7:1af47e3a19b6 | 43 | LOGGER_LOG global_log_function = consolelogger_log; |
AzureIoTClient | 7:1af47e3a19b6 | 44 | |
Azure.IoT Build | 6:c55b013dfc2a | 45 | void xlogging_set_log_function(LOGGER_LOG log_function) |
Azure.IoT Build | 6:c55b013dfc2a | 46 | { |
Azure.IoT Build | 6:c55b013dfc2a | 47 | global_log_function = log_function; |
Azure.IoT Build | 6:c55b013dfc2a | 48 | } |
Azure.IoT Build | 6:c55b013dfc2a | 49 | |
Azure.IoT Build | 6:c55b013dfc2a | 50 | LOGGER_LOG xlogging_get_log_function(void) |
Azure.IoT Build | 6:c55b013dfc2a | 51 | { |
Azure.IoT Build | 6:c55b013dfc2a | 52 | return global_log_function; |
Azure.IoT Build | 6:c55b013dfc2a | 53 | } |
AzureIoTClient | 7:1af47e3a19b6 | 54 | |
AzureIoTClient | 27:8656a313842b | 55 | #if (defined(_MSC_VER)) && (!(defined WINCE)) |
AzureIoTClient | 27:8656a313842b | 56 | |
AzureIoTClient | 27:8656a313842b | 57 | LOGGER_LOG_GETLASTERROR global_log_function_GetLastError = consolelogger_log_with_GetLastError; |
AzureIoTClient | 27:8656a313842b | 58 | |
AzureIoTClient | 27:8656a313842b | 59 | void xlogging_set_log_function_GetLastError(LOGGER_LOG_GETLASTERROR log_function_GetLastError) |
AzureIoTClient | 27:8656a313842b | 60 | { |
AzureIoTClient | 27:8656a313842b | 61 | global_log_function_GetLastError = log_function_GetLastError; |
AzureIoTClient | 27:8656a313842b | 62 | } |
AzureIoTClient | 27:8656a313842b | 63 | |
AzureIoTClient | 27:8656a313842b | 64 | LOGGER_LOG_GETLASTERROR xlogging_get_log_function_GetLastError(void) |
AzureIoTClient | 27:8656a313842b | 65 | { |
AzureIoTClient | 27:8656a313842b | 66 | return global_log_function_GetLastError; |
AzureIoTClient | 27:8656a313842b | 67 | } |
AzureIoTClient | 27:8656a313842b | 68 | #endif |
AzureIoTClient | 27:8656a313842b | 69 | |
AzureIoTClient | 41:dc93369d5ed4 | 70 | /* Print up to 16 bytes per line. */ |
AzureIoTClient | 41:dc93369d5ed4 | 71 | #define LINE_SIZE 16 |
AzureIoTClient | 41:dc93369d5ed4 | 72 | |
AzureIoTClient | 41:dc93369d5ed4 | 73 | /* Return the printable char for the provided value. */ |
AzureIoTClient | 41:dc93369d5ed4 | 74 | #define PRINTABLE(c) ((c >= ' ') && (c <= '~')) ? (char)c : '.' |
AzureIoTClient | 41:dc93369d5ed4 | 75 | |
AzureIoTClient | 41:dc93369d5ed4 | 76 | /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */ |
AzureIoTClient | 41:dc93369d5ed4 | 77 | #define HEX_STR(c) (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A') |
AzureIoTClient | 41:dc93369d5ed4 | 78 | |
AzureIoTClient | 41:dc93369d5ed4 | 79 | void LogBinary(const char* comment, const void* data, size_t size) |
AzureIoTClient | 41:dc93369d5ed4 | 80 | { |
AzureIoTClient | 41:dc93369d5ed4 | 81 | char charBuf[LINE_SIZE + 1]; |
AzureIoTClient | 41:dc93369d5ed4 | 82 | char hexBuf[LINE_SIZE * 3 + 1]; |
AzureIoTClient | 41:dc93369d5ed4 | 83 | size_t countbuf = 0; |
AzureIoTClient | 41:dc93369d5ed4 | 84 | size_t i = 0; |
AzureIoTClient | 41:dc93369d5ed4 | 85 | const unsigned char* bufAsChar = (const unsigned char*)data; |
AzureIoTClient | 41:dc93369d5ed4 | 86 | const unsigned char* startPos = bufAsChar; |
AzureIoTClient | 41:dc93369d5ed4 | 87 | |
AzureIoTClient | 41:dc93369d5ed4 | 88 | LOG(AZ_LOG_TRACE, LOG_LINE, "%s %zu bytes", comment, size); |
AzureIoTClient | 41:dc93369d5ed4 | 89 | |
AzureIoTClient | 41:dc93369d5ed4 | 90 | /* Print the whole buffer. */ |
AzureIoTClient | 41:dc93369d5ed4 | 91 | for (i = 0; i < size; i++) |
AzureIoTClient | 41:dc93369d5ed4 | 92 | { |
AzureIoTClient | 41:dc93369d5ed4 | 93 | /* Store the printable value of the char in the charBuf to print. */ |
AzureIoTClient | 41:dc93369d5ed4 | 94 | charBuf[countbuf] = PRINTABLE(*bufAsChar); |
AzureIoTClient | 41:dc93369d5ed4 | 95 | |
AzureIoTClient | 41:dc93369d5ed4 | 96 | /* Convert the high nibble to a printable hexadecimal value. */ |
AzureIoTClient | 41:dc93369d5ed4 | 97 | hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4); |
AzureIoTClient | 41:dc93369d5ed4 | 98 | |
AzureIoTClient | 41:dc93369d5ed4 | 99 | /* Convert the low nibble to a printable hexadecimal value. */ |
AzureIoTClient | 41:dc93369d5ed4 | 100 | hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar); |
AzureIoTClient | 41:dc93369d5ed4 | 101 | |
AzureIoTClient | 41:dc93369d5ed4 | 102 | hexBuf[countbuf * 3 + 2] = ' '; |
AzureIoTClient | 41:dc93369d5ed4 | 103 | |
AzureIoTClient | 41:dc93369d5ed4 | 104 | countbuf++; |
AzureIoTClient | 41:dc93369d5ed4 | 105 | bufAsChar++; |
AzureIoTClient | 41:dc93369d5ed4 | 106 | /* If the line is full, print it to start another one. */ |
AzureIoTClient | 41:dc93369d5ed4 | 107 | if (countbuf == LINE_SIZE) |
AzureIoTClient | 41:dc93369d5ed4 | 108 | { |
AzureIoTClient | 41:dc93369d5ed4 | 109 | charBuf[countbuf] = '\0'; |
AzureIoTClient | 41:dc93369d5ed4 | 110 | hexBuf[countbuf * 3] = '\0'; |
AzureIoTClient | 41:dc93369d5ed4 | 111 | LOG(AZ_LOG_TRACE, LOG_LINE, "%p: %s %s", startPos, hexBuf, charBuf); |
AzureIoTClient | 41:dc93369d5ed4 | 112 | countbuf = 0; |
AzureIoTClient | 41:dc93369d5ed4 | 113 | startPos = bufAsChar; |
AzureIoTClient | 41:dc93369d5ed4 | 114 | } |
AzureIoTClient | 41:dc93369d5ed4 | 115 | } |
AzureIoTClient | 41:dc93369d5ed4 | 116 | |
AzureIoTClient | 41:dc93369d5ed4 | 117 | /* If the last line does not fit the line size. */ |
AzureIoTClient | 41:dc93369d5ed4 | 118 | if (countbuf > 0) |
AzureIoTClient | 41:dc93369d5ed4 | 119 | { |
AzureIoTClient | 41:dc93369d5ed4 | 120 | /* Close the charBuf string. */ |
AzureIoTClient | 41:dc93369d5ed4 | 121 | charBuf[countbuf] = '\0'; |
AzureIoTClient | 41:dc93369d5ed4 | 122 | |
AzureIoTClient | 41:dc93369d5ed4 | 123 | /* Fill the hexBuf with spaces to keep the charBuf alignment. */ |
AzureIoTClient | 41:dc93369d5ed4 | 124 | while ((countbuf++) < LINE_SIZE - 1) |
AzureIoTClient | 41:dc93369d5ed4 | 125 | { |
AzureIoTClient | 41:dc93369d5ed4 | 126 | hexBuf[countbuf * 3] = ' '; |
AzureIoTClient | 41:dc93369d5ed4 | 127 | hexBuf[countbuf * 3 + 1] = ' '; |
AzureIoTClient | 41:dc93369d5ed4 | 128 | hexBuf[countbuf * 3 + 2] = ' '; |
AzureIoTClient | 41:dc93369d5ed4 | 129 | } |
AzureIoTClient | 41:dc93369d5ed4 | 130 | hexBuf[countbuf * 3] = '\0'; |
AzureIoTClient | 41:dc93369d5ed4 | 131 | |
AzureIoTClient | 41:dc93369d5ed4 | 132 | /* Print the last line. */ |
AzureIoTClient | 41:dc93369d5ed4 | 133 | LOG(AZ_LOG_TRACE, LOG_LINE, "%p: %s %s", startPos, hexBuf, charBuf); |
AzureIoTClient | 41:dc93369d5ed4 | 134 | } |
AzureIoTClient | 41:dc93369d5ed4 | 135 | } |
AzureIoTClient | 41:dc93369d5ed4 | 136 | |
AzureIoTClient | 7:1af47e3a19b6 | 137 | #endif |