diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2017-03-09 10:37:45 +0000 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2017-03-12 07:58:06 +0100 |
commit | 3b092bd9b6b3953d5c3052d57e4827dbd85941fd (patch) | |
tree | 4b7c912f521ae9c7aa52796761b0d8350aaf4236 /src | |
parent | e3e7db829ecd31b0327cff62048ea83ed97f7063 (diff) |
util: Properly handle errors during log message formatting
Instead of having an exception propagate into the program when an
error happens while formatting a log message, just print a message to
the log.
Addresses #9423.
Diffstat (limited to 'src')
-rw-r--r-- | src/util.h | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/util.h b/src/util.h index e27ce121c8..c9d465954c 100644 --- a/src/util.h +++ b/src/util.h @@ -73,14 +73,24 @@ bool LogAcceptCategory(const char* category); /** Send a string to the log output */ int LogPrintStr(const std::string &str); -#define LogPrint(category, ...) do { \ - if (LogAcceptCategory((category))) { \ - LogPrintStr(tfm::format(__VA_ARGS__)); \ +/** Get format string from VA_ARGS for error reporting */ +template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; } + +#define LogPrintf(...) do { \ + std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \ + try { \ + _log_msg_ = tfm::format(__VA_ARGS__); \ + } catch (std::runtime_error &e) { \ + /* Original format string will have newline so don't add one here */ \ + _log_msg_ = "Error \"" + std::string(e.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \ } \ + LogPrintStr(_log_msg_); \ } while(0) -#define LogPrintf(...) do { \ - LogPrintStr(tfm::format(__VA_ARGS__)); \ +#define LogPrint(category, ...) do { \ + if (LogAcceptCategory((category))) { \ + LogPrintf(__VA_ARGS__); \ + } \ } while(0) template<typename... Args> |