diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -135,6 +135,10 @@ (void)t; MarkUsed(args...); } +template +std::string FormatStringFromLogArgs(const char *fmt, const Args &... args) { + return fmt; +} #ifdef USE_COVERAGE #define LogPrintf(...) \ @@ -146,16 +150,27 @@ MarkUsed(__VA_ARGS__); \ } while (0) #else -#define LogPrint(category, ...) \ +#define LogPrintf(...) \ do { \ - if (LogAcceptCategory((category))) { \ - LogInstance().LogPrintStr(tfm::format(__VA_ARGS__)); \ + /* Unlikely name to avoid shadowing variables */ \ + std::string _log_msg_; \ + try { \ + _log_msg_ = tfm::format(__VA_ARGS__); \ + } catch (tinyformat::format_error & fmterr) { \ + /** \ + * Original format string will have newline so don't add one here \ + */ \ + _log_msg_ = "Error \"" + std::string(fmterr.what()) + \ + "\" while formatting log message: " + \ + FormatStringFromLogArgs(__VA_ARGS__); \ } \ + LogInstance().LogPrintStr(_log_msg_); \ } while (0) - -#define LogPrintf(...) \ +#define LogPrint(category, ...) \ do { \ - LogInstance().LogPrintStr(tfm::format(__VA_ARGS__)); \ + if (LogAcceptCategory((category))) { \ + LogPrintf(__VA_ARGS__); \ + } \ } while (0) #endif diff --git a/src/tinyformat.h b/src/tinyformat.h --- a/src/tinyformat.h +++ b/src/tinyformat.h @@ -123,7 +123,8 @@ namespace tfm = tinyformat; // Error handling; calls assert() by default. -#define TINYFORMAT_ERROR(reasonString) throw std::runtime_error(reasonString) +#define TINYFORMAT_ERROR(reasonString) \ + throw tinyformat::format_error(reasonString) // Define for C++11 variadic templates which make the code shorter & more // general. If you don't define this, C++11 support is autodetected below. @@ -164,6 +165,11 @@ namespace tinyformat { +class format_error : public std::runtime_error { +public: + format_error(const std::string &what) : std::runtime_error(what) {} +}; + //------------------------------------------------------------------------------ namespace detail { diff --git a/src/util/system.h b/src/util/system.h --- a/src/util/system.h +++ b/src/util/system.h @@ -56,7 +56,7 @@ bool SetupNetworking(); template bool error(const char *fmt, const Args &... args) { - LogPrintf("ERROR: " + tfm::format(fmt, args...) + "\n"); + LogPrintf("ERROR: %s\n", tfm::format(fmt, args...)); return false; }