diff --git a/components/log/host_test/log_test/main/log_test.cpp b/components/log/host_test/log_test/main/log_test.cpp index 40da7d3763..a9c2a12435 100644 --- a/components/log/host_test/log_test/main/log_test.cpp +++ b/components/log/host_test/log_test/main/log_test.cpp @@ -204,6 +204,31 @@ TEST_CASE("log buffer") CHECK(regex_search(fix.get_print_buffer_string(), buffer_regex)); } +TEST_CASE("log bytes > 127") +{ + PrintFixture fix(ESP_LOG_INFO); + const uint8_t buffer[] = { + 0xff, 0x80, + }; + ESP_LOG_BUFFER_HEX(TEST_TAG, buffer, sizeof(buffer)); + const std::regex buffer_regex("I \\([0-9]*\\) test: ff 80", std::regex::ECMAScript); + CHECK(regex_search(fix.get_print_buffer_string(), buffer_regex)); +} + +TEST_CASE("log buffer dump") +{ + PrintFixture fix(ESP_LOG_INFO); + const uint8_t buffer[] = { + 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, 0x08, + 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8 + }; + ESP_LOG_BUFFER_HEXDUMP(TEST_TAG, buffer, sizeof(buffer), ESP_LOG_INFO); + const std::regex buffer_regex("I \\([0-9]*\\) test: 0x[0-9a-f]+\\s+" + "00 00 00 00 05 06 07 08 ff fe fd fc fb fa f9 f8 " + "\\s+|[\\.]{16}|", std::regex::ECMAScript); + CHECK(regex_search(fix.get_print_buffer_string(), buffer_regex)); +} + TEST_CASE("rom printf") { PutcFixture fix; diff --git a/components/log/log_buffers.c b/components/log/log_buffers.c index bdbb1437c6..bef3072455 100644 --- a/components/log/log_buffers.c +++ b/components/log/log_buffers.c @@ -49,7 +49,7 @@ void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t b } for (int i = 0; i < bytes_cur_line; i ++) { - sprintf(hex_buffer + 3 * i, "%02x ", ptr_line[i]); + sprintf(hex_buffer + 3 * i, "%02x ", (unsigned char) ptr_line[i]); } ESP_LOG_LEVEL(log_level, tag, "%s", hex_buffer); buffer += bytes_cur_line; @@ -101,7 +101,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 const char *ptr_line; //format: field[length] // ADDR[10]+" "+DATA_HEX[8*3]+" "+DATA_HEX[8*3]+" |"+DATA_CHAR[8]+"|" - char hd_buffer[10 + 3 + BYTES_PER_LINE * 3 + 3 + BYTES_PER_LINE + 1 + 1]; + char hd_buffer[2 + sizeof(void *) * 2 + 3 + BYTES_PER_LINE * 3 + 1 + 3 + BYTES_PER_LINE + 1 + 1]; char *ptr_hd; int bytes_cur_line; @@ -126,7 +126,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 ptr_hd += sprintf(ptr_hd, " "); } if (i < bytes_cur_line) { - ptr_hd += sprintf(ptr_hd, " %02x", ptr_line[i]); + ptr_hd += sprintf(ptr_hd, " %02x", (unsigned char) ptr_line[i]); } else { ptr_hd += sprintf(ptr_hd, " "); }