@@ -71,6 +71,9 @@ int LogPrintStr(const std::string& str);
7171
7272#define LogPrintf (...) LogPrint(NULL , __VA_ARGS__)
7373
74+ /* * Get format string from VA_ARGS for error reporting */
75+ template <typename ... Args> std::string FormatStringFromLogArgs (const char *fmt, const Args&... args) { return fmt; }
76+
7477/* *
7578 * When we switch to C++11, this can be switched to variadic templates instead
7679 * of this macro-based construction (see tinyformat.h).
@@ -81,13 +84,25 @@ int LogPrintStr(const std::string& str);
8184 static inline int LogPrint (const char * category, const char * format, TINYFORMAT_VARARGS(n)) \
8285 { \
8386 if (!LogAcceptCategory (category)) return 0 ; \
84- return LogPrintStr (tfm::format (format, TINYFORMAT_PASSARGS (n))); \
87+ std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
88+ try { \
89+ _log_msg_ = tfm::format (format, TINYFORMAT_PASSARGS (n)); \
90+ } catch (std::runtime_error &e) { \
91+ _log_msg_ = " Error \" " + std::string (e.what ()) + " \" while formatting log message: " + FormatStringFromLogArgs (format, TINYFORMAT_PASSARGS (n));\
92+ } \
93+ return LogPrintStr (_log_msg_); \
8594 } \
8695 /* * Log error and return false */ \
8796 template <TINYFORMAT_ARGTYPES(n)> \
8897 static inline bool error (const char * format, TINYFORMAT_VARARGS(n)) \
8998 { \
90- LogPrintStr (std::string (" ERROR: " ) + tfm::format (format, TINYFORMAT_PASSARGS (n)) + " \n " ); \
99+ std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
100+ try { \
101+ _log_msg_ = tfm::format (format, TINYFORMAT_PASSARGS (n)); \
102+ } catch (std::runtime_error &e) { \
103+ _log_msg_ = " Error \" " + std::string (e.what ()) + " \" while formatting log message: " + FormatStringFromLogArgs (format, TINYFORMAT_PASSARGS (n));\
104+ } \
105+ LogPrintStr (std::string (" ERROR: " ) + _log_msg_ + " \n " ); \
91106 return false ; \
92107 }
93108
0 commit comments