1
6
7
8
9
10
11
12
13
14
15
21
22
26
27
28
31
32
33
34
35
36
37
38
39
40
48
49
57
58
66
67
68
69
72
73
74
75
76
77
82
83
84
85
86
87
88
89
90
91
101
102
109
110
111
112
113
114
115
116
117
129
130
131
132
135
136
137
/* ... */
#include <string.h>
#include <stdbool.h>
#include "esp_log.h"
#include "esp_private/log_util.h"
#ifndef CONFIG_IDF_TARGET_LINUX
#include "esp_memory_utils.h"
#else
static inline bool esp_ptr_byte_accessible(const void* ptr)
{
(void) ptr;
return true;
}{...}
/* ... */#endif
/* ... */
#define BYTES_PER_LINE 16
/* ... */
#define IS_CHAR_PRINTABLE(character) ((character) >= 32 && (character) <= 126)
typedef void (*print_line_t)(uintptr_t, const void *, char *, int);
static void log_buffer_hex_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len);
static void log_buffer_char_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len);
static void log_buffer_hexdump_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len);
static void print_buffer(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level, char *output_str, print_line_t print_line_func);
void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
{
char output_str[3 * BYTES_PER_LINE];
print_buffer(tag, buffer, buff_len, log_level, output_str, log_buffer_hex_line);
}{ ... }
void esp_log_buffer_char_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
{
char output_str[BYTES_PER_LINE + 1];
print_buffer(tag, buffer, buff_len, log_level, output_str, log_buffer_char_line);
}{ ... }
void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level)
{
char output_str[(2 + sizeof(void *) * 2) + 3 + (BYTES_PER_LINE * 3) + 2 + (1 + BYTES_PER_LINE + 1) + 1];
print_buffer(tag, buffer, buff_len, log_level, output_str, log_buffer_hexdump_line);
}{ ... }
static void print_buffer(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level, char *output_str, print_line_t print_line_func)
{
if (buff_len == 0) {
return;
}{...}
char temp_buffer[BYTES_PER_LINE + 3];
do {
const char *ptr_line = buffer;
int bytes_cur_line = (buff_len > BYTES_PER_LINE) ? BYTES_PER_LINE : buff_len;
if (!esp_ptr_byte_accessible(buffer)) {
memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4);
ptr_line = temp_buffer;
}{...}
print_line_func((uintptr_t)buffer, ptr_line, output_str, bytes_cur_line);
ESP_LOG_LEVEL(log_level, tag, "%s", output_str);
buffer += bytes_cur_line;
buff_len -= bytes_cur_line;
}{...} while (buff_len);
}{ ... }
static void log_buffer_hex_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len)
{
(void) orig_buff;
const unsigned char *ptr = (unsigned char *)ptr_line;
for (int i = 0; i < buff_len; i++) {
output_str += esp_log_util_cvt_hex(ptr[i], 2, output_str);
*output_str++ = ' ';
}{...}
*--output_str = 0;
}{ ... }
static void log_buffer_char_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len)
{
(void) orig_buff;
const char *ptr = (char *)ptr_line;
memcpy(output_str, ptr, buff_len);
output_str[buff_len] = 0;
}{ ... }
static void log_buffer_hexdump_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len)
{
const unsigned char *ptr = (unsigned char *)ptr_line;
*output_str++ = '0';
*output_str++ = 'x';
output_str += esp_log_util_cvt_hex(orig_buff, sizeof(uintptr_t), output_str);
*output_str++ = ' ';
for (int i = 0; i < BYTES_PER_LINE; i++) {
if ((i & 7) == 0) {
*output_str++ = ' ';
}{...}
*output_str++ = ' ';
if (i < buff_len) {
output_str += esp_log_util_cvt_hex(ptr[i], 2, output_str);
}{...} else {
*output_str++ = ' ';
*output_str++ = ' ';
}{...}
}{...}
*output_str++ = ' ';
*output_str++ = ' ';
*output_str++ = '|';
for (int i = 0; i < buff_len; i++) {
*output_str++ = IS_CHAR_PRINTABLE(ptr[i]) ? ptr[i] : '.';
}{...}
*output_str++ = '|';
*output_str = 0;
}{ ... }