@@ -71,6 +71,9 @@ int LogPrintStr(const std::string& str);
71
71
72
72
#define LogPrintf (...) LogPrint(NULL , __VA_ARGS__)
73
73
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
+
74
77
/* *
75
78
* When we switch to C++11, this can be switched to variadic templates instead
76
79
* of this macro-based construction (see tinyformat.h).
@@ -81,13 +84,25 @@ int LogPrintStr(const std::string& str);
81
84
static inline int LogPrint (const char * category, const char * format, TINYFORMAT_VARARGS(n)) \
82
85
{ \
83
86
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_); \
85
94
} \
86
95
/* * Log error and return false */ \
87
96
template <TINYFORMAT_ARGTYPES(n)> \
88
97
static inline bool error (const char * format, TINYFORMAT_VARARGS(n)) \
89
98
{ \
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 " ); \
91
106
return false ; \
92
107
}
93
108
0 commit comments