Skip to content

Commit b8dab5e

Browse files
authored
Added possibility to use ESP32-IDF log insted of redefined one (espressif#4845)
With this PR user can select to use the original ESP-IDF log instead of the redefined one. User can also redefine the log function as per [Logging Library](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/log.html#_CPPv419esp_log_set_vprintf14vprintf_like_t) so he can for example redirect logs to a file. To enable this change just add -DUSE_ESP32_LOG to build flags. User can also change the default TAG (that now is ES32) to whatever it wants adding '-DTAG="tag_value"' to build flags
1 parent 2141313 commit b8dab5e

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(CORE_SRCS
77
cores/esp32/esp32-hal-dac.c
88
cores/esp32/esp32-hal-gpio.c
99
cores/esp32/esp32-hal-i2c.c
10+
cores/esp32/esp32-hal-log.c
1011
cores/esp32/esp32-hal-ledc.c
1112
cores/esp32/esp32-hal-matrix.c
1213
cores/esp32/esp32-hal-misc.c

‎cores/esp32/esp32-hal-log.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef__MY_LOG__
2+
#define__MY_LOG__
3+
#include"stdio.h"
4+
#include"esp32-hal-log.h"
5+
voidlog_to_esp(char*tag, esp_log_level_tlevel, constchar*format, ...)
6+
{
7+
va_listva_args;
8+
va_start(va_args, format);
9+
10+
charlog_buffer[512];
11+
intlen=vsnprintf(log_buffer, sizeof(log_buffer), format, va_args);
12+
if (len>0)
13+
{
14+
ESP_LOG_LEVEL_LOCAL(level, tag, "%s", log_buffer);
15+
}
16+
17+
va_end(va_args);
18+
}
19+
#endif

‎cores/esp32/esp32-hal-log.h‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -11,7 +11,6 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
1514
#ifndef__ARDUHAL_LOG_H__
1615
#define__ARDUHAL_LOG_H__
1716

@@ -37,6 +36,9 @@ extern "C"
3736
#defineARDUHAL_LOG_LEVEL CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL
3837
#else
3938
#defineARDUHAL_LOG_LEVEL CORE_DEBUG_LEVEL
39+
#ifdefUSE_ESP_IDF_LOG
40+
#defineLOG_LOCAL_LEVEL CORE_DEBUG_LEVEL
41+
#endif
4042
#endif
4143

4244
#ifndefCONFIG_ARDUHAL_LOG_COLORS
@@ -72,62 +74,101 @@ extern "C"
7274
#defineARDUHAL_LOG_RESET_COLOR
7375
#endif
7476

77+
78+
7579
constchar*pathToFileName(constchar*path);
7680
intlog_printf(constchar*fmt, ...);
7781

7882
#defineARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n"
7983
#defineARDUHAL_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter "[" #letter "][%s:%u] %s(): " format ARDUHAL_LOG_RESET_COLOR "\r\n", pathToFileName(__FILE__), __LINE__, __FUNCTION__
8084

8185
#ifARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
86+
#ifndefUSE_ESP_IDF_LOG
8287
#definelog_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
8388
#defineisr_log_v(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
8489
#else
90+
#definelog_v(format, ...) do{log_to_esp(TAG, ESP_LOG_VERBOSE, format, ##__VA_ARGS__)}while(0)
91+
#defineisr_log_v(format, ...) do{ets_printf(LOG_FORMAT(V, format), esp_log_timestamp(), TAG, ##__VA_ARGS__)}while(0)
92+
#endif
93+
#else
8594
#definelog_v(format, ...)
8695
#defineisr_log_v(format, ...)
8796
#endif
8897

8998
#ifARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
99+
#ifndefUSE_ESP_IDF_LOG
90100
#definelog_d(format, ...) log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
91101
#defineisr_log_d(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
92102
#else
103+
#definelog_d(format, ...) do{log_to_esp(TAG, ESP_LOG_DEBUG, format, ##__VA_ARGS__)}while(0)
104+
#defineisr_log_d(format, ...) do{ets_printf(LOG_FORMAT(D, format), esp_log_timestamp(), TAG, ##__VA_ARGS__)}while(0)
105+
#endif
106+
#else
93107
#definelog_d(format, ...)
94108
#defineisr_log_d(format, ...)
95109
#endif
96110

97111
#ifARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
112+
#ifndefUSE_ESP_IDF_LOG
98113
#definelog_i(format, ...) log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
99114
#defineisr_log_i(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
100115
#else
116+
#definelog_i(format, ...) do{log_to_esp(TAG, ESP_LOG_INFO, format, ##__VA_ARGS__)}while(0)
117+
#defineisr_log_i(format, ...) do{ets_printf(LOG_FORMAT(I, format), esp_log_timestamp(), TAG, ##__VA_ARGS__)}while(0)
118+
#endif
119+
#else
101120
#definelog_i(format, ...)
102121
#defineisr_log_i(format, ...)
103122
#endif
104123

105124
#ifARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
125+
#ifndefUSE_ESP_IDF_LOG
106126
#definelog_w(format, ...) log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
107127
#defineisr_log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
108128
#else
129+
#definelog_w(format, ...) do{log_to_esp(TAG, ESP_LOG_WARN, format, ##__VA_ARGS__)}while(0)
130+
#defineisr_log_w(format, ...) do{ets_printf(LOG_FORMAT(W, format), esp_log_timestamp(), TAG, ##__VA_ARGS__)}while(0)
131+
#endif
132+
#else
109133
#definelog_w(format, ...)
110134
#defineisr_log_w(format, ...)
111135
#endif
112136

113137
#ifARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
138+
#ifndefUSE_ESP_IDF_LOG
114139
#definelog_e(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
115140
#defineisr_log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
116141
#else
142+
#definelog_e(format, ...) do{log_to_esp(TAG, ESP_LOG_ERROR, format, ##__VA_ARGS__)}while(0)
143+
#defineisr_log_e(format, ...) do{ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), TAG, ##__VA_ARGS__)}while(0)
144+
#endif
145+
#else
117146
#definelog_e(format, ...)
118147
#defineisr_log_e(format, ...)
119148
#endif
120149

121150
#ifARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_NONE
151+
#ifndefUSE_ESP_IDF_LOG
122152
#definelog_n(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
123153
#defineisr_log_n(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
124154
#else
155+
#definelog_n(format, ...) do{log_to_esp(TAG, ESP_LOG_ERROR, format, ##__VA_ARGS__)}while(0)
156+
#defineisr_log_n(format, ...) do{ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), TAG, ##__VA_ARGS__)}while(0)
157+
#endif
158+
#else
125159
#definelog_n(format, ...)
126160
#defineisr_log_n(format, ...)
127161
#endif
128162

129163
#include"esp_log.h"
130164

165+
#ifdefUSE_ESP_IDF_LOG
166+
#ifndefTAG
167+
#defineTAG "ARDUINO"
168+
#endif
169+
voidlog_to_esp(char*tag, esp_log_level_tlevel, constchar*format, ...);
170+
//#define log_n(format, ...) myLog(ESP_LOG_NONE, format, ##__VA_ARGS__)
171+
#else
131172
#ifdefCONFIG_ARDUHAL_ESP_LOG
132173
#undef ESP_LOGE
133174
#undef ESP_LOGW
@@ -151,6 +192,7 @@ int log_printf(const char *fmt, ...);
151192
#defineESP_EARLY_LOGD(tag, ...) isr_log_d(__VA_ARGS__)
152193
#defineESP_EARLY_LOGV(tag, ...) isr_log_v(__VA_ARGS__)
153194
#endif
195+
#endif
154196

155197
#ifdef__cplusplus
156198
}

0 commit comments

Comments
(0)