Skip to content

Commit

Permalink
Update util.h
Browse files Browse the repository at this point in the history
  • Loading branch information
vashshawn authored Apr 14, 2024
1 parent e569fc7 commit 2c9d816
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,39 +171,38 @@ extern volatile bool fReopenDebugLog;

void RandAddSeed();
void RandAddSeedPerfmon();
int ATTR_WARN_PRINTF(1,2) OutputDebugStringF(const char* pszFormat, ...);

/*
Rationale for the real_strprintf / strprintf construction:
It is not allowed to use va_start with a pass-by-reference argument.
(C++ standard, 18.7, paragraph 3). Use a dummy argument to work around this, and use a
macro to keep similar semantics.
*/

/** Overload strprintf for char*, so that GCC format type warnings can be given */
std::string ATTR_WARN_PRINTF(1,3) real_strprintf(const char *format, int dummy, ...);
/** Overload strprintf for std::string, to be able to use it with _ (translation).
* This will not support GCC format type warnings (-Wformat) so be careful.
*/
std::string real_LogPrintf(const std::string &format, int dummy, ...);
#define LogPrintf(format, ...) real_LogPrintf(format, 0, __VA_ARGS__)
std::string vstrprintf(const char *format, va_list ap);

bool ATTR_WARN_PRINTF(1,2) error(const char *format, ...);

/* Redefine LogPrintf so that it directs output to debug.log
*
* Do this *after* defining the other printf-like functions, because otherwise the
* __attribute__((format(printf,X,Y))) gets expanded to __attribute__((format(OutputDebugStringF,X,Y)))
* which confuses gcc.
*/
#define LogPrintf OutputDebugStringF

/* Return true if log accepts specified category */
bool LogAcceptCategory(const char* category);
/* Send a string to the log output */
int LogPrintStr(const std::string &str);

#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)

/* When we switch to C++11, this can be switched to variadic templates instead
* of this macro-based construction (see tinyformat.h).
*/
#define MAKE_ERROR_AND_LOG_FUNC(n) \
/* 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)

/* Zero-arg versions of logging and error, these are not covered by
* TINYFORMAT_FOREACH_ARGNUM
*/
static inline int LogPrint(const char* category, const char* format)
{
if(!LogAcceptCategory(category)) return 0;
Expand All @@ -215,6 +214,7 @@ static inline bool error(const char* format)
return false;
}


void PrintException(std::exception* pex, const char* pszThread);
void PrintExceptionContinue(std::exception* pex, const char* pszThread);
void ParseString(const std::string& str, char c, std::vector<std::string>& v);
Expand Down

0 comments on commit 2c9d816

Please sign in to comment.