diff --git a/src/util.h b/src/util.h index 468613100e87..98c924328c62 100644 --- a/src/util.h +++ b/src/util.h @@ -94,20 +94,33 @@ bool LogAcceptCategory(const char* category); /** Send a string to the log output */ int LogPrintStr(const std::string &str); +/** Formats a string without throwing exceptions. Instead, it'll return an error string instead of formatted string. */ +template +std::string SafeStringFormat(const std::string& fmt, const Args&... args) +{ + try { + return tinyformat::format(fmt, args...); + } catch (std::runtime_error& e) { + std::string message = tinyformat::format("\n****TINYFORMAT ERROR****\n err=\"%s\"\n fmt=\"%s\"\n", e.what(), fmt); + fprintf(stderr, "%s", message.c_str()); + return message; + } +} + #define LogPrint(category, ...) do { \ if (LogAcceptCategory((category))) { \ - LogPrintStr(tinyformat::format(__VA_ARGS__)); \ + LogPrintStr(SafeStringFormat(__VA_ARGS__)); \ } \ } while(0) #define LogPrintf(...) do { \ - LogPrintStr(tinyformat::format(__VA_ARGS__)); \ + LogPrintStr(SafeStringFormat(__VA_ARGS__)); \ } while(0) template bool error(const char* fmt, const Args&... args) { - LogPrintStr("ERROR: " + tinyformat::format(fmt, args...) + "\n"); + LogPrintStr("ERROR: " + SafeStringFormat(fmt, args...) + "\n"); return false; }