-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_buffers.c
137 lines (124 loc) · 4.21 KB
/
log_buffers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ctype.h>
#include <string.h>
#include "esp_log.h"
#include "esp_memory_utils.h"
//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 ++) {
sprintf(hex_buffer + 3 * i, "%02x ", ptr_line[i]);
}
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]+"|"
char hd_buffer[10 + 3 + BYTES_PER_LINE * 3 + 3 + BYTES_PER_LINE + 1 + 1];
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) {
ptr_hd += sprintf(ptr_hd, " %02x", ptr_line[i]);
} 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);
}