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
1624namespace esp_utils {
1725namespace detail {
1826
1927std::string parseFunctionName (const char *func_name);
2028
29+ #if ESP_UTILS_LOG_CXX20_SUPPORT
2130// Template for string NTTP parameters
2231template <size_t N>
2332struct 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+
93160private:
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 ()
0 commit comments