2011-05-14 10:31:46 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-02-08 22:50:24 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
2015-03-18 00:06:58 +01:00
|
|
|
// Copyright (c) 2014-2015 The Dash developers
|
2014-11-17 04:04:01 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
/**
|
|
|
|
* Server/client environment: argument handling, config file parsing,
|
|
|
|
* logging, thread wrappers
|
|
|
|
*/
|
2011-05-14 10:31:46 +02:00
|
|
|
#ifndef BITCOIN_UTIL_H
|
|
|
|
#define BITCOIN_UTIL_H
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2015-04-03 00:51:08 +02:00
|
|
|
#include "config/dash-config.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "compat.h"
|
2014-01-16 15:52:37 +01:00
|
|
|
#include "tinyformat.h"
|
2014-09-14 12:43:56 +02:00
|
|
|
#include "utiltime.h"
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-05-24 15:45:08 +02:00
|
|
|
|
2012-04-09 23:50:56 +02:00
|
|
|
#include <boost/filesystem/path.hpp>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include <boost/thread/exceptions.hpp>
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2015-03-18 00:06:58 +01:00
|
|
|
//Dash only features
|
2014-12-09 02:17:57 +01:00
|
|
|
|
|
|
|
extern bool fMasterNode;
|
2015-01-18 16:28:16 +01:00
|
|
|
extern bool fLiteMode;
|
2014-12-09 02:17:57 +01:00
|
|
|
extern int nInstantXDepth;
|
|
|
|
extern int nDarksendRounds;
|
|
|
|
extern int nAnonymizeDarkcoinAmount;
|
|
|
|
extern int nLiquidityProvider;
|
|
|
|
extern bool fEnableDarksend;
|
|
|
|
extern int64_t enforceMasternodePaymentsTime;
|
|
|
|
extern std::string strMasterNodeAddr;
|
|
|
|
extern int nMasternodeMinProtocol;
|
|
|
|
extern int keysLoaded;
|
|
|
|
extern bool fSucessfullyLoaded;
|
|
|
|
extern std::vector<int64_t> darkSendDenominations;
|
2011-05-14 10:31:46 +02:00
|
|
|
|
|
|
|
extern std::map<std::string, std::string> mapArgs;
|
|
|
|
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
|
|
|
extern bool fDebug;
|
|
|
|
extern bool fPrintToConsole;
|
2013-12-14 13:51:11 +01:00
|
|
|
extern bool fPrintToDebugLog;
|
2011-05-14 10:31:46 +02:00
|
|
|
extern bool fServer;
|
|
|
|
extern std::string strMiscWarning;
|
|
|
|
extern bool fLogTimestamps;
|
2014-02-27 02:55:04 +01:00
|
|
|
extern bool fLogIPs;
|
2013-01-01 23:12:30 +01:00
|
|
|
extern volatile bool fReopenDebugLog;
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2014-05-13 12:15:00 +02:00
|
|
|
void SetupEnvironment();
|
2013-09-18 10:03:21 +02:00
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/** Return true if log accepts specified category */
|
2014-01-16 15:52:37 +01:00
|
|
|
bool LogAcceptCategory(const char* category);
|
2014-11-17 04:04:01 +01:00
|
|
|
/** Send a string to the log output */
|
2014-01-16 15:52:37 +01:00
|
|
|
int LogPrintStr(const std::string &str);
|
|
|
|
|
2013-09-18 12:38:08 +02:00
|
|
|
#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
|
2012-03-18 23:14:03 +01:00
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
|
|
|
* When we switch to C++11, this can be switched to variadic templates instead
|
2014-01-16 15:52:37 +01:00
|
|
|
* of this macro-based construction (see tinyformat.h).
|
2012-09-09 14:43:06 +02:00
|
|
|
*/
|
2014-01-16 15:52:37 +01:00
|
|
|
#define MAKE_ERROR_AND_LOG_FUNC(n) \
|
2014-11-17 04:04:01 +01:00
|
|
|
/** Print to debug.log if -debug=category switch is given OR category is NULL. */ \
|
2014-01-16 15:52:37 +01:00
|
|
|
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))); \
|
|
|
|
} \
|
2014-11-17 04:04:01 +01:00
|
|
|
/** Log error and return false */ \
|
2014-01-16 15:52:37 +01:00
|
|
|
template<TINYFORMAT_ARGTYPES(n)> \
|
|
|
|
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
|
|
|
|
{ \
|
2014-01-29 08:10:22 +01:00
|
|
|
LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
|
2014-01-16 15:52:37 +01:00
|
|
|
return false; \
|
|
|
|
}
|
|
|
|
|
|
|
|
TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC)
|
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
|
|
|
* Zero-arg versions of logging and error, these are not covered by
|
2014-01-16 15:52:37 +01:00
|
|
|
* TINYFORMAT_FOREACH_ARGNUM
|
|
|
|
*/
|
|
|
|
static inline int LogPrint(const char* category, const char* format)
|
|
|
|
{
|
|
|
|
if(!LogAcceptCategory(category)) return 0;
|
|
|
|
return LogPrintStr(format);
|
|
|
|
}
|
|
|
|
static inline bool error(const char* format)
|
|
|
|
{
|
2014-01-29 08:10:22 +01:00
|
|
|
LogPrintStr(std::string("ERROR: ") + format + "\n");
|
2014-01-16 15:52:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-09 14:43:06 +02:00
|
|
|
|
2011-05-14 10:31:46 +02:00
|
|
|
void PrintExceptionContinue(std::exception* pex, const char* pszThread);
|
2012-02-06 18:37:49 +01:00
|
|
|
void ParseParameters(int argc, const char*const argv[]);
|
2012-05-12 07:24:27 +02:00
|
|
|
void FileCommit(FILE *fileout);
|
2013-01-30 04:17:33 +01:00
|
|
|
bool TruncateFile(FILE *file, unsigned int length);
|
2013-04-26 00:46:47 +02:00
|
|
|
int RaiseFileDescriptorLimit(int nMinFD);
|
2012-08-16 02:21:28 +02:00
|
|
|
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
|
2012-05-12 07:24:27 +02:00
|
|
|
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
|
2014-03-24 02:14:43 +01:00
|
|
|
bool TryCreateDirectory(const boost::filesystem::path& p);
|
2012-04-09 23:50:56 +02:00
|
|
|
boost::filesystem::path GetDefaultDataDir();
|
|
|
|
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
|
|
|
|
boost::filesystem::path GetConfigFile();
|
2014-12-06 20:41:53 +01:00
|
|
|
boost::filesystem::path GetMasternodeConfigFile();
|
2013-07-24 09:30:09 +02:00
|
|
|
#ifndef WIN32
|
2014-09-20 10:56:25 +02:00
|
|
|
boost::filesystem::path GetPidFile();
|
2012-04-09 23:50:56 +02:00
|
|
|
void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
|
2013-07-24 09:30:09 +02:00
|
|
|
#endif
|
2012-04-22 14:35:22 +02:00
|
|
|
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
|
2012-04-22 16:22:45 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
|
|
|
#endif
|
2012-11-29 00:33:12 +01:00
|
|
|
boost::filesystem::path GetTempPath();
|
2011-05-14 10:31:46 +02:00
|
|
|
void ShrinkDebugFile();
|
2012-05-24 05:10:59 +02:00
|
|
|
void runCommand(std::string strCommand);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
|
|
|
inline bool IsSwitchChar(char c)
|
|
|
|
{
|
2011-10-07 17:02:21 +02:00
|
|
|
#ifdef WIN32
|
2011-05-14 10:31:46 +02:00
|
|
|
return c == '-' || c == '/';
|
|
|
|
#else
|
|
|
|
return c == '-';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-06 18:37:49 +01:00
|
|
|
/**
|
|
|
|
* Return string argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @param default (e.g. "1")
|
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
|
|
|
std::string GetArg(const std::string& strArg, const std::string& strDefault);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2012-02-06 18:37:49 +01:00
|
|
|
/**
|
|
|
|
* Return integer argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @param default (e.g. 1)
|
|
|
|
* @return command-line argument (0 if invalid number) or default value
|
|
|
|
*/
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t GetArg(const std::string& strArg, int64_t nDefault);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2012-02-06 18:37:49 +01:00
|
|
|
/**
|
|
|
|
* Return boolean argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @param default (true or false)
|
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
2013-04-28 17:37:50 +02:00
|
|
|
bool GetBoolArg(const std::string& strArg, bool fDefault);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2012-01-03 16:14:22 +01:00
|
|
|
/**
|
|
|
|
* Set an argument if it doesn't already have a value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to set (e.g. "-foo")
|
|
|
|
* @param strValue Value (e.g. "1")
|
|
|
|
* @return true if argument gets set, false if it already had a value
|
|
|
|
*/
|
|
|
|
bool SoftSetArg(const std::string& strArg, const std::string& strValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a boolean argument if it doesn't already have a value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to set (e.g. "-foo")
|
|
|
|
* @param fValue Value (e.g. false)
|
|
|
|
* @return true if argument gets set, false if it already had a value
|
|
|
|
*/
|
2012-02-06 21:48:00 +01:00
|
|
|
bool SoftSetBoolArg(const std::string& strArg, bool fValue);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2014-08-20 17:43:56 +02:00
|
|
|
void SetThreadPriority(int nPriority);
|
2012-06-24 17:03:57 +02:00
|
|
|
void RenameThread(const char* name);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2013-08-08 11:58:57 +02:00
|
|
|
/**
|
2014-11-17 04:04:01 +01:00
|
|
|
* Standard wrapper for do-something-forever thread functions.
|
|
|
|
* "Forever" really means until the thread is interrupted.
|
|
|
|
* Use it like:
|
|
|
|
* new boost::thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, 900000));
|
|
|
|
* or maybe:
|
|
|
|
* boost::function<void()> f = boost::bind(&FunctionWithArg, argument);
|
|
|
|
* threadGroup.create_thread(boost::bind(&LoopForever<boost::function<void()> >, "nothing", f, milliseconds));
|
2013-08-08 11:58:57 +02:00
|
|
|
*/
|
2013-04-13 07:13:08 +02:00
|
|
|
template <typename Callable> void LoopForever(const char* name, Callable func, int64_t msecs)
|
2013-03-09 02:19:17 +01:00
|
|
|
{
|
2015-03-19 15:15:08 +01:00
|
|
|
std::string s = strprintf("dash-%s", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
RenameThread(s.c_str());
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread start\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
MilliSleep(msecs);
|
2013-06-24 00:23:28 +02:00
|
|
|
func();
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (boost::thread_interrupted)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread stop\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2014-02-26 13:23:52 +01:00
|
|
|
PrintExceptionContinue(&e, name);
|
|
|
|
throw;
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
catch (...) {
|
2014-02-26 13:23:52 +01:00
|
|
|
PrintExceptionContinue(NULL, name);
|
|
|
|
throw;
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 04:04:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* .. and a wrapper that just calls func once
|
|
|
|
*/
|
2013-03-09 02:19:17 +01:00
|
|
|
template <typename Callable> void TraceThread(const char* name, Callable func)
|
|
|
|
{
|
2015-03-19 15:15:08 +01:00
|
|
|
std::string s = strprintf("dash-%s", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
RenameThread(s.c_str());
|
|
|
|
try
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread start\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
func();
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread exit\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
catch (boost::thread_interrupted)
|
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread interrupt\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2014-02-26 13:23:52 +01:00
|
|
|
PrintExceptionContinue(&e, name);
|
|
|
|
throw;
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
catch (...) {
|
2014-02-26 13:23:52 +01:00
|
|
|
PrintExceptionContinue(NULL, name);
|
|
|
|
throw;
|
2013-03-09 02:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UTIL_H
|