aboutsummaryrefslogtreecommitdiff
path: root/src/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp169
1 files changed, 89 insertions, 80 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 170ea05139..b07c9c1b7e 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1,7 +1,7 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
-// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "util.h"
#include "sync.h"
@@ -25,8 +25,10 @@ namespace boost {
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/foreach.hpp>
+#include <boost/thread.hpp>
#include <openssl/crypto.h>
#include <openssl/rand.h>
+#include <stdarg.h>
#ifdef WIN32
#ifdef _MSC_VER
@@ -47,6 +49,7 @@ namespace boost {
#ifndef NOMINMAX
#define NOMINMAX
#endif
+#include <io.h> /* for _commit */
#include "shlobj.h"
#endif
@@ -67,6 +70,7 @@ bool fTestNet = false;
bool fNoListen = false;
bool fLogTimestamps = false;
CMedianFilter<int64> vTimeOffsets(200,0);
+bool fReopenDebugLog = false;
// Init openssl library multithreading support
static CCriticalSection** ppmutexOpenSSL;
@@ -147,7 +151,7 @@ void RandAddSeedPerfmon()
{
RAND_add(pdata, nSize, nSize/100.0);
memset(pdata, 0, nSize);
- printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
+ printf("RandAddSeed() %d bytes\n", nSize);
}
#endif
}
@@ -172,8 +176,12 @@ int GetRandInt(int nMax)
return GetRand(nMax);
}
-
-
+uint256 GetRandHash()
+{
+ uint256 hash;
+ RAND_bytes((unsigned char*)&hash, sizeof(hash));
+ return hash;
+}
@@ -207,6 +215,16 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
if (fileout)
{
static bool fStartedNewLine = true;
+ static boost::mutex mutexDebugLog;
+ boost::mutex::scoped_lock scoped_lock(mutexDebugLog);
+
+ // reopen the log file, if requested
+ if (fReopenDebugLog) {
+ fReopenDebugLog = false;
+ boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
+ if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
+ setbuf(fileout, NULL); // unbuffered
+ }
// Debug print useful for profiling
if (fLogTimestamps && fStartedNewLine)
@@ -228,68 +246,30 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
{
static CCriticalSection cs_OutputDebugStringF;
- // accumulate a line at a time
+ // accumulate and output a line at a time
{
LOCK(cs_OutputDebugStringF);
- static char pszBuffer[50000];
- static char* pend;
- if (pend == NULL)
- pend = pszBuffer;
+ static std::string buffer;
+
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
- int limit = END(pszBuffer) - pend - 2;
- int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
+ buffer += vstrprintf(pszFormat, arg_ptr);
va_end(arg_ptr);
- if (ret < 0 || ret >= limit)
- {
- pend = END(pszBuffer) - 2;
- *pend++ = '\n';
- }
- else
- pend += ret;
- *pend = '\0';
- char* p1 = pszBuffer;
- char* p2;
- while ((p2 = strchr(p1, '\n')))
+
+ int line_start = 0, line_end;
+ while((line_end = buffer.find('\n', line_start)) != -1)
{
- p2++;
- char c = *p2;
- *p2 = '\0';
- OutputDebugStringA(p1);
- *p2 = c;
- p1 = p2;
+ OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
+ line_start = line_end + 1;
}
- if (p1 != pszBuffer)
- memmove(pszBuffer, p1, pend - p1 + 1);
- pend -= (p1 - pszBuffer);
+ buffer.erase(0, line_start);
}
}
#endif
return ret;
}
-
-// Safer snprintf
-// - prints up to limit-1 characters
-// - output string is always null terminated even if limit reached
-// - return value is the number of characters actually printed
-int my_snprintf(char* buffer, size_t limit, const char* format, ...)
-{
- if (limit == 0)
- return 0;
- va_list arg_ptr;
- va_start(arg_ptr, format);
- int ret = _vsnprintf(buffer, limit, format, arg_ptr);
- va_end(arg_ptr);
- if (ret < 0 || ret >= (int)limit)
- {
- ret = limit - 1;
- buffer[limit-1] = 0;
- }
- return ret;
-}
-
-string real_strprintf(const std::string &format, int dummy, ...)
+string vstrprintf(const std::string &format, va_list ap)
{
char buffer[50000];
char* p = buffer;
@@ -298,7 +278,7 @@ string real_strprintf(const std::string &format, int dummy, ...)
loop
{
va_list arg_ptr;
- va_start(arg_ptr, dummy);
+ va_copy(arg_ptr, ap);
ret = _vsnprintf(p, limit, format.c_str(), arg_ptr);
va_end(arg_ptr);
if (ret >= 0 && ret < limit)
@@ -316,19 +296,22 @@ string real_strprintf(const std::string &format, int dummy, ...)
return str;
}
+string real_strprintf(const std::string &format, int dummy, ...)
+{
+ va_list arg_ptr;
+ va_start(arg_ptr, dummy);
+ string str = vstrprintf(format, arg_ptr);
+ va_end(arg_ptr);
+ return str;
+}
+
bool error(const char *format, ...)
{
- char buffer[50000];
- int limit = sizeof(buffer);
va_list arg_ptr;
va_start(arg_ptr, format);
- int ret = _vsnprintf(buffer, limit, format, arg_ptr);
+ std::string str = vstrprintf(format, arg_ptr);
va_end(arg_ptr);
- if (ret < 0 || ret >= limit)
- {
- buffer[limit - 1] = 0;
- }
- printf("ERROR: %s\n", buffer);
+ printf("ERROR: %s\n", str.c_str());
return false;
}
@@ -756,7 +739,7 @@ bool WildcardMatch(const string& str, const string& mask)
-void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
+static std::string FormatException(std::exception* pex, const char* pszThread)
{
#ifdef WIN32
char pszModule[MAX_PATH] = "";
@@ -765,37 +748,34 @@ void FormatException(char* pszMessage, std::exception* pex, const char* pszThrea
const char* pszModule = "bitcoin";
#endif
if (pex)
- snprintf(pszMessage, 1000,
+ return strprintf(
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
else
- snprintf(pszMessage, 1000,
+ return strprintf(
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
}
void LogException(std::exception* pex, const char* pszThread)
{
- char pszMessage[10000];
- FormatException(pszMessage, pex, pszThread);
- printf("\n%s", pszMessage);
+ std::string message = FormatException(pex, pszThread);
+ printf("\n%s", message.c_str());
}
void PrintException(std::exception* pex, const char* pszThread)
{
- char pszMessage[10000];
- FormatException(pszMessage, pex, pszThread);
- printf("\n\n************************\n%s\n", pszMessage);
- fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
- strMiscWarning = pszMessage;
+ std::string message = FormatException(pex, pszThread);
+ printf("\n\n************************\n%s\n", message.c_str());
+ fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
+ strMiscWarning = message;
throw;
}
void PrintExceptionContinue(std::exception* pex, const char* pszThread)
{
- char pszMessage[10000];
- FormatException(pszMessage, pex, pszThread);
- printf("\n\n************************\n%s\n", pszMessage);
- fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
- strMiscWarning = pszMessage;
+ std::string message = FormatException(pex, pszThread);
+ printf("\n\n************************\n%s\n", message.c_str());
+ fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
+ strMiscWarning = message;
}
boost::filesystem::path GetDefaultDataDir()
@@ -854,7 +834,7 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific)
path = GetDefaultDataDir();
}
if (fNetSpecific && GetBoolArg("-testnet", false))
- path /= "testnet";
+ path /= "testnet3";
fs::create_directory(path);
@@ -910,6 +890,27 @@ void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
}
}
+bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
+{
+#ifdef WIN32
+ return MoveFileExA(src.string().c_str(), dest.string().c_str(),
+ MOVEFILE_REPLACE_EXISTING);
+#else
+ int rc = std::rename(src.string().c_str(), dest.string().c_str());
+ return (rc == 0);
+#endif /* WIN32 */
+}
+
+void FileCommit(FILE *fileout)
+{
+ fflush(fileout); // harmless if redundantly called
+#ifdef WIN32
+ _commit(_fileno(fileout));
+#else
+ fsync(fileno(fileout));
+#endif
+}
+
int GetFilesize(FILE* file)
{
int nSavePos = ftell(file);
@@ -1017,7 +1018,7 @@ void AddTimeData(const CNetAddr& ip, int64 nTime)
string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Bitcoin will not work properly.");
strMiscWarning = strMessage;
printf("*** %s\n", strMessage.c_str());
- ThreadSafeMessageBox(strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION);
+ uiInterface.ThreadSafeMessageBox(strMessage+" ", string("Bitcoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
}
}
}
@@ -1078,3 +1079,11 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
return fs::path("");
}
#endif
+
+void runCommand(std::string strCommand)
+{
+ int nErr = ::system(strCommand.c_str());
+ if (nErr)
+ printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
+}
+