From 9767e5e1203fe3ca9508cba02ae7487eba1204cc Mon Sep 17 00:00:00 2001 From: Mattia Pini Date: Sun, 21 Feb 2021 12:50:11 +0100 Subject: [PATCH 1/3] Added possibility to use ESP32-IDF log insted of redefined one by Arduino HAL --- cores/esp32/esp32-hal-log.c | 42 +++++++++++++++++++++++++++++++++++++ cores/esp32/esp32-hal-log.h | 25 ++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 cores/esp32/esp32-hal-log.c diff --git a/cores/esp32/esp32-hal-log.c b/cores/esp32/esp32-hal-log.c new file mode 100644 index 00000000000..4a5d1383876 --- /dev/null +++ b/cores/esp32/esp32-hal-log.c @@ -0,0 +1,42 @@ +#ifndef __MY_LOG__ +#define __MY_LOG__ +#include "stdio.h" +#include "esp32-hal-log.h" +#ifdef USE_ESP32_LOG + void log_to_esp(esp_log_level_t level, const char *format, ...) + { + va_list va_args; + va_start(va_args, format); + + char log_buffer[512]; + int len = vsnprintf(log_buffer, sizeof(log_buffer), format, va_args); + if (len > 0) + { + switch (level) + { + case ESP_LOG_ERROR: + ESP_LOGE(TAG, "%s", log_buffer); + break; + case ESP_LOG_DEBUG: + ESP_LOGD(TAG, "%s", log_buffer); + break; + case ESP_LOG_WARN: + ESP_LOGW(TAG, "%s", log_buffer); + break; + case ESP_LOG_INFO: + ESP_LOGI(TAG, "%s", log_buffer); + break; + case ESP_LOG_VERBOSE: + ESP_LOGV(TAG, "%s", log_buffer); + break; + case ESP_LOG_NONE: + //do nothing + break; + } + } + + va_end(va_args); + } +#endif +#endif + diff --git a/cores/esp32/esp32-hal-log.h b/cores/esp32/esp32-hal-log.h index bdde1e6b9e1..5789f8a6f7e 100644 --- a/cores/esp32/esp32-hal-log.h +++ b/cores/esp32/esp32-hal-log.h @@ -1,4 +1,4 @@ -// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - #ifndef __ARDUHAL_LOG_H__ #define __ARDUHAL_LOG_H__ @@ -37,6 +36,9 @@ extern "C" #define ARDUHAL_LOG_LEVEL CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL #else #define ARDUHAL_LOG_LEVEL CORE_DEBUG_LEVEL +#ifdef USE_ESP32_LOG +#define LOG_LOCAL_LEVEL CORE_DEBUG_LEVEL +#endif #endif #ifndef CONFIG_ARDUHAL_LOG_COLORS @@ -72,12 +74,15 @@ extern "C" #define ARDUHAL_LOG_RESET_COLOR #endif + + const char * pathToFileName(const char * path); int log_printf(const char *fmt, ...); #define ARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n" #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__ +#ifndef USE_ESP32_LOG #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE #define log_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__) #define isr_log_v(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__) @@ -125,9 +130,24 @@ int log_printf(const char *fmt, ...); #define log_n(format, ...) #define isr_log_n(format, ...) #endif +#endif #include "esp_log.h" +#ifdef USE_ESP32_LOG + +#ifndef TAG +#define TAG "ESP32" +#endif +void log_to_esp(esp_log_level_t level, const char* format, ...); + +#define log_e(format, ...) do {log_to_esp(ESP_LOG_ERROR, format, ##__VA_ARGS__);}while(0) +#define log_w(format, ...) do {log_to_esp(ESP_LOG_WARN, format, ##__VA_ARGS__);}while(0) +#define log_d(format, ...) do {log_to_esp(ESP_LOG_DEBUG, format, ##__VA_ARGS__);}while(0) +#define log_i(format, ...) do {log_to_esp(ESP_LOG_INFO, format, ##__VA_ARGS__);}while(0) +#define log_v(format, ...) do {log_to_esp(ESP_LOG_VERBOSE, format, ##__VA_ARGS__);}while(0) +//#define log_n(format, ...) myLog(ESP_LOG_NONE, format, ##__VA_ARGS__) +#else #ifdef CONFIG_ARDUHAL_ESP_LOG #undef ESP_LOGE #undef ESP_LOGW @@ -151,6 +171,7 @@ int log_printf(const char *fmt, ...); #define ESP_EARLY_LOGD(tag, ...) isr_log_d(__VA_ARGS__) #define ESP_EARLY_LOGV(tag, ...) isr_log_v(__VA_ARGS__) #endif +#endif #ifdef __cplusplus } From 3e60f6c5a01d9a699021cc4cc4909f74ff910d02 Mon Sep 17 00:00:00 2001 From: Mattia Pini Date: Sun, 21 Feb 2021 14:20:03 +0100 Subject: [PATCH 2/3] Fixed CI --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7c6ac4fc6b..5d2c0600162 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(CORE_SRCS cores/esp32/esp32-hal-dac.c cores/esp32/esp32-hal-gpio.c cores/esp32/esp32-hal-i2c.c + cores/esp32/esp32-hal-log.c cores/esp32/esp32-hal-ledc.c cores/esp32/esp32-hal-matrix.c cores/esp32/esp32-hal-misc.c From b49f0568b06ee8909c9096a14cbb07203b684ec1 Mon Sep 17 00:00:00 2001 From: Mattia Pini Date: Mon, 22 Feb 2021 21:21:39 +0100 Subject: [PATCH 3/3] Renamed USE_ESP32_LOG to USE_ESP_IDF_LOG Changed TAG definition to "arduino" Removed switch in log_to_esp and used ESP_LOG_LEVEL_LOCAL Added missing isr_log_* definitions FIxed conditional definitions of log MACROs --- cores/esp32/esp32-hal-log.c | 47 ++++++++++--------------------------- cores/esp32/esp32-hal-log.h | 47 +++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/cores/esp32/esp32-hal-log.c b/cores/esp32/esp32-hal-log.c index 4a5d1383876..2dbaf3a3f9b 100644 --- a/cores/esp32/esp32-hal-log.c +++ b/cores/esp32/esp32-hal-log.c @@ -2,41 +2,18 @@ #define __MY_LOG__ #include "stdio.h" #include "esp32-hal-log.h" -#ifdef USE_ESP32_LOG - void log_to_esp(esp_log_level_t level, const char *format, ...) - { - va_list va_args; - va_start(va_args, format); +void log_to_esp(char* tag, esp_log_level_t level, const char *format, ...) +{ + va_list va_args; + va_start(va_args, format); - char log_buffer[512]; - int len = vsnprintf(log_buffer, sizeof(log_buffer), format, va_args); - if (len > 0) - { - switch (level) - { - case ESP_LOG_ERROR: - ESP_LOGE(TAG, "%s", log_buffer); - break; - case ESP_LOG_DEBUG: - ESP_LOGD(TAG, "%s", log_buffer); - break; - case ESP_LOG_WARN: - ESP_LOGW(TAG, "%s", log_buffer); - break; - case ESP_LOG_INFO: - ESP_LOGI(TAG, "%s", log_buffer); - break; - case ESP_LOG_VERBOSE: - ESP_LOGV(TAG, "%s", log_buffer); - break; - case ESP_LOG_NONE: - //do nothing - break; - } - } - - va_end(va_args); + char log_buffer[512]; + int len = vsnprintf(log_buffer, sizeof(log_buffer), format, va_args); + if (len > 0) + { + ESP_LOG_LEVEL_LOCAL(level, tag, "%s", log_buffer); } -#endif -#endif + va_end(va_args); +} +#endif diff --git a/cores/esp32/esp32-hal-log.h b/cores/esp32/esp32-hal-log.h index 5789f8a6f7e..18271cfb4e9 100644 --- a/cores/esp32/esp32-hal-log.h +++ b/cores/esp32/esp32-hal-log.h @@ -36,7 +36,7 @@ extern "C" #define ARDUHAL_LOG_LEVEL CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL #else #define ARDUHAL_LOG_LEVEL CORE_DEBUG_LEVEL -#ifdef USE_ESP32_LOG +#ifdef USE_ESP_IDF_LOG #define LOG_LOCAL_LEVEL CORE_DEBUG_LEVEL #endif #endif @@ -82,70 +82,91 @@ int log_printf(const char *fmt, ...); #define ARDUHAL_SHORT_LOG_FORMAT(letter, format) ARDUHAL_LOG_COLOR_ ## letter format ARDUHAL_LOG_RESET_COLOR "\r\n" #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__ -#ifndef USE_ESP32_LOG #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE +#ifndef USE_ESP_IDF_LOG #define log_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__) #define isr_log_v(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__) #else +#define log_v(format, ...) do {log_to_esp(TAG, ESP_LOG_VERBOSE, format, ##__VA_ARGS__);}while(0) +#define isr_log_v(format, ...) do {ets_printf(LOG_FORMAT(V, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0) +#endif +#else #define log_v(format, ...) #define isr_log_v(format, ...) #endif #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG +#ifndef USE_ESP_IDF_LOG #define log_d(format, ...) log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__) #define isr_log_d(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__) #else +#define log_d(format, ...) do {log_to_esp(TAG, ESP_LOG_DEBUG, format, ##__VA_ARGS__);}while(0) +#define isr_log_d(format, ...) do {ets_printf(LOG_FORMAT(D, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0) +#endif +#else #define log_d(format, ...) #define isr_log_d(format, ...) #endif #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO +#ifndef USE_ESP_IDF_LOG #define log_i(format, ...) log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__) #define isr_log_i(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__) #else +#define log_i(format, ...) do {log_to_esp(TAG, ESP_LOG_INFO, format, ##__VA_ARGS__);}while(0) +#define isr_log_i(format, ...) do {ets_printf(LOG_FORMAT(I, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0) +#endif +#else #define log_i(format, ...) #define isr_log_i(format, ...) #endif #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN +#ifndef USE_ESP_IDF_LOG #define log_w(format, ...) log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__) #define isr_log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__) #else +#define log_w(format, ...) do {log_to_esp(TAG, ESP_LOG_WARN, format, ##__VA_ARGS__);}while(0) +#define isr_log_w(format, ...) do {ets_printf(LOG_FORMAT(W, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0) +#endif +#else #define log_w(format, ...) #define isr_log_w(format, ...) #endif #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR +#ifndef USE_ESP_IDF_LOG #define log_e(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__) #define isr_log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__) #else +#define log_e(format, ...) do {log_to_esp(TAG, ESP_LOG_ERROR, format, ##__VA_ARGS__);}while(0) +#define isr_log_e(format, ...) do {ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0) +#endif +#else #define log_e(format, ...) #define isr_log_e(format, ...) #endif #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_NONE +#ifndef USE_ESP_IDF_LOG #define log_n(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__) #define isr_log_n(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__) #else +#define log_n(format, ...) do {log_to_esp(TAG, ESP_LOG_ERROR, format, ##__VA_ARGS__);}while(0) +#define isr_log_n(format, ...) do {ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), TAG, ##__VA_ARGS__);}while(0) +#endif +#else #define log_n(format, ...) #define isr_log_n(format, ...) #endif -#endif #include "esp_log.h" -#ifdef USE_ESP32_LOG - +#ifdef USE_ESP_IDF_LOG #ifndef TAG -#define TAG "ESP32" +#define TAG "ARDUINO" #endif -void log_to_esp(esp_log_level_t level, const char* format, ...); - -#define log_e(format, ...) do {log_to_esp(ESP_LOG_ERROR, format, ##__VA_ARGS__);}while(0) -#define log_w(format, ...) do {log_to_esp(ESP_LOG_WARN, format, ##__VA_ARGS__);}while(0) -#define log_d(format, ...) do {log_to_esp(ESP_LOG_DEBUG, format, ##__VA_ARGS__);}while(0) -#define log_i(format, ...) do {log_to_esp(ESP_LOG_INFO, format, ##__VA_ARGS__);}while(0) -#define log_v(format, ...) do {log_to_esp(ESP_LOG_VERBOSE, format, ##__VA_ARGS__);}while(0) +void log_to_esp(char* tag, esp_log_level_t level, const char* format, ...); //#define log_n(format, ...) myLog(ESP_LOG_NONE, format, ##__VA_ARGS__) #else #ifdef CONFIG_ARDUHAL_ESP_LOG