Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
Diff: xlogging.c
- Revision:
- 16:18e7ebd42bb2
- Parent:
- 8:3db46d1e5471
- Child:
- 18:6d8a413a4d9a
diff -r 956c6d205aa7 -r 18e7ebd42bb2 xlogging.c --- a/xlogging.c Wed Nov 16 21:38:39 2016 -0800 +++ b/xlogging.c Wed Dec 14 16:00:59 2016 -0800 @@ -53,4 +53,68 @@ return global_log_function; } +/* Print up to 16 bytes per line. */ +#define LINE_SIZE 16 + +/* Return the printable char for the provided value. */ +#define PRINTABLE(c) ((c >= ' ') && (c <= '~')) ? (char)c : '.' + +/* Convert the lower nibble of the provided byte to a hexadecimal printable char. */ +#define HEX_STR(c) (((c) & 0xF) < 0xA) ? (char)(((c) & 0xF) + '0') : (char)(((c) & 0xF) - 0xA + 'A') + +void xlogging_dump_buffer(const void* buf, size_t size) +{ + char charBuf[LINE_SIZE + 1]; + char hexBuf[LINE_SIZE * 3 + 1]; + char countbuf = 0; + const unsigned char* bufAsChar = (const unsigned char*)buf; + const unsigned char* startPos = bufAsChar; + + /* Print the whole buffer. */ + for (size_t i = 0; i < size; i++) + { + /* Store the printable value of the char in the charBuf to print. */ + charBuf[countbuf] = PRINTABLE(*bufAsChar); + + /* Convert the high nibble to a printable hexadecimal value. */ + hexBuf[countbuf * 3] = HEX_STR(*bufAsChar >> 4); + + /* Convert the low nibble to a printable hexadecimal value. */ + hexBuf[countbuf * 3 + 1] = HEX_STR(*bufAsChar); + + hexBuf[countbuf * 3 + 2] = ' '; + + countbuf++; + bufAsChar++; + /* If the line is full, print it to start another one. */ + if (countbuf == LINE_SIZE) + { + charBuf[countbuf] = '\0'; + hexBuf[countbuf * 3] = '\0'; + LOG(LOG_TRACE, 0, "%p: %s %s", startPos, hexBuf, charBuf); + countbuf = 0; + startPos = bufAsChar; + } + } + + /* If the last line does not fit the line size. */ + if (countbuf > 0) + { + /* Close the charBuf string. */ + charBuf[countbuf] = '\0'; + + /* Fill the hexBuf with spaces to keep the charBuf alignment. */ + while ((countbuf++) < LINE_SIZE - 1) + { + hexBuf[countbuf * 3] = ' '; + hexBuf[countbuf * 3 + 1] = ' '; + hexBuf[countbuf * 3 + 2] = ' '; + } + hexBuf[countbuf * 3] = '\0'; + + /* Print the last line. */ + LOG(LOG_TRACE, 0, "%p: %s %s", startPos, hexBuf, charBuf); + } +} + #endif