mirror of
https://github.com/kvazar-network/kevacoin.git
synced 2025-02-10 14:14:30 +00:00
Merge #9963: util: Properly handle errors during log message formatting
b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan) 3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan) Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541
This commit is contained in:
commit
8040ae6fc5
@ -123,7 +123,7 @@ namespace tinyformat {}
|
|||||||
namespace tfm = tinyformat;
|
namespace tfm = tinyformat;
|
||||||
|
|
||||||
// Error handling; calls assert() by default.
|
// 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
|
// 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.
|
// general. If you don't define this, C++11 support is autodetected below.
|
||||||
@ -164,6 +164,13 @@ namespace tfm = tinyformat;
|
|||||||
|
|
||||||
namespace tinyformat {
|
namespace tinyformat {
|
||||||
|
|
||||||
|
class format_error: public std::runtime_error
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
format_error(const std::string &what): std::runtime_error(what) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
22
src/util.h
22
src/util.h
@ -73,14 +73,24 @@ bool LogAcceptCategory(const char* category);
|
|||||||
/** Send a string to the log output */
|
/** Send a string to the log output */
|
||||||
int LogPrintStr(const std::string &str);
|
int LogPrintStr(const std::string &str);
|
||||||
|
|
||||||
#define LogPrint(category, ...) do { \
|
/** Get format string from VA_ARGS for error reporting */
|
||||||
if (LogAcceptCategory((category))) { \
|
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
|
||||||
LogPrintStr(tfm::format(__VA_ARGS__)); \
|
|
||||||
} \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
#define LogPrintf(...) do { \
|
#define LogPrintf(...) do { \
|
||||||
LogPrintStr(tfm::format(__VA_ARGS__)); \
|
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
|
||||||
|
try { \
|
||||||
|
_log_msg_ = tfm::format(__VA_ARGS__); \
|
||||||
|
} catch (tinyformat::format_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 LogPrint(category, ...) do { \
|
||||||
|
if (LogAcceptCategory((category))) { \
|
||||||
|
LogPrintf(__VA_ARGS__); \
|
||||||
|
} \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user