diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-01-16 15:52:37 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-01-23 16:05:00 +0100 |
commit | b77dfdc9e36e308aa806d63aa3b5628971789d5a (patch) | |
tree | 555894b6364e76c4a5c3d7962f6991de1f5588f8 /src/util.cpp | |
parent | 53e9d3aa44e24fecd2d58984baff3cb4af23c12e (diff) |
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
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 78 |
1 files changed, 10 insertions, 68 deletions
diff --git a/src/util.cpp b/src/util.cpp index 280798f2fb..dd96ae0d70 100644 --- a/src/util.cpp +++ b/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()) |