diff options
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 114 |
1 files changed, 55 insertions, 59 deletions
diff --git a/src/util.cpp b/src/util.cpp index 0bd2960233..71994587cf 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2012 The Bitcoin developers +// Copyright (c) 2009-2013 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -8,11 +8,13 @@ #ifdef __linux__ #define _POSIX_C_SOURCE 200112L #endif +#include <algorithm> #include <fcntl.h> #include <sys/stat.h> #include <sys/resource.h> #endif +#include "chainparams.h" #include "util.h" #include "sync.h" #include "version.h" @@ -76,14 +78,11 @@ bool fPrintToConsole = false; bool fPrintToDebugger = false; bool fDaemon = false; bool fServer = false; -bool fCommandLine = false; string strMiscWarning; -bool fTestNet = false; bool fNoListen = false; bool fLogTimestamps = false; CMedianFilter<int64> vTimeOffsets(200,0); volatile bool fReopenDebugLog = false; -bool fCachedPath[2] = {false, false}; // Init OpenSSL library multithreading support static CCriticalSection** ppmutexOpenSSL; @@ -96,8 +95,6 @@ void locking_callback(int mode, int i, const char* file, int line) } } -LockedPageManager LockedPageManager::instance; - // Init class CInit { @@ -166,7 +163,7 @@ void RandAddSeedPerfmon() { RAND_add(pdata, nSize, nSize/100.0); OPENSSL_cleanse(pdata, nSize); - printf("RandAddSeed() %lu bytes\n", nSize); + LogPrint("rand", "RandAddSeed() %lu bytes\n", nSize); } #endif } @@ -198,15 +195,7 @@ uint256 GetRandHash() return hash; } - - - - - - -// -// OutputDebugStringF (aka printf -- there is a #define that we really -// should get rid of one day) has been broken a couple of times now +// LogPrintf() has been broken a couple of times now // by well-meaning people adding mutexes in the most straightforward way. // It breaks because it may be called by global destructors during shutdown. // Since the order of destruction of static/global objects is undefined, @@ -233,8 +222,16 @@ static void DebugPrintInit() mutexDebugLog = new boost::mutex(); } -int OutputDebugStringF(const char* pszFormat, ...) +int LogPrint(const char* category, const char* pszFormat, ...) { + if (category != NULL) + { + if (!fDebug) return 0; + const vector<string>& categories = mapMultiArgs["-debug"]; + if (find(categories.begin(), categories.end(), string(category)) == categories.end()) + return 0; + } + int ret = 0; // Returns total number of characters written if (fPrintToConsole) { @@ -311,7 +308,7 @@ string vstrprintf(const char *format, va_list ap) char* p = buffer; int limit = sizeof(buffer); int ret; - loop + while (true) { va_list arg_ptr; va_copy(arg_ptr, ap); @@ -360,7 +357,7 @@ bool error(const char *format, ...) va_start(arg_ptr, format); std::string str = vstrprintf(format, arg_ptr); va_end(arg_ptr); - printf("ERROR: %s\n", str.c_str()); + LogPrintf("ERROR: %s\n", str.c_str()); return false; } @@ -371,7 +368,7 @@ void ParseString(const string& str, char c, vector<string>& v) return; string::size_type i1 = 0; string::size_type i2; - loop + while (true) { i2 = str.find(c, i1); if (i2 == str.npos) @@ -487,7 +484,7 @@ vector<unsigned char> ParseHex(const char* psz) { // convert hex dump to vector vector<unsigned char> vch; - loop + while (true) { while (isspace(*psz)) psz++; @@ -941,7 +938,7 @@ string DecodeBase32(const string& str) bool WildcardMatch(const char* psz, const char* mask) { - loop + while (true) { switch (*mask) { @@ -994,13 +991,13 @@ static std::string FormatException(std::exception* pex, const char* pszThread) void LogException(std::exception* pex, const char* pszThread) { std::string message = FormatException(pex, pszThread); - printf("\n%s", message.c_str()); + LogPrintf("\n%s", message.c_str()); } void PrintException(std::exception* pex, const char* pszThread) { std::string message = FormatException(pex, pszThread); - printf("\n\n************************\n%s\n", message.c_str()); + LogPrintf("\n\n************************\n%s\n", message.c_str()); fprintf(stderr, "\n\n************************\n%s\n", message.c_str()); strMiscWarning = message; throw; @@ -1009,7 +1006,7 @@ void PrintException(std::exception* pex, const char* pszThread) void PrintExceptionContinue(std::exception* pex, const char* pszThread) { std::string message = FormatException(pex, pszThread); - printf("\n\n************************\n%s\n", message.c_str()); + LogPrintf("\n\n************************\n%s\n", message.c_str()); fprintf(stderr, "\n\n************************\n%s\n", message.c_str()); strMiscWarning = message; } @@ -1043,22 +1040,25 @@ boost::filesystem::path GetDefaultDataDir() #endif } +static boost::filesystem::path pathCached[CChainParams::MAX_NETWORK_TYPES+1]; +static CCriticalSection csPathCached; + const boost::filesystem::path &GetDataDir(bool fNetSpecific) { namespace fs = boost::filesystem; - static fs::path pathCached[2]; - static CCriticalSection csPathCached; + LOCK(csPathCached); + + int nNet = CChainParams::MAX_NETWORK_TYPES; + if (fNetSpecific) nNet = Params().NetworkID(); - fs::path &path = pathCached[fNetSpecific]; + fs::path &path = pathCached[nNet]; - // This can be called during exceptions by printf, so we cache the + // This can be called during exceptions by LogPrintf(), so we cache the // value so we don't have to do memory allocations after that. - if (fCachedPath[fNetSpecific]) + if (!path.empty()) return path; - LOCK(csPathCached); - if (mapArgs.count("-datadir")) { path = fs::system_complete(mapArgs["-datadir"]); if (!fs::is_directory(path)) { @@ -1068,15 +1068,20 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific) } else { path = GetDefaultDataDir(); } - if (fNetSpecific && GetBoolArg("-testnet", false)) - path /= "testnet3"; + if (fNetSpecific) + path /= Params().DataDir(); - fs::create_directory(path); + fs::create_directories(path); - fCachedPath[fNetSpecific] = true; return path; } +void ClearDatadirCache() +{ + std::fill(&pathCached[0], &pathCached[CChainParams::MAX_NETWORK_TYPES+1], + boost::filesystem::path()); +} + boost::filesystem::path GetConfigFile() { boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf")); @@ -1091,9 +1096,6 @@ void ReadConfigFile(map<string, string>& mapSettingsRet, if (!streamConfig.good()) return; // No bitcoin.conf file is OK - // clear path cache after loading config file - fCachedPath[0] = fCachedPath[1] = false; - set<string> setOptions; setOptions.insert("*"); @@ -1109,6 +1111,8 @@ void ReadConfigFile(map<string, string>& mapSettingsRet, } mapMultiSettingsRet[strKey].push_back(it->value[0]); } + // If datadir is changed in .conf file: + ClearDatadirCache(); } boost::filesystem::path GetPidFile() @@ -1118,6 +1122,7 @@ boost::filesystem::path GetPidFile() return pathPidFile; } +#ifndef WIN32 void CreatePidFile(const boost::filesystem::path &path, pid_t pid) { FILE* file = fopen(path.string().c_str(), "w"); @@ -1127,6 +1132,7 @@ void CreatePidFile(const boost::filesystem::path &path, pid_t pid) fclose(file); } } +#endif bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest) { @@ -1147,6 +1153,8 @@ void FileCommit(FILE *fileout) #else #if defined(__linux__) || defined(__NetBSD__) fdatasync(fileno(fileout)); + #elif defined(__APPLE__) && defined(F_FULLFSYNC) + fcntl(fileno(fileout), F_FULLFSYNC, 0); #else fsync(fileno(fileout)); #endif @@ -1311,7 +1319,7 @@ void AddTimeData(const CNetAddr& ip, int64 nTime) // Add data vTimeOffsets.input(nOffsetSample); - printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60); + LogPrintf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60); if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) { int64 nMedian = vTimeOffsets.median(); @@ -1339,17 +1347,17 @@ void AddTimeData(const CNetAddr& ip, int64 nTime) fDone = true; 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()); + LogPrintf("*** %s\n", strMessage.c_str()); uiInterface.ThreadSafeMessageBox(strMessage, "", CClientUIInterface::MSG_WARNING); } } } if (fDebug) { BOOST_FOREACH(int64 n, vSorted) - printf("%+"PRI64d" ", n); - printf("| "); + LogPrintf("%+"PRI64d" ", n); + LogPrintf("| "); } - printf("nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60); + LogPrintf("nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60); } } @@ -1411,7 +1419,7 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) return fs::path(pszPath); } - printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n"); + LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n"); return fs::path(""); } #endif @@ -1431,7 +1439,7 @@ boost::filesystem::path GetTempPath() { path = boost::filesystem::path("/tmp"); #endif if (path.empty() || !boost::filesystem::is_directory(path)) { - printf("GetTempPath(): failed to find temp path\n"); + LogPrintf("GetTempPath(): failed to find temp path\n"); return boost::filesystem::path(""); } return path; @@ -1442,7 +1450,7 @@ 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); + LogPrintf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr); } void RenameThread(const char* name) @@ -1468,15 +1476,3 @@ void RenameThread(const char* name) (void)name; #endif } - -bool NewThread(void(*pfn)(void*), void* parg) -{ - try - { - boost::thread(pfn, parg); // thread detaches when out of scope - } catch(boost::thread_resource_error &e) { - printf("Error creating thread: %s\n", e.what()); - return false; - } - return true; -} |