Skip to content

Commit

Permalink
Merge dashpay#903: [Log] Handle errors during log message formatting
Browse files Browse the repository at this point in the history
93bc037 [Log] Handle errors during log message formatting (warrows)

Tree-SHA512: 55acc54247ad0980a4ed28ff167d35bae5d437e6484bd5ebb8c9cf948e47751d2e7dc4e9c338d7a6d22301f85448b4e6b8c4f6dbf97d68168939d13bdecb310b
  • Loading branch information
Fuzzbawls committed Jun 7, 2019
2 parents 458b08c + 93bc037 commit 68c81c4
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ int LogPrintStr(const std::string& str);

#define LogPrintf(...) LogPrint(NULL, __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; }

/**
* When we switch to C++11, this can be switched to variadic templates instead
* of this macro-based construction (see tinyformat.h).
Expand All @@ -81,13 +84,25 @@ int LogPrintStr(const std::string& str);
static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
{ \
if (!LogAcceptCategory(category)) return 0; \
return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
try { \
_log_msg_ = tfm::format(format, TINYFORMAT_PASSARGS(n)); \
} catch (std::runtime_error &e) { \
_log_msg_ = "Error \"" + std::string(e.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(format, TINYFORMAT_PASSARGS(n));\
} \
return LogPrintStr(_log_msg_); \
} \
/** Log error and return false */ \
template <TINYFORMAT_ARGTYPES(n)> \
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
LogPrintStr(std::string("ERROR: ") + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
try { \
_log_msg_ = tfm::format(format, TINYFORMAT_PASSARGS(n)); \
} catch (std::runtime_error &e) { \
_log_msg_ = "Error \"" + std::string(e.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(format, TINYFORMAT_PASSARGS(n));\
} \
LogPrintStr(std::string("ERROR: ") + _log_msg_ + "\n"); \
return false; \
}

Expand Down

0 comments on commit 68c81c4

Please sign in to comment.