Skip to content

Commit 1710e36

Browse files
committed
Logging refactoring to supports macros redefinition and -D USE_ESP_IDF_LOG=1
1 parent e56e8fc commit 1710e36

File tree

5 files changed

+131
-59
lines changed

5 files changed

+131
-59
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ jobs:
5555
board: esp32-s3-devkitc-1
5656
- env: ci-arduino-2
5757
board: esp32-c3-devkitc-02
58+
- env: ci-arduino-2-esp-idf_log
59+
board: esp32dev
5860

5961
- env: ci-arduino-3
6062
board: esp32dev
@@ -66,6 +68,8 @@ jobs:
6668
board: esp32-c3-devkitc-02
6769
- env: ci-arduino-3
6870
board: esp32-c6-devkitc-1
71+
- env: ci-arduino-3-esp-idf_log
72+
board: esp32dev
6973

7074
# - env: ci-arduino-rc
7175
# board: esp32dev

platformio.ini

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[platformio]
2-
default_envs = arduino-2, arduino-3
2+
default_envs = arduino-2, arduino-3, arduino-libretiny
33
lib_dir = .
44
; src_dir = examples/Client
55
; src_dir = examples/FetchWebsite
@@ -16,6 +16,7 @@ build_flags =
1616
-D CONFIG_ASYNC_TCP_STACK_SIZE=4096
1717
-D CONFIG_ARDUHAL_LOG_COLORS
1818
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
19+
; -D USE_ESP_IDF_LOG=1
1920
upload_protocol = esptool
2021
monitor_speed = 115200
2122
monitor_filters = esp32_exception_decoder, log2file
@@ -53,6 +54,21 @@ board = ${sysenv.PIO_BOARD}
5354
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.30-2/platform-espressif32.zip
5455
board = ${sysenv.PIO_BOARD}
5556

57+
[env:ci-arduino-2-esp-idf_log]
58+
59+
board = ${sysenv.PIO_BOARD}
60+
build_flags =
61+
${env.build_flags}
62+
-D USE_ESP_IDF_LOG=1
63+
-D TAG=\"core\"
64+
65+
[env:ci-arduino-3-esp-idf_log]
66+
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.30-2/platform-espressif32.zip
67+
board = ${sysenv.PIO_BOARD}
68+
build_flags =
69+
${env.build_flags}
70+
-D USE_ESP_IDF_LOG=1
71+
5672
[env:ci-arduino-rc]
5773
platform = https://github.com/pioarduino/platform-espressif32/releases/download/54.03.20-rc2/platform-espressif32.zip
5874
board = ${sysenv.PIO_BOARD}

src/AsyncTCP.cpp

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,13 @@
22
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
33

44
#include "AsyncTCP.h"
5+
#include "AsyncTCPLogging.h"
56
#include "AsyncTCPSimpleIntrusiveList.h"
67

7-
#ifndef LIBRETINY
8-
#include <esp_log.h>
9-
10-
#ifdef ARDUINO
11-
#include <esp32-hal.h>
12-
#include <esp32-hal-log.h>
13-
#if (ESP_IDF_VERSION_MAJOR >= 5)
14-
#include <NetworkInterface.h>
15-
#endif
16-
#else
17-
#include "esp_timer.h"
18-
#define log_e(...) ESP_LOGE(__FILE__, __VA_ARGS__)
19-
#define log_w(...) ESP_LOGW(__FILE__, __VA_ARGS__)
20-
#define log_i(...) ESP_LOGI(__FILE__, __VA_ARGS__)
21-
#define log_d(...) ESP_LOGD(__FILE__, __VA_ARGS__)
22-
#define log_v(...) ESP_LOGV(__FILE__, __VA_ARGS__)
23-
static unsigned long millis() {
24-
return (unsigned long)(esp_timer_get_time() / 1000ULL);
25-
}
26-
#endif
27-
#endif
28-
29-
#ifdef LIBRETINY
8+
/**
9+
* LibreTiny specific configurations
10+
*/
11+
#if defined(LIBRETINY)
3012
#include <Arduino.h>
3113
// LibreTiny does not support IDF - disable code that expects it to be available
3214
#define ESP_IDF_VERSION_MAJOR (0)
@@ -35,7 +17,28 @@ static unsigned long millis() {
3517
// ESP watchdog is not available
3618
#undef CONFIG_ASYNC_TCP_USE_WDT
3719
#define CONFIG_ASYNC_TCP_USE_WDT 0
38-
#endif
20+
#endif // LIBRETINY
21+
22+
/**
23+
* Arduino specific configurations
24+
*/
25+
#if defined(ARDUINO) && !defined(LIBRETINY)
26+
#include <Arduino.h>
27+
#include <esp_idf_version.h>
28+
#if (ESP_IDF_VERSION_MAJOR >= 5)
29+
#include <NetworkInterface.h>
30+
#endif // ESP_IDF_VERSION_MAJOR
31+
#endif // ARDUINO
32+
33+
/**
34+
* ESP-IDF specific configurations
35+
*/
36+
#if !defined(LIBRETINY) && !defined(ARDUINO)
37+
#include "esp_timer.h"
38+
static unsigned long millis() {
39+
return (unsigned long)(esp_timer_get_time() / 1000ULL);
40+
}
41+
#endif // !LIBRETINY && !ARDUINO
3942

4043
extern "C" {
4144
#include "lwip/dns.h"
@@ -231,7 +234,7 @@ static inline lwip_tcp_event_packet_t *_get_async_event() {
231234
next_pkt = _async_queue.begin()) {
232235
// if the next event that will come is a poll event for the same connection, we can discard it and continue
233236
_free_event(_async_queue.pop_front());
234-
log_d("coalescing polls, network congestion or async callbacks might be too slow!");
237+
async_tcp_log_d("coalescing polls, network congestion or async callbacks might be too slow!");
235238
}
236239

237240
/*
@@ -245,7 +248,7 @@ static inline lwip_tcp_event_packet_t *_get_async_event() {
245248
*/
246249
if (_async_queue.size() > (rand() % CONFIG_ASYNC_TCP_QUEUE_SIZE / 4 + CONFIG_ASYNC_TCP_QUEUE_SIZE * 3 / 4)) {
247250
_free_event(e);
248-
log_d("discarding poll due to queue congestion");
251+
async_tcp_log_d("discarding poll due to queue congestion");
249252
continue;
250253
}
251254

@@ -308,7 +311,7 @@ void AsyncTCP_detail::handle_async_event(lwip_tcp_event_packet_t *e) {
308311
static void _async_service_task(void *pvParameters) {
309312
#if CONFIG_ASYNC_TCP_USE_WDT
310313
if (esp_task_wdt_add(NULL) != ESP_OK) {
311-
log_w("Failed to add async task to WDT");
314+
async_tcp_log_w("Failed to add async task to WDT");
312315
}
313316
#endif
314317
for (;;) {
@@ -404,7 +407,7 @@ static int8_t _tcp_connected(void *arg, tcp_pcb *pcb, int8_t err) {
404407
AsyncClient *client = reinterpret_cast<AsyncClient *>(arg);
405408
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t{LWIP_TCP_CONNECTED, client};
406409
if (!e) {
407-
log_e("Failed to allocate event packet");
410+
async_tcp_log_e("Failed to allocate event packet");
408411
return ERR_MEM;
409412
}
410413
e->connected.pcb = pcb;
@@ -418,9 +421,9 @@ int8_t AsyncTCP_detail::tcp_poll(void *arg, struct tcp_pcb *pcb) {
418421
// throttle polling events queueing when event queue is getting filled up, let it handle _onack's
419422
{
420423
queue_mutex_guard guard;
421-
// log_d("qs:%u", _async_queue.size());
424+
// async_tcp_log_d("qs:%u", _async_queue.size());
422425
if (_async_queue.size() > (rand() % CONFIG_ASYNC_TCP_QUEUE_SIZE / 2 + CONFIG_ASYNC_TCP_QUEUE_SIZE / 4)) {
423-
log_d("throttling");
426+
async_tcp_log_d("throttling");
424427
return ERR_OK;
425428
}
426429
}
@@ -429,7 +432,7 @@ int8_t AsyncTCP_detail::tcp_poll(void *arg, struct tcp_pcb *pcb) {
429432
AsyncClient *client = reinterpret_cast<AsyncClient *>(arg);
430433
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t{LWIP_TCP_POLL, client};
431434
if (!e) {
432-
log_e("Failed to allocate event packet");
435+
async_tcp_log_e("Failed to allocate event packet");
433436
return ERR_MEM;
434437
}
435438
e->poll.pcb = pcb;
@@ -443,7 +446,7 @@ int8_t AsyncTCP_detail::tcp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb
443446
AsyncClient *client = reinterpret_cast<AsyncClient *>(arg);
444447
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t{LWIP_TCP_RECV, client};
445448
if (!e) {
446-
log_e("Failed to allocate event packet");
449+
async_tcp_log_e("Failed to allocate event packet");
447450
return ERR_MEM;
448451
}
449452
if (pb) {
@@ -470,7 +473,7 @@ int8_t AsyncTCP_detail::tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
470473
AsyncClient *client = reinterpret_cast<AsyncClient *>(arg);
471474
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t{LWIP_TCP_SENT, client};
472475
if (!e) {
473-
log_e("Failed to allocate event packet");
476+
async_tcp_log_e("Failed to allocate event packet");
474477
return ERR_MEM;
475478
}
476479
e->sent.pcb = pcb;
@@ -493,7 +496,7 @@ void AsyncTCP_detail::tcp_error(void *arg, int8_t err) {
493496
// enqueue event to be processed in the async task for the user callback
494497
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t{LWIP_TCP_ERROR, client};
495498
if (!e) {
496-
log_e("Failed to allocate event packet");
499+
async_tcp_log_e("Failed to allocate event packet");
497500
return;
498501
}
499502
e->error.err = err;
@@ -508,7 +511,7 @@ static void _tcp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
508511

509512
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t{LWIP_TCP_DNS, client};
510513
if (!e) {
511-
log_e("Failed to allocate event packet");
514+
async_tcp_log_e("Failed to allocate event packet");
512515
return;
513516
}
514517

@@ -819,11 +822,11 @@ void AsyncClient::onPoll(AcConnectHandler cb, void *arg) {
819822

820823
bool AsyncClient::connect(ip_addr_t addr, uint16_t port) {
821824
if (_pcb) {
822-
log_d("already connected, state %d", _pcb->state);
825+
async_tcp_log_d("already connected, state %d", _pcb->state);
823826
return false;
824827
}
825828
if (!_start_async_task()) {
826-
log_e("failed to start task");
829+
async_tcp_log_e("failed to start task");
827830
return false;
828831
}
829832

@@ -836,7 +839,7 @@ bool AsyncClient::connect(ip_addr_t addr, uint16_t port) {
836839
pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
837840
#endif
838841
if (!pcb) {
839-
log_e("pcb == NULL");
842+
async_tcp_log_e("pcb == NULL");
840843
return false;
841844
}
842845
_bind_tcp_callbacks(pcb, this);
@@ -878,7 +881,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
878881
ip_addr_t addr;
879882

880883
if (!_start_async_task()) {
881-
log_e("failed to start task");
884+
async_tcp_log_e("failed to start task");
882885
return false;
883886
}
884887

@@ -905,7 +908,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
905908
_connect_port = port;
906909
return true;
907910
}
908-
log_d("error: %d", err);
911+
async_tcp_log_d("error: %d", err);
909912
return false;
910913
}
911914

@@ -1018,7 +1021,7 @@ void AsyncClient::_error(int8_t err) {
10181021
// In LwIP Thread
10191022
int8_t AsyncClient::_lwip_fin(tcp_pcb *pcb, int8_t err) {
10201023
if (!_pcb || pcb != _pcb) {
1021-
log_d("0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t)pcb, (uint32_t)_pcb);
1024+
async_tcp_log_d("0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t)pcb, (uint32_t)_pcb);
10221025
return ERR_OK;
10231026
}
10241027
_reset_tcp_callbacks(_pcb, this);
@@ -1072,11 +1075,11 @@ int8_t AsyncClient::_recv(tcp_pcb *pcb, pbuf *pb, int8_t err) {
10721075

10731076
int8_t AsyncClient::_poll(tcp_pcb *pcb) {
10741077
if (!_pcb) {
1075-
// log_d("pcb is NULL");
1078+
// async_tcp_log_d("pcb is NULL");
10761079
return ERR_OK;
10771080
}
10781081
if (pcb != _pcb) {
1079-
log_d("0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t)pcb, (uint32_t)_pcb);
1082+
async_tcp_log_d("0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t)pcb, (uint32_t)_pcb);
10801083
return ERR_OK;
10811084
}
10821085

@@ -1087,7 +1090,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
10871090
const uint32_t one_day = 86400000;
10881091
bool last_tx_is_after_last_ack = (_rx_last_ack - _tx_last_packet + one_day) < one_day;
10891092
if (last_tx_is_after_last_ack && (now - _tx_last_packet) >= _ack_timeout) {
1090-
log_d("ack timeout %d", pcb->state);
1093+
async_tcp_log_d("ack timeout %d", pcb->state);
10911094
if (_timeout_cb) {
10921095
_timeout_cb(_timeout_cb_arg, this, (now - _tx_last_packet));
10931096
}
@@ -1096,7 +1099,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
10961099
}
10971100
// RX Timeout
10981101
if (_rx_timeout && (now - _rx_last_packet) >= (_rx_timeout * 1000)) {
1099-
log_d("rx timeout %d", pcb->state);
1102+
async_tcp_log_d("rx timeout %d", pcb->state);
11001103
_close();
11011104
return ERR_OK;
11021105
}
@@ -1485,7 +1488,7 @@ void AsyncServer::begin() {
14851488
}
14861489

14871490
if (!_start_async_task()) {
1488-
log_e("failed to start task");
1491+
async_tcp_log_e("failed to start task");
14891492
return;
14901493
}
14911494
int8_t err;
@@ -1498,22 +1501,22 @@ void AsyncServer::begin() {
14981501
#endif
14991502
}
15001503
if (!_pcb) {
1501-
log_e("_pcb == NULL");
1504+
async_tcp_log_e("_pcb == NULL");
15021505
return;
15031506
}
15041507

15051508
err = _tcp_bind(&_pcb, &_addr, _port);
15061509

15071510
if (err != ERR_OK) {
15081511
// pcb was closed by _tcp_bind
1509-
log_e("bind error: %d", err);
1512+
async_tcp_log_e("bind error: %d", err);
15101513
return;
15111514
}
15121515

15131516
static uint8_t backlog = 5;
15141517
_pcb = _tcp_listen_with_backlog(_pcb, backlog);
15151518
if (!_pcb) {
1516-
log_e("listen_pcb == NULL");
1519+
async_tcp_log_e("listen_pcb == NULL");
15171520
return;
15181521
}
15191522
tcp_core_guard tcg;
@@ -1536,7 +1539,7 @@ void AsyncServer::end() {
15361539
// runs on LwIP thread
15371540
int8_t AsyncTCP_detail::tcp_accept(void *arg, tcp_pcb *pcb, int8_t err) {
15381541
if (!pcb) {
1539-
log_e("_accept failed: pcb is NULL");
1542+
async_tcp_log_e("_accept failed: pcb is NULL");
15401543
return ERR_ABRT;
15411544
}
15421545
auto server = reinterpret_cast<AsyncServer *>(arg);
@@ -1558,20 +1561,20 @@ int8_t AsyncTCP_detail::tcp_accept(void *arg, tcp_pcb *pcb, int8_t err) {
15581561
// We can't let the client object call in to close, as we're on the LWIP thread; it could deadlock trying to RPC to itself
15591562
c->_pcb = nullptr;
15601563
tcp_abort(pcb);
1561-
log_e("_accept failed: couldn't accept client");
1564+
async_tcp_log_e("_accept failed: couldn't accept client");
15621565
return ERR_ABRT;
15631566
}
15641567
if (c) {
15651568
// Couldn't complete setup
15661569
// pcb has already been aborted
15671570
delete c;
15681571
pcb = nullptr;
1569-
log_e("_accept failed: couldn't complete setup");
1572+
async_tcp_log_e("_accept failed: couldn't complete setup");
15701573
return ERR_ABRT;
15711574
}
1572-
log_e("_accept failed: couldn't allocate client");
1575+
async_tcp_log_e("_accept failed: couldn't allocate client");
15731576
} else {
1574-
log_e("_accept failed: no onConnect callback");
1577+
async_tcp_log_e("_accept failed: no onConnect callback");
15751578
}
15761579
tcp_abort(pcb);
15771580
return ERR_OK;

src/AsyncTCP.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "AsyncTCPVersion.h"
88
#define ASYNCTCP_FORK_ESP32Async
99

10-
#ifndef LIBRETINY
11-
#include <esp_idf_version.h>
12-
#endif
13-
1410
#ifdef ARDUINO
1511
#include "IPAddress.h"
1612
#if __has_include(<IPv6Address.h>)

0 commit comments

Comments
 (0)