-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added possibility to use ESP32-IDF log insted of redefined one #4845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please pass also the TAG as argument: void log_to_esp(esp_log_level_t level, const char *tag, const char *format, ...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here i wanted to replicate the actual behavior of the arduino library, so the tag is fixed. |
||
{ | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this switch can be replaced by the following line: ESP_LOG_LEVEL_LOCAL(level, tag, "%s", log_buffer); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was my decision to doesn't call directly ESP_LOG_LEVEL_LOCAL, instead using the provided MACROs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by the way, the following should also work: ESP_LOG_LEVEL_LOCAL(level, tag, (const char *)log_buffer); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, in this way the compiler says : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok :) |
||
{ | ||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that renaming the name to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, let me change it |
||
#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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using TAG like "arduino" makes more sense here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem , i will change it for sure :) |
||
#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 | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove the
#ifdef
switch. Function will not be included in the final binary if not called within the application and will at the same time allowUSE_ESP32_LOG
to be defined on per file basis.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! :)