2021-04-12 02:02:59 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-11-21 12:40:59 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2022-04-10 13:52:06 -04:00
|
|
|
#include <stdbool.h>
|
2019-11-21 12:40:59 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2022-04-10 13:52:06 -04:00
|
|
|
#ifndef CONFIG_IDF_TARGET_LINUX
|
|
|
|
#include "esp_memory_utils.h" // for esp_ptr_byte_accessible
|
|
|
|
#else
|
2024-02-04 01:50:54 -05:00
|
|
|
static inline bool esp_ptr_byte_accessible(const void* ptr)
|
|
|
|
{
|
2022-04-10 13:52:06 -04:00
|
|
|
(void) ptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif // CONFIG_IDF_TARGET_LINUX
|
2019-11-21 12:40:59 -05:00
|
|
|
|
|
|
|
//print number of bytes per line for esp_log_buffer_char and esp_log_buffer_hex
|
|
|
|
#define BYTES_PER_LINE 16
|
|
|
|
|
|
|
|
void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t buff_len,
|
|
|
|
esp_log_level_t log_level)
|
|
|
|
{
|
|
|
|
if (buff_len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
|
|
|
|
char hex_buffer[3 * BYTES_PER_LINE + 1];
|
|
|
|
const char *ptr_line;
|
|
|
|
int bytes_cur_line;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (buff_len > BYTES_PER_LINE) {
|
|
|
|
bytes_cur_line = BYTES_PER_LINE;
|
|
|
|
} else {
|
|
|
|
bytes_cur_line = buff_len;
|
|
|
|
}
|
|
|
|
if (!esp_ptr_byte_accessible(buffer)) {
|
|
|
|
//use memcpy to get around alignment issue
|
|
|
|
memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
|
|
|
|
ptr_line = temp_buffer;
|
|
|
|
} else {
|
|
|
|
ptr_line = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes_cur_line; i ++) {
|
2024-03-11 05:08:03 -04:00
|
|
|
sprintf(hex_buffer + 3 * i, "%02x ", (unsigned char) ptr_line[i]);
|
2019-11-21 12:40:59 -05:00
|
|
|
}
|
|
|
|
ESP_LOG_LEVEL(log_level, tag, "%s", hex_buffer);
|
|
|
|
buffer += bytes_cur_line;
|
|
|
|
buff_len -= bytes_cur_line;
|
|
|
|
} while (buff_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_log_buffer_char_internal(const char *tag, const void *buffer, uint16_t buff_len,
|
|
|
|
esp_log_level_t log_level)
|
|
|
|
{
|
|
|
|
if (buff_len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
|
|
|
|
char char_buffer[BYTES_PER_LINE + 1];
|
|
|
|
const char *ptr_line;
|
|
|
|
int bytes_cur_line;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (buff_len > BYTES_PER_LINE) {
|
|
|
|
bytes_cur_line = BYTES_PER_LINE;
|
|
|
|
} else {
|
|
|
|
bytes_cur_line = buff_len;
|
|
|
|
}
|
|
|
|
if (!esp_ptr_byte_accessible(buffer)) {
|
|
|
|
//use memcpy to get around alignment issue
|
|
|
|
memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
|
|
|
|
ptr_line = temp_buffer;
|
|
|
|
} else {
|
|
|
|
ptr_line = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes_cur_line; i ++) {
|
|
|
|
sprintf(char_buffer + i, "%c", ptr_line[i]);
|
|
|
|
}
|
|
|
|
ESP_LOG_LEVEL(log_level, tag, "%s", char_buffer);
|
|
|
|
buffer += bytes_cur_line;
|
|
|
|
buff_len -= bytes_cur_line;
|
|
|
|
} while (buff_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (buff_len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char temp_buffer[BYTES_PER_LINE + 3]; //for not-byte-accessible memory
|
|
|
|
const char *ptr_line;
|
|
|
|
//format: field[length]
|
|
|
|
// ADDR[10]+" "+DATA_HEX[8*3]+" "+DATA_HEX[8*3]+" |"+DATA_CHAR[8]+"|"
|
2024-03-11 05:08:03 -04:00
|
|
|
char hd_buffer[2 + sizeof(void *) * 2 + 3 + BYTES_PER_LINE * 3 + 1 + 3 + BYTES_PER_LINE + 1 + 1];
|
2019-11-21 12:40:59 -05:00
|
|
|
char *ptr_hd;
|
|
|
|
int bytes_cur_line;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (buff_len > BYTES_PER_LINE) {
|
|
|
|
bytes_cur_line = BYTES_PER_LINE;
|
|
|
|
} else {
|
|
|
|
bytes_cur_line = buff_len;
|
|
|
|
}
|
|
|
|
if (!esp_ptr_byte_accessible(buffer)) {
|
|
|
|
//use memcpy to get around alignment issue
|
|
|
|
memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
|
|
|
|
ptr_line = temp_buffer;
|
|
|
|
} else {
|
|
|
|
ptr_line = buffer;
|
|
|
|
}
|
|
|
|
ptr_hd = hd_buffer;
|
|
|
|
|
|
|
|
ptr_hd += sprintf(ptr_hd, "%p ", buffer);
|
|
|
|
for (int i = 0; i < BYTES_PER_LINE; i ++) {
|
|
|
|
if ((i & 7) == 0) {
|
|
|
|
ptr_hd += sprintf(ptr_hd, " ");
|
|
|
|
}
|
|
|
|
if (i < bytes_cur_line) {
|
2024-03-11 05:08:03 -04:00
|
|
|
ptr_hd += sprintf(ptr_hd, " %02x", (unsigned char) ptr_line[i]);
|
2019-11-21 12:40:59 -05:00
|
|
|
} else {
|
|
|
|
ptr_hd += sprintf(ptr_hd, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr_hd += sprintf(ptr_hd, " |");
|
|
|
|
for (int i = 0; i < bytes_cur_line; i ++) {
|
|
|
|
if (isprint((int)ptr_line[i])) {
|
|
|
|
ptr_hd += sprintf(ptr_hd, "%c", ptr_line[i]);
|
|
|
|
} else {
|
|
|
|
ptr_hd += sprintf(ptr_hd, ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr_hd += sprintf(ptr_hd, "|");
|
|
|
|
|
|
|
|
ESP_LOG_LEVEL(log_level, tag, "%s", hd_buffer);
|
|
|
|
buffer += bytes_cur_line;
|
|
|
|
buff_len -= bytes_cur_line;
|
|
|
|
} while (buff_len);
|
|
|
|
}
|