Skip to content

feat(log): disable CXX-related implementations #9

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

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ idf_component_register(
SRCS ${SRCS}
INCLUDE_DIRS ${SRC_DIR}
)

target_compile_options(${COMPONENT_LIB}
PUBLIC
-Wno-missing-field-initializers
PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17>
)
4 changes: 0 additions & 4 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 2 additions & 7 deletions esp_utils_conf.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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 /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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*
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 6 additions & 0 deletions micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17>
)

# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_esp_lib_utils)
6 changes: 3 additions & 3 deletions src/esp_utils_versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
164 changes: 83 additions & 81 deletions src/log/esp_utils_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,75 @@
#define ESP_UTILS_LOG_TAG "Utils"
#endif

#ifdef __cplusplus

#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <functional>
#include <mutex>

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 <int level>
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<std::mutex> 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 <cstdio>
// #include <cstdarg>
// #include <cstring>
// #include <functional>
// #include <mutex>

// 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 <int level>
// 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<std::mutex> 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<ESP_UTILS_LOG_LEVEL_DEBUG>(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
#define ESP_UTILS_LOGI(format, ...) \
Expand All @@ -89,25 +88,21 @@ class Log {
esp_utils::Log::getInstance().print<ESP_UTILS_LOG_LEVEL_WARNING>(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
#define ESP_UTILS_LOGE(format, ...) \
esp_utils::Log::getInstance().print<ESP_UTILS_LOG_LEVEL_ERROR>(__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 <stdio.h>
#include <string.h>

#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", \
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Loading