Skip to content

Commit 6b5fb91

Browse files
committed
fix(mdns): Make debug module more flexible
and enable routing printfs to esp_log component
1 parent ed87fa2 commit 6b5fb91

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

components/mdns/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ menu "mDNS"
142142
help
143143
Enable for the library to log received and sent mDNS packets to stdout.
144144

145+
config MDNS_DEBUG_USE_ESP_LOG
146+
bool "Route debug prints to ESP_LOG"
147+
depends on MDNS_ENABLE_DEBUG_PRINTS
148+
default y
149+
help
150+
Enable this option to route debug prints to ESP_LOG, which allows more flexibility.
151+
152+
config MDNS_DEBUG_BUFFER_SIZE
153+
int "Size of temporary buffer for debug prints"
154+
depends on MDNS_DEBUG_USE_ESP_LOG
155+
default 1024
156+
help
157+
This buffer is used to output mDNS packets in debug prints.
158+
145159
config MDNS_ENABLE_CONSOLE_CLI
146160
bool "Enable Command Line Interface on device console"
147161
default y

components/mdns/mdns_debug.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,58 @@
66

77
#include <string.h>
88
#include <stdint.h>
9-
#include <stdarg.h>
109
#include <inttypes.h>
11-
#include "freertos/FreeRTOS.h"
1210
#include "sdkconfig.h"
1311
#include "mdns_private.h"
1412
#include "mdns_utils.h"
1513

14+
#ifdef CONFIG_MDNS_DEBUG_USE_ESP_LOG
15+
16+
#include <stdarg.h>
17+
#include "esp_log.h"
18+
19+
#define MDNS_DBG_MAX_LINE CONFIG_MDNS_DEBUG_BUFFER_SIZE
20+
21+
static char s_mdns_dbg_buf[MDNS_DBG_MAX_LINE];
22+
static size_t s_mdns_dbg_pos = 0;
23+
24+
static void mdns_dbg_puts(const char *str)
25+
{
26+
ESP_LOGI("mdns", "%s", str);
27+
}
28+
29+
static inline void mdns_dbg_flush(void)
30+
{
31+
if (s_mdns_dbg_pos > 0) {
32+
s_mdns_dbg_buf[s_mdns_dbg_pos] = '\0';
33+
mdns_dbg_puts(s_mdns_dbg_buf);
34+
s_mdns_dbg_pos = 0;
35+
}
36+
}
37+
38+
static void mdns_dbg_printf(const char *fmt, ...)
39+
{
40+
va_list ap;
41+
va_start(ap, fmt);
42+
int len = vsnprintf(s_mdns_dbg_buf + s_mdns_dbg_pos, MDNS_DBG_MAX_LINE - s_mdns_dbg_pos, fmt, ap);
43+
va_end(ap);
44+
45+
if (len < 0) {
46+
return;
47+
}
48+
49+
s_mdns_dbg_pos += len;
50+
51+
if (s_mdns_dbg_pos >= MDNS_DBG_MAX_LINE - 1) {
52+
mdns_dbg_flush();
53+
}
54+
}
55+
56+
#define dbg_printf(...) mdns_dbg_printf(__VA_ARGS__)
57+
#else
1658
#define dbg_printf(...) printf(__VA_ARGS__)
59+
#define mdns_dbg_flush()
60+
#endif
1761

1862
void static dbg_packet(const uint8_t *data, size_t len)
1963
{
@@ -239,6 +283,7 @@ void static dbg_packet(const uint8_t *data, size_t len)
239283
}
240284
}
241285
}
286+
mdns_dbg_flush();
242287
}
243288

244289
void mdns_debug_tx_packet(mdns_tx_packet_t *p, uint8_t packet[MDNS_MAX_PACKET_SIZE], uint16_t index)
@@ -255,6 +300,7 @@ void mdns_debug_tx_packet(mdns_tx_packet_t *p, uint8_t packet[MDNS_MAX_PACKET_SI
255300
}
256301
#endif
257302
dbg_packet(packet, index);
303+
mdns_dbg_flush();
258304
}
259305

260306
void mdns_debug_rx_packet(mdns_rx_packet_t *packet, const uint8_t* data, uint16_t len)
@@ -271,6 +317,7 @@ void mdns_debug_rx_packet(mdns_rx_packet_t *packet, const uint8_t* data, uint16_
271317
}
272318
#endif
273319
dbg_packet(data, len);
320+
mdns_dbg_flush();
274321
}
275322

276323
static void dbg_printf_result(mdns_result_t *r_t)
@@ -302,13 +349,15 @@ static void dbg_printf_result(mdns_result_t *r_t)
302349
#endif
303350
r_a = r_a->next;
304351
}
352+
mdns_dbg_flush();
305353
}
306354

307355
void mdns_debug_printf_browse_result(mdns_result_t *r_t, mdns_browse_t *b_t)
308356
{
309357
dbg_printf("----------------sync browse %s.%s result---------------\n", b_t->service, b_t->proto);
310358
dbg_printf("browse pointer: %p\n", b_t);
311359
dbg_printf_result(r_t);
360+
mdns_dbg_flush();
312361
}
313362

314363
void mdns_debug_printf_browse_result_all(mdns_result_t *r_t)
@@ -319,4 +368,5 @@ void mdns_debug_printf_browse_result_all(mdns_result_t *r_t)
319368
dbg_printf_result(r_t);
320369
r_t = r_t->next;
321370
}
371+
mdns_dbg_flush();
322372
}

components/mdns/private_include/mdns_debug.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
#pragma once
77

88
#include <stddef.h>
9+
#include "sdkconfig.h"
910
#include "mdns_private.h"
1011

1112
#ifdef __cplusplus
1213
extern "C" {
1314
#endif
1415

1516
#ifdef CONFIG_MDNS_ENABLE_DEBUG_PRINTS
17+
#include "esp_log.h"
1618

1719
/* Define the debug macros for the mDNS module
1820
*/
1921
#define DBG_BROWSE_RESULTS(result, browse) mdns_debug_printf_browse_result(result, browse)
2022

2123
#define DBG_BROWSE_RESULTS_WITH_MSG(result, ...) do { \
22-
printf(__VA_ARGS__); \
24+
ESP_LOGD("mdns", __VA_ARGS__); \
2325
mdns_debug_printf_browse_result_all(result); \
2426
} while(0)
2527

0 commit comments

Comments
 (0)