diff --git a/components/log/include/esp_log_buffer.h b/components/log/include/esp_log_buffer.h index 5453061403..d9e3a49807 100644 --- a/components/log/include/esp_log_buffer.h +++ b/components/log/include/esp_log_buffer.h @@ -80,7 +80,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 * @param level Log level */ #define ESP_LOG_BUFFER_HEX_LEVEL(tag, buffer, buff_len, level) \ - do { if (LOG_LOCAL_LEVEL >= (level)) {esp_log_buffer_hex_internal(tag, buffer, buff_len, level);} } while(0) + do { if (ESP_LOG_ENABLED(level)) {esp_log_buffer_hex_internal(tag, buffer, buff_len, level);} } while(0) /** * @brief Log a buffer of characters at specified level, separated into 16 bytes each line. Buffer should contain only printable characters. @@ -100,7 +100,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 * */ #define ESP_LOG_BUFFER_CHAR_LEVEL(tag, buffer, buff_len, level) \ - do { if (LOG_LOCAL_LEVEL >= (level)) {esp_log_buffer_char_internal(tag, buffer, buff_len, level);} } while(0) + do { if (ESP_LOG_ENABLED(level)) {esp_log_buffer_char_internal(tag, buffer, buff_len, level);} } while(0) /** * @brief Dump a buffer to the log at specified level. @@ -121,7 +121,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 * @param level Log level. */ #define ESP_LOG_BUFFER_HEXDUMP(tag, buffer, buff_len, level) \ - do { if (LOG_LOCAL_LEVEL >= (level)) {esp_log_buffer_hexdump_internal(tag, buffer, buff_len, level);} } while(0) + do { if (ESP_LOG_ENABLED(level)) {esp_log_buffer_hexdump_internal(tag, buffer, buff_len, level);} } while(0) /** * @brief Log a buffer of hex bytes at Info level @@ -134,7 +134,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 * */ #define ESP_LOG_BUFFER_HEX(tag, buffer, buff_len) \ - do { if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) {ESP_LOG_BUFFER_HEX_LEVEL(tag, buffer, buff_len, ESP_LOG_INFO);} } while(0) + do { if (ESP_LOG_ENABLED(ESP_LOG_INFO)) {ESP_LOG_BUFFER_HEX_LEVEL(tag, buffer, buff_len, ESP_LOG_INFO);} } while(0) /** * @brief Log a buffer of characters at Info level. Buffer should contain only printable characters. @@ -147,7 +147,7 @@ void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16 * */ #define ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len) \ - do { if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) {ESP_LOG_BUFFER_CHAR_LEVEL(tag, buffer, buff_len, ESP_LOG_INFO);} } while(0) + do { if (ESP_LOG_ENABLED(ESP_LOG_INFO)) {ESP_LOG_BUFFER_CHAR_LEVEL(tag, buffer, buff_len, ESP_LOG_INFO);} } while(0) /** @cond */ /** diff --git a/components/log/include/esp_log_level.h b/components/log/include/esp_log_level.h index cc982f9cc4..4a38e1b420 100644 --- a/components/log/include/esp_log_level.h +++ b/components/log/include/esp_log_level.h @@ -44,6 +44,19 @@ ESP_STATIC_ASSERT(ESP_LOG_MAX <= ESP_LOG_LEVEL_MASK, "Log level items of esp_log #endif #endif // LOG_LOCAL_LEVEL +/** + * @brief Check if a specific log level is enabled at compile-time. + * + * This macro checks whether logging for the specified log level is enabled based on the + * current local log level setting (`LOG_LOCAL_LEVEL`). It uses a compile-time check to + * determine if logging for the specified level should be included in the binary, + * helping to exclude logs that are not configured. + * + * @param level log level. + * @return true if the specified log level is enabled, false otherwise. + */ +#define ESP_LOG_ENABLED(level) (LOG_LOCAL_LEVEL >= (level)) + #if NON_OS_BUILD #define _ESP_LOG_ENABLED(log_level) (LOG_LOCAL_LEVEL >= (log_level)) diff --git a/components/log/src/buffer/log_buffers.c b/components/log/src/buffer/log_buffers.c index 88887ed667..f9717aef94 100644 --- a/components/log/src/buffer/log_buffers.c +++ b/components/log/src/buffer/log_buffers.c @@ -6,6 +6,7 @@ #include #include +#include #include "esp_log.h" #include "esp_private/log_util.h" @@ -73,7 +74,7 @@ static void print_buffer(const char *tag, const void *buffer, uint16_t buff_len, do { const char *ptr_line = buffer; - int bytes_cur_line = (buff_len > BYTES_PER_LINE) ? BYTES_PER_LINE : buff_len; + int bytes_cur_line = MIN(BYTES_PER_LINE, buff_len); if (!esp_ptr_byte_accessible(buffer)) { //use memcpy to get around alignment issue memcpy(temp_buffer, buffer, (bytes_cur_line + 3) / 4 * 4); @@ -92,11 +93,13 @@ static void log_buffer_hex_line(uintptr_t orig_buff, const void *ptr_line, char { (void) orig_buff; const unsigned char *ptr = (unsigned char *)ptr_line; - for (int i = 0; i < buff_len; i++) { + int i; + for (i = 0; i < buff_len - 1; i++) { output_str += esp_log_util_cvt_hex(ptr[i], 2, output_str); *output_str++ = ' '; } - *--output_str = 0; + output_str += esp_log_util_cvt_hex(ptr[i], 2, output_str); + *output_str = '\0'; } static void log_buffer_char_line(uintptr_t orig_buff, const void *ptr_line, char *output_str, int buff_len)