Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
xlogging.c@16:18e7ebd42bb2, 2016-12-14 (annotated)
- Committer:
- Azure.IoT.Build
- Date:
- Wed Dec 14 16:00:59 2016 -0800
- Revision:
- 16:18e7ebd42bb2
- Parent:
- 8:3db46d1e5471
- Child:
- 18:6d8a413a4d9a
1.1.2
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 | 8:3db46d1e5471 | 13 | void consolelogger_log(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...) |
AzureIoTClient | 7:1af47e3a19b6 | 14 | { |
AzureIoTClient | 7:1af47e3a19b6 | 15 | va_list args; |
AzureIoTClient | 7:1af47e3a19b6 | 16 | va_start(args, format); |
AzureIoTClient | 7:1af47e3a19b6 | 17 | |
AzureIoTClient | 7:1af47e3a19b6 | 18 | time_t t = time(NULL); |
AzureIoTClient | 7:1af47e3a19b6 | 19 | |
AzureIoTClient | 7:1af47e3a19b6 | 20 | switch (log_category) |
AzureIoTClient | 7:1af47e3a19b6 | 21 | { |
AzureIoTClient | 7:1af47e3a19b6 | 22 | case LOG_INFO: |
AzureIoTClient | 7:1af47e3a19b6 | 23 | (void)printf("Info: "); |
AzureIoTClient | 7:1af47e3a19b6 | 24 | break; |
AzureIoTClient | 7:1af47e3a19b6 | 25 | case LOG_ERROR: |
AzureIoTClient | 8:3db46d1e5471 | 26 | (void)printf("Error: Time:%.24s File:%s Func:%s Line:%d ", ctime(&t), file, func, line); |
AzureIoTClient | 7:1af47e3a19b6 | 27 | break; |
AzureIoTClient | 7:1af47e3a19b6 | 28 | default: |
AzureIoTClient | 7:1af47e3a19b6 | 29 | break; |
AzureIoTClient | 7:1af47e3a19b6 | 30 | } |
AzureIoTClient | 7:1af47e3a19b6 | 31 | |
AzureIoTClient | 7:1af47e3a19b6 | 32 | (void)vprintf(format, args); |
AzureIoTClient | 7:1af47e3a19b6 | 33 | va_end(args); |
AzureIoTClient | 7:1af47e3a19b6 | 34 | |
AzureIoTClient | 7:1af47e3a19b6 | 35 | (void)log_category; |
AzureIoTClient | 7:1af47e3a19b6 | 36 | if (options & LOG_LINE) |
AzureIoTClient | 7:1af47e3a19b6 | 37 | { |
AzureIoTClient | 7:1af47e3a19b6 | 38 | (void)printf("\r\n"); |
AzureIoTClient | 7:1af47e3a19b6 | 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 | |
Azure.IoT Build | 6:c55b013dfc2a | 46 | void xlogging_set_log_function(LOGGER_LOG log_function) |
Azure.IoT Build | 6:c55b013dfc2a | 47 | { |
Azure.IoT Build | 6:c55b013dfc2a | 48 | global_log_function = log_function; |
Azure.IoT Build | 6:c55b013dfc2a | 49 | } |
Azure.IoT Build | 6:c55b013dfc2a | 50 | |
Azure.IoT Build | 6:c55b013dfc2a | 51 | LOGGER_LOG xlogging_get_log_function(void) |
Azure.IoT Build | 6:c55b013dfc2a | 52 | { |
Azure.IoT Build | 6:c55b013dfc2a | 53 | return global_log_function; |
Azure.IoT Build | 6:c55b013dfc2a | 54 | } |
AzureIoTClient | 7:1af47e3a19b6 | 55 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 56 | /* Print up to 16 bytes per line. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 57 | #define LINE_SIZE 16 |
Azure.IoT.Build | 16:18e7ebd42bb2 | 58 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 59 | /* Return the printable char for the provided value. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 60 | #define PRINTABLE(c) ((c >= ' ') && (c <= '~')) ? (char)c : '.' |
Azure.IoT.Build | 16:18e7ebd42bb2 | 61 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 62 | /* Convert the lower nibble of the provided byte to a hexadecimal printable char. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 63 | #define HEX_STR(c) (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A') |
Azure.IoT.Build | 16:18e7ebd42bb2 | 64 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 65 | void xlogging_dump_buffer(const void* buf, size_t size) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 66 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 67 | char charBuf[LINE_SIZE + 1]; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 68 | char hexBuf[LINE_SIZE * 3 + 1]; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 69 | char countbuf = 0; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 70 | const unsigned char* bufAsChar = (const unsigned char*)buf; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 71 | const unsigned char* startPos = bufAsChar; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 72 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 73 | /* Print the whole buffer. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 74 | for (size_t i = 0; i < size; i++) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 75 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 76 | /* Store the printable value of the char in the charBuf to print. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 77 | charBuf[countbuf] = PRINTABLE(*bufAsChar); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 78 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 79 | /* Convert the high nibble to a printable hexadecimal value. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 80 | hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 81 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 82 | /* Convert the low nibble to a printable hexadecimal value. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 83 | hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 84 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 85 | hexBuf[countbuf * 3 + 2] = ' '; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 86 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 87 | countbuf++; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 88 | bufAsChar++; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 89 | /* If the line is full, print it to start another one. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 90 | if (countbuf == LINE_SIZE) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 91 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 92 | charBuf[countbuf] = '\0'; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 93 | hexBuf[countbuf * 3] = '\0'; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 94 | LOG(LOG_TRACE, 0, "%p: %s %s", startPos, hexBuf, charBuf); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 95 | countbuf = 0; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 96 | startPos = bufAsChar; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 97 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 98 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 99 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 100 | /* If the last line does not fit the line size. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 101 | if (countbuf > 0) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 102 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 103 | /* Close the charBuf string. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 104 | charBuf[countbuf] = '\0'; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 105 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 106 | /* Fill the hexBuf with spaces to keep the charBuf alignment. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 107 | while ((countbuf++) < LINE_SIZE - 1) |
Azure.IoT.Build | 16:18e7ebd42bb2 | 108 | { |
Azure.IoT.Build | 16:18e7ebd42bb2 | 109 | hexBuf[countbuf * 3] = ' '; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 110 | hexBuf[countbuf * 3 + 1] = ' '; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 111 | hexBuf[countbuf * 3 + 2] = ' '; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 112 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 113 | hexBuf[countbuf * 3] = '\0'; |
Azure.IoT.Build | 16:18e7ebd42bb2 | 114 | |
Azure.IoT.Build | 16:18e7ebd42bb2 | 115 | /* Print the last line. */ |
Azure.IoT.Build | 16:18e7ebd42bb2 | 116 | LOG(LOG_TRACE, 0, "%p: %s %s", startPos, hexBuf, charBuf); |
Azure.IoT.Build | 16:18e7ebd42bb2 | 117 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 118 | } |
Azure.IoT.Build | 16:18e7ebd42bb2 | 119 | |
AzureIoTClient | 7:1af47e3a19b6 | 120 | #endif |