util: switch LogPrint and error to variadic templates

This commit is contained in:
Wladimir J. van der Laan 2016-05-04 14:21:04 +02:00
parent 9eaa0afa6e
commit 08d7b563e9

View file

@ -76,40 +76,33 @@ int LogPrintStr(const std::string &str);
#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__) #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
/** template<typename T1, typename... Args>
* When we switch to C++11, this can be switched to variadic templates instead static inline int LogPrint(const char* category, const char* fmt, const T1& v1, const Args&... args)
* of this macro-based construction (see tinyformat.h). {
*/ if(!LogAcceptCategory(category)) return 0; \
#define MAKE_ERROR_AND_LOG_FUNC(n) \ return LogPrintStr(tfm::format(fmt, v1, args...));
/** Print to debug.log if -debug=category switch is given OR category is NULL. */ \ }
template<TINYFORMAT_ARGTYPES(n)> \
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))); \
} \
/** Log error and return false */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
return false; \
}
TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC) template<typename T1, typename... Args>
bool error(const char* fmt, const T1& v1, const Args&... args)
{
LogPrintStr("ERROR: " + tfm::format(fmt, v1, args...) + "\n");
return false;
}
/** /**
* Zero-arg versions of logging and error, these are not covered by * Zero-arg versions of logging and error, these are not covered by
* TINYFORMAT_FOREACH_ARGNUM * the variadic templates above (and don't take format arguments but
* bare strings).
*/ */
static inline int LogPrint(const char* category, const char* format) static inline int LogPrint(const char* category, const char* s)
{ {
if(!LogAcceptCategory(category)) return 0; if(!LogAcceptCategory(category)) return 0;
return LogPrintStr(format); return LogPrintStr(s);
} }
static inline bool error(const char* format) static inline bool error(const char* s)
{ {
LogPrintStr(std::string("ERROR: ") + format + "\n"); LogPrintStr(std::string("ERROR: ") + s + "\n");
return false; return false;
} }