From 76dfa09fe43dc88b9896faf60b491cb1c5814b82 Mon Sep 17 00:00:00 2001 From: Liu Zhongwei Date: Fri, 7 Feb 2025 16:08:12 +0800 Subject: [PATCH] feat(log): disable CXX-related implementations --- CHANGELOG.md | 6 ++ CMakeLists.txt | 7 ++ Kconfig | 4 - esp_utils_conf.h | 9 +-- idf_component.yml | 2 +- library.properties | 2 +- micropython.cmake | 6 ++ src/esp_utils_versions.h | 6 +- src/log/esp_utils_log.h | 164 ++++++++++++++++++++------------------- 9 files changed, 109 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba9e7b3..88c6ad3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # ChangeLog +## v0.2.0 - 2025-02-07 + +### Enhancements: + +* feat(log): disable CXX-related implementations + ## v0.1.2 - 2025-01-23 ### Enhancements: diff --git a/CMakeLists.txt b/CMakeLists.txt index f215245..523056e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,3 +5,10 @@ idf_component_register( SRCS ${SRCS} INCLUDE_DIRS ${SRC_DIR} ) + +target_compile_options(${COMPONENT_LIB} + PUBLIC + -Wno-missing-field-initializers + PRIVATE + $<$:-std=gnu++17> +) diff --git a/Kconfig b/Kconfig index 956c00c..e6d61c6 100644 --- a/Kconfig +++ b/Kconfig @@ -73,10 +73,6 @@ menu "ESP Library Utils Configurations" default n help If enabled, the driver will print trace log messages when enter/exit functions, useful for debugging - - config ESP_UTILS_CONF_LOG_BUFFER_SIZE - int "Buffer size for formatting messages" - default 256 endmenu menu "Memory functions" diff --git a/esp_utils_conf.h b/esp_utils_conf.h index 7091fb0..4ecc251 100644 --- a/esp_utils_conf.h +++ b/esp_utils_conf.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -41,11 +41,6 @@ #endif // ESP_UTILS_CONF_LOG_LEVEL -/** - * @brief Log format buffer size - */ -#define ESP_UTILS_CONF_LOG_BUFFER_SIZE (256) - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////// Memory Configurations ///////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -93,7 +88,7 @@ * 3. Even if the patch version is not consistent, it will not affect normal functionality. */ #define ESP_UTILS_CONF_FILE_VERSION_MAJOR 1 -#define ESP_UTILS_CONF_FILE_VERSION_MINOR 1 +#define ESP_UTILS_CONF_FILE_VERSION_MINOR 2 #define ESP_UTILS_CONF_FILE_VERSION_PATCH 0 // *INDENT-ON* diff --git a/idf_component.yml b/idf_component.yml index 5c842ee..6baac30 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -1,4 +1,4 @@ -version: "0.1.2" +version: "0.2.0" description: esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory. url: https://github.com/esp-arduino-libs/esp-lib-utils repository: https://github.com/esp-arduino-libs/esp-lib-utils.git diff --git a/library.properties b/library.properties index bb47f68..3543d42 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=esp-lib-utils -version=0.1.2 +version=0.2.0 author=espressif maintainer=espressif sentence=esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory. diff --git a/micropython.cmake b/micropython.cmake index 1d57969..095b41e 100644 --- a/micropython.cmake +++ b/micropython.cmake @@ -18,5 +18,11 @@ target_sources(usermod_esp_lib_utils INTERFACE ${SRCS_C} ${SRCS_CXX} ${MPY_C} ${ # Add the current directory as an include directory. target_include_directories(usermod_esp_lib_utils INTERFACE ${SRC_DIR} ${MPY_DIR}) +# Add compile options. Since the target is not created by `idf_component_register()`, we need to add the `ESP_PLATFORM` define manually. +target_compile_options(usermod_esp_lib_utils + INTERFACE + -Wno-missing-field-initializers -DESP_PLATFORM $<$:-std=gnu++17> +) + # Link our INTERFACE library to the usermod target. target_link_libraries(usermod INTERFACE usermod_esp_lib_utils) diff --git a/src/esp_utils_versions.h b/src/esp_utils_versions.h index 8a20559..24d8dcc 100644 --- a/src/esp_utils_versions.h +++ b/src/esp_utils_versions.h @@ -7,10 +7,10 @@ /* Library Version */ #define ESP_UTILS_VERSION_MAJOR 0 -#define ESP_UTILS_VERSION_MINOR 1 -#define ESP_UTILS_VERSION_PATCH 2 +#define ESP_UTILS_VERSION_MINOR 2 +#define ESP_UTILS_VERSION_PATCH 0 /* File `esp_utils_conf.h` */ #define ESP_UTILS_CONF_VERSION_MAJOR 1 -#define ESP_UTILS_CONF_VERSION_MINOR 1 +#define ESP_UTILS_CONF_VERSION_MINOR 2 #define ESP_UTILS_CONF_VERSION_PATCH 0 diff --git a/src/log/esp_utils_log.h b/src/log/esp_utils_log.h index e742a77..4e9573b 100644 --- a/src/log/esp_utils_log.h +++ b/src/log/esp_utils_log.h @@ -11,76 +11,75 @@ #define ESP_UTILS_LOG_TAG "Utils" #endif -#ifdef __cplusplus - -#include -#include -#include -#include -#include - -extern "C" const char *esp_utils_log_extract_file_name(const char *file_path); - -namespace esp_utils { - -/** - * Class to handle logging - */ -class Log { -public: - // Singleton pattern: Get the unique instance of the class - static Log &getInstance() - { - static Log instance; - return instance; - } - - // Templates and conditional compilation: Filter logs by different levels - template - void print(const char *file, int line, const char *func, const char *format, ...) - { - // Logs below the global level will not be compiled - if constexpr (level >= ESP_UTILS_CONF_LOG_LEVEL) { - // Mutex to avoid interleaved log messages - std::lock_guard lock(_mutex); - // Use variadic arguments for formatted output - va_list args; - va_start(args, format); - vsnprintf(_buffer, sizeof(_buffer), format, args); - va_end(args); - // Output log message - printf( - "[%c][%s][%s:%04d](%s): %s\n", logLevelToChar(level), ESP_UTILS_LOG_TAG, - esp_utils_log_extract_file_name(file), line, func, _buffer - ); - } - } - -private: - Log() = default; - - // Convert log level to string - static constexpr char logLevelToChar(int level) - { - switch (level) { - case ESP_UTILS_LOG_LEVEL_DEBUG: return 'D'; - case ESP_UTILS_LOG_LEVEL_INFO: return 'I'; - case ESP_UTILS_LOG_LEVEL_WARNING: return 'W'; - case ESP_UTILS_LOG_LEVEL_ERROR: return 'E'; - default: break; - } - return ' '; - } - - char _buffer[ESP_UTILS_CONF_LOG_BUFFER_SIZE]; - std::mutex _mutex; -}; - -} // namespace esp_utils +// #include +// #include +// #include +// #include +// #include + +// extern "C" const char *esp_utils_log_extract_file_name(const char *file_path); + +// namespace esp_utils { + +// /** +// * Class to handle logging +// */ +// class Log { +// public: +// // Singleton pattern: Get the unique instance of the class +// static Log &getInstance() +// { +// static Log instance; +// return instance; +// } + +// // Templates and conditional compilation: Filter logs by different levels +// template +// void print(const char *file, int line, const char *func, const char *format, ...) +// { +// // Logs below the global level will not be compiled +// if constexpr (level >= ESP_UTILS_CONF_LOG_LEVEL) { +// // Mutex to avoid interleaved log messages +// std::lock_guard lock(_mutex); +// // Use variadic arguments for formatted output +// va_list args; +// va_start(args, format); +// vsnprintf(_buffer, sizeof(_buffer), format, args); +// va_end(args); +// // Output log message +// printf( +// "[%c][%s][%s:%04d](%s): %s\n", logLevelToChar(level), ESP_UTILS_LOG_TAG, +// esp_utils_log_extract_file_name(file), line, func, _buffer +// ); +// } +// } + +// private: +// Log() = default; + +// // Convert log level to string +// static constexpr char logLevelToChar(int level) +// { +// switch (level) { +// case ESP_UTILS_LOG_LEVEL_DEBUG: return 'D'; +// case ESP_UTILS_LOG_LEVEL_INFO: return 'I'; +// case ESP_UTILS_LOG_LEVEL_WARNING: return 'W'; +// case ESP_UTILS_LOG_LEVEL_ERROR: return 'E'; +// default: break; +// } +// return ' '; +// } + +// char _buffer[ESP_UTILS_CONF_LOG_BUFFER_SIZE]; +// std::mutex _mutex; +// }; + +// } // namespace esp_utils /** * Macros to simplify logging calls */ +/* #define ESP_UTILS_LOGD(format, ...) \ esp_utils::Log::getInstance().print(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__) #define ESP_UTILS_LOGI(format, ...) \ @@ -89,25 +88,21 @@ class Log { esp_utils::Log::getInstance().print(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__) #define ESP_UTILS_LOGE(format, ...) \ esp_utils::Log::getInstance().print(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__) - -/** - * Micros to log trace of function calls - */ -#if ESP_UTILS_CONF_ENABLE_LOG_TRACE -#define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() ESP_UTILS_LOGD("(@%p) Enter", this) -#define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS() ESP_UTILS_LOGD("(@%p) Exit", this) -#else -#define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() -#define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS() -#endif - -#else +*/ #include #include +#ifdef __cplusplus +extern "C" { +#endif + const char *esp_utils_log_extract_file_name(const char *file_path); +#ifdef __cplusplus +} +#endif + #define ESP_UTILS_IMPL_LOGD(format, ...) printf("[D][" ESP_UTILS_LOG_TAG "][%s:%04d](%s): " format "\n", \ esp_utils_log_extract_file_name(__FILE__), __LINE__, __func__, ##__VA_ARGS__) #define ESP_UTILS_IMPL_LOGI(format, ...) printf("[I][" ESP_UTILS_LOG_TAG "][%s:%04d](%s): " format "\n", \ @@ -137,8 +132,6 @@ const char *esp_utils_log_extract_file_name(const char *file_path); #define ESP_UTILS_LOGW(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_WARNING, format, ##__VA_ARGS__) #define ESP_UTILS_LOGE(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_ERROR, format, ##__VA_ARGS__) -#endif // __cplusplus - /** * Micros to log trace of function calls */ @@ -149,3 +142,12 @@ const char *esp_utils_log_extract_file_name(const char *file_path); #define ESP_UTILS_LOG_TRACE_ENTER() #define ESP_UTILS_LOG_TRACE_EXIT() #endif +#ifdef __cplusplus +#if ESP_UTILS_CONF_ENABLE_LOG_TRACE +#define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() ESP_UTILS_LOGD("(@%p) Enter", this) +#define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS() ESP_UTILS_LOGD("(@%p) Exit", this) +#else +#define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() +#define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS() +#endif +#endif