Skip to content

Commit b8dab5e

Browse files
authored
Added possibility to use ESP32-IDF log insted of redefined one (#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

Diff for: CMakeLists.txt

+1
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

Diff for: cores/esp32/esp32-hal-log.c

+19
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+
void log_to_esp(char* tag, esp_log_level_t level, const char *format, ...)
6+
{
7+
va_list va_args;
8+
va_start(va_args, format);
9+
10+
char log_buffer[512];
11+
int len = 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

Diff for: cores/esp32/esp32-hal-log.h

+44-2
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
#define ARDUHAL_LOG_LEVEL CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL
3837
#else
3938
#define ARDUHAL_LOG_LEVEL CORE_DEBUG_LEVEL
39+
#ifdef USE_ESP_IDF_LOG
40+
#define LOG_LOCAL_LEVEL CORE_DEBUG_LEVEL
41+
#endif
4042
#endif
4143

4244
#ifndef CONFIG_ARDUHAL_LOG_COLORS
@@ -72,62 +74,101 @@ extern "C"
7274
#define ARDUHAL_LOG_RESET_COLOR
7375
#endif
7476

77+
78+
7579
const char * pathToFileName(const char * path);
7680
int log_printf(const char *fmt, ...);
7781

7882
#define ARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n"
7983
#define ARDUHAL_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
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
86+
#ifndef USE_ESP_IDF_LOG
8287
#define log_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
8388
#define isr_log_v(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
8489
#else
90+
#define log_v(format, ...) do {log_to_esp(TAG, ESP_LOG_VERBOSE, format, ##__VA_ARGS__);}while(0)
91+
#define isr_log_v(format, ...) do {ets_printf(LOG_FORMAT(V, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0)
92+
#endif
93+
#else
8594
#define log_v(format, ...)
8695
#define isr_log_v(format, ...)
8796
#endif
8897

8998
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
99+
#ifndef USE_ESP_IDF_LOG
90100
#define log_d(format, ...) log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
91101
#define isr_log_d(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
92102
#else
103+
#define log_d(format, ...) do {log_to_esp(TAG, ESP_LOG_DEBUG, format, ##__VA_ARGS__);}while(0)
104+
#define isr_log_d(format, ...) do {ets_printf(LOG_FORMAT(D, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0)
105+
#endif
106+
#else
93107
#define log_d(format, ...)
94108
#define isr_log_d(format, ...)
95109
#endif
96110

97111
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
112+
#ifndef USE_ESP_IDF_LOG
98113
#define log_i(format, ...) log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
99114
#define isr_log_i(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
100115
#else
116+
#define log_i(format, ...) do {log_to_esp(TAG, ESP_LOG_INFO, format, ##__VA_ARGS__);}while(0)
117+
#define isr_log_i(format, ...) do {ets_printf(LOG_FORMAT(I, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0)
118+
#endif
119+
#else
101120
#define log_i(format, ...)
102121
#define isr_log_i(format, ...)
103122
#endif
104123

105124
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
125+
#ifndef USE_ESP_IDF_LOG
106126
#define log_w(format, ...) log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
107127
#define isr_log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
108128
#else
129+
#define log_w(format, ...) do {log_to_esp(TAG, ESP_LOG_WARN, format, ##__VA_ARGS__);}while(0)
130+
#define isr_log_w(format, ...) do {ets_printf(LOG_FORMAT(W, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0)
131+
#endif
132+
#else
109133
#define log_w(format, ...)
110134
#define isr_log_w(format, ...)
111135
#endif
112136

113137
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
138+
#ifndef USE_ESP_IDF_LOG
114139
#define log_e(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
115140
#define isr_log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
116141
#else
142+
#define log_e(format, ...) do {log_to_esp(TAG, ESP_LOG_ERROR, format, ##__VA_ARGS__);}while(0)
143+
#define isr_log_e(format, ...) do {ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0)
144+
#endif
145+
#else
117146
#define log_e(format, ...)
118147
#define isr_log_e(format, ...)
119148
#endif
120149

121150
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_NONE
151+
#ifndef USE_ESP_IDF_LOG
122152
#define log_n(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
123153
#define isr_log_n(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
124154
#else
155+
#define log_n(format, ...) do {log_to_esp(TAG, ESP_LOG_ERROR, format, ##__VA_ARGS__);}while(0)
156+
#define isr_log_n(format, ...) do {ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0)
157+
#endif
158+
#else
125159
#define log_n(format, ...)
126160
#define isr_log_n(format, ...)
127161
#endif
128162

129163
#include "esp_log.h"
130164

165+
#ifdef USE_ESP_IDF_LOG
166+
#ifndef TAG
167+
#define TAG "ARDUINO"
168+
#endif
169+
void log_to_esp(char* tag, esp_log_level_t level, const char* format, ...);
170+
//#define log_n(format, ...) myLog(ESP_LOG_NONE, format, ##__VA_ARGS__)
171+
#else
131172
#ifdef CONFIG_ARDUHAL_ESP_LOG
132173
#undef ESP_LOGE
133174
#undef ESP_LOGW
@@ -151,6 +192,7 @@ int log_printf(const char *fmt, ...);
151192
#define ESP_EARLY_LOGD(tag, ...) isr_log_d(__VA_ARGS__)
152193
#define ESP_EARLY_LOGV(tag, ...) isr_log_v(__VA_ARGS__)
153194
#endif
195+
#endif
154196

155197
#ifdef __cplusplus
156198
}

0 commit comments

Comments
 (0)