Changeset View
Changeset View
Standalone View
Standalone View
src/logging.h
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | |||||
// peer can fill up a user's disk with debug.log entries. | // peer can fill up a user's disk with debug.log entries. | ||||
static inline void MarkUsed() {} | static inline void MarkUsed() {} | ||||
template <typename T, typename... Args> | template <typename T, typename... Args> | ||||
static inline void MarkUsed(const T &t, const Args &... args) { | static inline void MarkUsed(const T &t, const Args &... args) { | ||||
(void)t; | (void)t; | ||||
MarkUsed(args...); | MarkUsed(args...); | ||||
} | } | ||||
template <typename... Args> | |||||
std::string FormatStringFromLogArgs(const char *fmt, const Args &... args) { | |||||
return fmt; | |||||
} | |||||
#ifdef USE_COVERAGE | #ifdef USE_COVERAGE | ||||
#define LogPrintf(...) \ | #define LogPrintf(...) \ | ||||
do { \ | do { \ | ||||
MarkUsed(__VA_ARGS__); \ | MarkUsed(__VA_ARGS__); \ | ||||
} while (0) | } while (0) | ||||
#define LogPrint(category, ...) \ | #define LogPrint(category, ...) \ | ||||
do { \ | do { \ | ||||
MarkUsed(__VA_ARGS__); \ | MarkUsed(__VA_ARGS__); \ | ||||
} while (0) | } while (0) | ||||
#else | #else | ||||
#define LogPrint(category, ...) \ | #define LogPrintf(...) \ | ||||
do { \ | do { \ | ||||
if (LogAcceptCategory((category))) { \ | /* Unlikely name to avoid shadowing variables */ \ | ||||
LogInstance().LogPrintStr(tfm::format(__VA_ARGS__)); \ | 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) | } while (0) | ||||
#define LogPrint(category, ...) \ | |||||
#define LogPrintf(...) \ | |||||
do { \ | do { \ | ||||
LogInstance().LogPrintStr(tfm::format(__VA_ARGS__)); \ | if (LogAcceptCategory((category))) { \ | ||||
LogPrintf(__VA_ARGS__); \ | |||||
} \ | |||||
} while (0) | } while (0) | ||||
#endif | #endif | ||||
/** | /** | ||||
* These are aliases used to explicitly state that the message should not end | * These are aliases used to explicitly state that the message should not end | ||||
* with a newline character. It allows for detecting the missing newlines that | * with a newline character. It allows for detecting the missing newlines that | ||||
* could make the logs hard to read. | * could make the logs hard to read. | ||||
*/ | */ | ||||
#define LogPrintfToBeContinued LogPrintf | #define LogPrintfToBeContinued LogPrintf | ||||
#define LogPrintToBeContinued LogPrint | #define LogPrintToBeContinued LogPrint | ||||
#endif // BITCOIN_LOGGING_H | #endif // BITCOIN_LOGGING_H |