mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Typesafe strprintf/error/LogPrint functions
Switch to tinyformat-based formatting. Tinyformat is a typesafe drop-in replacement for C99 printf functions: https://github.com/c42f/tinyformat
This commit is contained in:
78
src/util.cpp
78
src/util.cpp
@@ -235,12 +235,12 @@ static void DebugPrintInit()
|
||||
mutexDebugLog = new boost::mutex();
|
||||
}
|
||||
|
||||
int LogPrint(const char* category, const char* pszFormat, ...)
|
||||
bool LogAcceptCategory(const char* category)
|
||||
{
|
||||
if (category != NULL)
|
||||
{
|
||||
if (!fDebug)
|
||||
return 0;
|
||||
return false;
|
||||
|
||||
// Give each thread quick access to -debug settings.
|
||||
// This helps prevent issues debugging global destructors,
|
||||
@@ -258,17 +258,18 @@ int LogPrint(const char* category, const char* pszFormat, ...)
|
||||
// if not debugging everything and not debugging specific category, LogPrint does nothing.
|
||||
if (setCategories.count(string("")) == 0 &&
|
||||
setCategories.count(string(category)) == 0)
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int LogPrintStr(const std::string &str)
|
||||
{
|
||||
int ret = 0; // Returns total number of characters written
|
||||
if (fPrintToConsole)
|
||||
{
|
||||
// print to console
|
||||
va_list arg_ptr;
|
||||
va_start(arg_ptr, pszFormat);
|
||||
ret += vprintf(pszFormat, arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
ret = fwrite(str.data(), 1, str.size(), stdout);
|
||||
}
|
||||
else if (fPrintToDebugLog)
|
||||
{
|
||||
@@ -291,76 +292,17 @@ int LogPrint(const char* category, const char* pszFormat, ...)
|
||||
// Debug print useful for profiling
|
||||
if (fLogTimestamps && fStartedNewLine)
|
||||
ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
|
||||
if (pszFormat[strlen(pszFormat) - 1] == '\n')
|
||||
if (!str.empty() && str[str.size()-1] == '\n')
|
||||
fStartedNewLine = true;
|
||||
else
|
||||
fStartedNewLine = false;
|
||||
|
||||
va_list arg_ptr;
|
||||
va_start(arg_ptr, pszFormat);
|
||||
ret += vfprintf(fileout, pszFormat, arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
ret = fwrite(str.data(), 1, str.size(), fileout);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
string vstrprintf(const char *format, va_list ap)
|
||||
{
|
||||
char buffer[50000];
|
||||
char* p = buffer;
|
||||
int limit = sizeof(buffer);
|
||||
int ret;
|
||||
while (true)
|
||||
{
|
||||
va_list arg_ptr;
|
||||
va_copy(arg_ptr, ap);
|
||||
ret = vsnprintf(p, limit, format, arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
if (ret >= 0 && ret < limit)
|
||||
break;
|
||||
if (p != buffer)
|
||||
delete[] p;
|
||||
limit *= 2;
|
||||
p = new char[limit];
|
||||
if (p == NULL)
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
string str(p, p+ret);
|
||||
if (p != buffer)
|
||||
delete[] p;
|
||||
return str;
|
||||
}
|
||||
|
||||
string real_strprintf(const char *format, int dummy, ...)
|
||||
{
|
||||
va_list arg_ptr;
|
||||
va_start(arg_ptr, dummy);
|
||||
string str = vstrprintf(format, arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
return str;
|
||||
}
|
||||
|
||||
string real_strprintf(const std::string &format, int dummy, ...)
|
||||
{
|
||||
va_list arg_ptr;
|
||||
va_start(arg_ptr, dummy);
|
||||
string str = vstrprintf(format.c_str(), arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
return str;
|
||||
}
|
||||
|
||||
bool error(const char *format, ...)
|
||||
{
|
||||
va_list arg_ptr;
|
||||
va_start(arg_ptr, format);
|
||||
std::string str = vstrprintf(format, arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
LogPrintf("ERROR: %s\n", str.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void ParseString(const string& str, char c, vector<string>& v)
|
||||
{
|
||||
if (str.empty())
|
||||
|
||||
Reference in New Issue
Block a user