Skip to content

Commit 00c7e62

Browse files
authored
Merge pull request #15 from esp-arduino-libs/feat/update_cxx_log_trace_guard
feat(log): update CXX log trace guard for compatibility with GNU++20 and below
2 parents d0fc176 + ab6a469 commit 00c7e62

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v0.2.3 - 2025-07-03
44

5+
### Enhancements:
6+
7+
* feat(log): update CXX log trace guard for compatibility with GNU++20 and below
8+
59
### Bugfixes:
610

711
* fix(log): update CXX log trace guard

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ endif()
1717
target_compile_options(${COMPONENT_LIB}
1818
PUBLIC
1919
-Wno-missing-field-initializers
20-
$<$<COMPILE_LANGUAGE:CXX>:-std=gnu++20>
2120
)
2221

2322
if(NOT ESP_PLATFORM)

src/log/esp_utils_log.hpp

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@
1010
#include <type_traits>
1111
#include <utility>
1212
#include <string>
13-
#include <source_location>
13+
14+
// Check C++20 support
15+
#if __cplusplus >= 202002L && defined(__cpp_nontype_template_args)
16+
# define ESP_UTILS_LOG_CXX20_SUPPORT 1
17+
# include <source_location>
18+
#else
19+
# define ESP_UTILS_LOG_CXX20_SUPPORT 0
20+
#endif
21+
1422
#include "esp_utils_log.h"
1523

1624
namespace esp_utils {
1725
namespace detail {
1826

1927
std::string parseFunctionName(const char *func_name);
2028

29+
#if ESP_UTILS_LOG_CXX20_SUPPORT
2130
// Template for string NTTP parameters
2231
template <size_t N>
2332
struct FixedString {
@@ -90,12 +99,72 @@ class log_trace_guard {
9099
}
91100
}
92101

102+
log_trace_guard(const log_trace_guard &) = delete;
103+
log_trace_guard(log_trace_guard &&) = delete;
104+
log_trace_guard &operator=(const log_trace_guard &) = delete;
105+
log_trace_guard &operator=(log_trace_guard &&) = delete;
106+
107+
private:
108+
int _line = 0;
109+
std::string _func_name;
110+
std::string _file_name;
111+
const void *_this_ptr = nullptr;
112+
};
113+
114+
#else
115+
116+
// C++17 fallback implementation without FixedString and source_location
117+
class log_trace_guard {
118+
public:
119+
log_trace_guard(const char *tag, const char *func, const char *file, int line, const void *this_ptr = nullptr)
120+
: _tag(tag), _line(line), _this_ptr(this_ptr)
121+
{
122+
_func_name = std::string(func);
123+
if (_func_name.empty()) {
124+
_func_name = "???";
125+
}
126+
_file_name = std::string(esp_utils_log_extract_file_name(file));
127+
if (_file_name.empty()) {
128+
_file_name = "???";
129+
}
130+
131+
if (_this_ptr) {
132+
ESP_UTILS_LOGD_IMPL_FUNC(
133+
_tag, "[%s:%04d](%s): (@%p) Enter", _file_name.c_str(), _line, _func_name.c_str(), _this_ptr
134+
);
135+
} else {
136+
ESP_UTILS_LOGD_IMPL_FUNC(
137+
_tag, "[%s:%04d](%s): Enter", _file_name.c_str(), _line, _func_name.c_str()
138+
);
139+
}
140+
}
141+
142+
~log_trace_guard()
143+
{
144+
if (_this_ptr) {
145+
ESP_UTILS_LOGD_IMPL_FUNC(
146+
_tag, "[%s:%04d](%s): (@%p) Exit", _file_name.c_str(), _line, _func_name.c_str(), _this_ptr
147+
);
148+
} else {
149+
ESP_UTILS_LOGD_IMPL_FUNC(
150+
_tag, "[%s:%04d](%s): Exit", _file_name.c_str(), _line, _func_name.c_str()
151+
);
152+
}
153+
}
154+
155+
log_trace_guard(const log_trace_guard &) = delete;
156+
log_trace_guard(log_trace_guard &&) = delete;
157+
log_trace_guard &operator=(const log_trace_guard &) = delete;
158+
log_trace_guard &operator=(log_trace_guard &&) = delete;
159+
93160
private:
161+
const char *_tag;
94162
int _line = 0;
95163
std::string _func_name;
96164
std::string _file_name;
97165
const void *_this_ptr = nullptr;
98166
};
167+
#endif
99168

100169
} // namespace detail
101170
} // namespace esp_utils
@@ -105,9 +174,14 @@ class log_trace_guard {
105174
# define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() ESP_UTILS_LOGD("(@%p) Enter", this)
106175
# define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS() ESP_UTILS_LOGD("(@%p) Exit", this)
107176

108-
# define ESP_UTILS_LOG_MAKE_FS(str) []{ constexpr esp_utils::detail::FixedString<sizeof(str)> s(str); return s; }()
109-
# define ESP_UTILS_LOG_TRACE_GUARD() esp_utils::detail::log_trace_guard<ESP_UTILS_LOG_MAKE_FS(ESP_UTILS_LOG_TAG)> _log_trace_guard_{}
110-
# define ESP_UTILS_LOG_TRACE_GUARD_WITH_THIS() esp_utils::detail::log_trace_guard<ESP_UTILS_LOG_MAKE_FS(ESP_UTILS_LOG_TAG)> _log_trace_guard_{this}
177+
# if ESP_UTILS_LOG_CXX20_SUPPORT
178+
# define ESP_UTILS_LOG_MAKE_FS(str) []{ constexpr esp_utils::detail::FixedString<sizeof(str)> s(str); return s; }()
179+
# define ESP_UTILS_LOG_TRACE_GUARD() esp_utils::detail::log_trace_guard<ESP_UTILS_LOG_MAKE_FS(ESP_UTILS_LOG_TAG)> _log_trace_guard_{}
180+
# define ESP_UTILS_LOG_TRACE_GUARD_WITH_THIS() esp_utils::detail::log_trace_guard<ESP_UTILS_LOG_MAKE_FS(ESP_UTILS_LOG_TAG)> _log_trace_guard_{this}
181+
# else
182+
# define ESP_UTILS_LOG_TRACE_GUARD() esp_utils::detail::log_trace_guard _log_trace_guard_{ESP_UTILS_LOG_TAG, __func__, __FILE__, __LINE__}
183+
# define ESP_UTILS_LOG_TRACE_GUARD_WITH_THIS() esp_utils::detail::log_trace_guard _log_trace_guard_{ESP_UTILS_LOG_TAG, __func__, __FILE__, __LINE__, this}
184+
# endif
111185
#else
112186
# define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS()
113187
# define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS()

test_apps/sdkconfig.ci.log_debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
CONFIG_ESP_UTILS_CONF_LOG_IMPL_STDLIB=y
12
CONFIG_ESP_UTILS_CONF_LOG_LEVEL_DEBUG=y
23
CONFIG_ESP_UTILS_CONF_ENABLE_LOG_TRACE=y

0 commit comments

Comments
 (0)