44
55#include < QString>
66#include < functional>
7+ #include < iostream>
78
89template <>
910struct fmt ::formatter<QString> {
@@ -15,6 +16,18 @@ struct fmt::formatter<QString> {
1516 }
1617};
1718
19+ template <typename ... Args>
20+ std::string safe_format (const std::string& fmt_str, const Args&... args) {
21+ std::string result;
22+ try {
23+ result = fmt::format (fmt_str, args...);
24+ } catch (const std::exception& e) {
25+ // Handle formatting errors here, example:
26+ std::cerr << " Error formatting string: " << fmt_str << " : " << e.what () << std::endl;
27+ }
28+ return result;
29+ }
30+
1831namespace shapeworks {
1932
2033/* *
@@ -105,7 +118,7 @@ class Logging {
105118 void show_progress (double value, const std::string& message);
106119
107120 // ! Log a debug message, use SW_DEBUG macro
108- void log_debug (const std::string& message, const int line, const char * file, const char * function) const ;
121+ void log_debug (const std::string& message, const int line, const char * file, const char * function) const ;
109122
110123 // ! Log a warning message, use SW_WARN macro
111124 void log_warning (const std::string& message, const int line, const char * file) const ;
@@ -156,44 +169,44 @@ class Logging {
156169
157170// ! Log message macro
158171#define SW_LOG (message, ...) \
159- shapeworks::Logging::Instance ().log_message(fmt::format (message, ##__VA_ARGS__), __LINE__, __FILE__)
172+ shapeworks::Logging::Instance ().log_message(safe_format (message, ##__VA_ARGS__), __LINE__, __FILE__);
160173
161174// ! Log warning macro
162175#define SW_WARN (message, ...) \
163- shapeworks::Logging::Instance ().log_warning(fmt::format (message, ##__VA_ARGS__), __LINE__, __FILE__)
176+ shapeworks::Logging::Instance ().log_warning(safe_format (message, ##__VA_ARGS__), __LINE__, __FILE__)
164177
165178// ! Log error macro
166179#define SW_ERROR (message, ...) \
167- shapeworks::Logging::Instance ().log_error(fmt::format (message, ##__VA_ARGS__), __LINE__, __FILE__)
180+ shapeworks::Logging::Instance ().log_error(safe_format (message, ##__VA_ARGS__), __LINE__, __FILE__)
168181
169182// ! Log debug macro
170183#define SW_DEBUG (message, ...) \
171- shapeworks::Logging::Instance ().log_debug(fmt::format (message, ##__VA_ARGS__), __LINE__, __FILE__, __FUNCTION__)
184+ shapeworks::Logging::Instance ().log_debug(safe_format (message, ##__VA_ARGS__), __LINE__, __FILE__, __FUNCTION__)
172185
173186// ! Variable trace macro (e.g. output variable name = <variable value>)
174187#define SW_TRACE (x ) SW_DEBUG(#x " = {}" , x);
175188
176189// ! Log show message macro
177190#define SW_MESSAGE (message, ...) \
178- shapeworks::Logging::Instance ().show_message(fmt::format (message, ##__VA_ARGS__), __LINE__, __FILE__)
191+ shapeworks::Logging::Instance ().show_message(safe_format (message, ##__VA_ARGS__), __LINE__, __FILE__)
179192
180193// ! Don't write to log, but set status (e.g. in the Studio statusbar)
181194#define SW_STATUS (message, ...) \
182- shapeworks::Logging::Instance ().show_status(fmt::format (message, ##__VA_ARGS__), __LINE__, __FILE__)
195+ shapeworks::Logging::Instance ().show_status(safe_format (message, ##__VA_ARGS__), __LINE__, __FILE__)
183196
184197#define SW_PROGRESS (value, message, ...) \
185- shapeworks::Logging::Instance ().show_progress(value, fmt::format (message, ##__VA_ARGS__));
198+ shapeworks::Logging::Instance ().show_progress(value, safe_format (message, ##__VA_ARGS__));
186199
187200// ! Close session macro
188201#define SW_CLOSE_LOG () shapeworks::Logging::Instance().close_log();
189202
190203// ! Log once macro, will only log the message once
191- #define SW_LOG_ONCE (message, ...) \
192- { \
193- static bool logged = false ; \
194- if (!logged) { \
204+ #define SW_LOG_ONCE (message, ...) \
205+ { \
206+ static bool logged = false ; \
207+ if (!logged) { \
195208 SW_LOG (message, ##__VA_ARGS__); \
196- logged = true ; \
197- } \
198- }
209+ logged = true ; \
210+ } \
211+ }
199212} // namespace shapeworks
0 commit comments