2011-05-14 10:31:46 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2019-01-29 15:53:14 +01:00
|
|
|
// Copyright (c) 2014-2019 The Dash Core 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"
|
2016-01-24 05:21:14 +01:00
|
|
|
#include "amount.h"
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2016-06-01 20:23:11 +02:00
|
|
|
#include <atomic>
|
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>
|
2015-04-16 16:20:01 +02:00
|
|
|
#include <boost/signals2/signal.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
|
|
|
|
2016-08-17 09:08:25 +02:00
|
|
|
// Debugging macros
|
|
|
|
|
|
|
|
// Uncomment the following line to enable debugging messages
|
|
|
|
// or enable on a per file basis prior to inclusion of util.h
|
|
|
|
//#define ENABLE_DASH_DEBUG
|
|
|
|
#ifdef ENABLE_DASH_DEBUG
|
|
|
|
#define DBG( x ) x
|
|
|
|
#else
|
|
|
|
#define DBG( x )
|
|
|
|
#endif
|
|
|
|
|
2015-03-18 00:06:58 +01:00
|
|
|
//Dash only features
|
2014-12-09 02:17:57 +01:00
|
|
|
|
2018-01-26 02:11:01 +01:00
|
|
|
extern bool fMasternodeMode;
|
2015-01-18 16:28:16 +01:00
|
|
|
extern bool fLiteMode;
|
2016-06-15 21:13:04 +02:00
|
|
|
extern int nWalletBackups;
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2016-09-16 19:16:22 +02:00
|
|
|
static const bool DEFAULT_LOGTIMEMICROS = false;
|
|
|
|
static const bool DEFAULT_LOGIPS = false;
|
|
|
|
static const bool DEFAULT_LOGTIMESTAMPS = true;
|
2016-03-14 22:04:48 +01:00
|
|
|
static const bool DEFAULT_LOGTHREADNAMES = false;
|
2015-10-23 19:07:36 +02:00
|
|
|
|
2015-04-16 16:20:01 +02:00
|
|
|
/** Signals for translation. */
|
|
|
|
class CTranslationInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Translate a message to the native language of the user. */
|
|
|
|
boost::signals2::signal<std::string (const char* psz)> Translate;
|
|
|
|
};
|
|
|
|
|
2016-12-27 19:11:21 +01:00
|
|
|
extern const std::map<std::string, std::vector<std::string> >& mapMultiArgs;
|
2011-05-14 10:31:46 +02:00
|
|
|
extern bool fDebug;
|
|
|
|
extern bool fPrintToConsole;
|
2013-12-14 13:51:11 +01:00
|
|
|
extern bool fPrintToDebugLog;
|
2016-12-19 12:38:35 +01:00
|
|
|
|
2011-05-14 10:31:46 +02:00
|
|
|
extern bool fLogTimestamps;
|
2015-10-23 19:07:36 +02:00
|
|
|
extern bool fLogTimeMicros;
|
2016-03-14 22:04:48 +01:00
|
|
|
extern bool fLogThreadNames;
|
2014-02-27 02:55:04 +01:00
|
|
|
extern bool fLogIPs;
|
2016-06-01 20:23:11 +02:00
|
|
|
extern std::atomic<bool> fReopenDebugLog;
|
2015-04-16 16:20:01 +02:00
|
|
|
extern CTranslationInterface translationInterface;
|
|
|
|
|
2015-06-27 21:21:41 +02:00
|
|
|
extern const char * const BITCOIN_CONF_FILENAME;
|
|
|
|
extern const char * const BITCOIN_PID_FILENAME;
|
|
|
|
|
2015-04-16 16:20:01 +02:00
|
|
|
/**
|
|
|
|
* Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
|
|
|
|
* If no translation slot is registered, nothing is returned, and simply return the input.
|
|
|
|
*/
|
|
|
|
inline std::string _(const char* psz)
|
|
|
|
{
|
|
|
|
boost::optional<std::string> rv = translationInterface.Translate(psz);
|
|
|
|
return rv ? (*rv) : psz;
|
|
|
|
}
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2014-05-13 12:15:00 +02:00
|
|
|
void SetupEnvironment();
|
2015-09-02 16:18:16 +02:00
|
|
|
bool SetupNetworking();
|
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);
|
|
|
|
|
2018-08-21 17:34:26 +02:00
|
|
|
/** Formats a string without throwing exceptions. Instead, it'll return an error string instead of formatted string. */
|
|
|
|
template<typename... Args>
|
|
|
|
std::string SafeStringFormat(const std::string& fmt, const Args&... args)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return tinyformat::format(fmt, args...);
|
|
|
|
} catch (std::runtime_error& e) {
|
|
|
|
std::string message = tinyformat::format("\n****TINYFORMAT ERROR****\n err=\"%s\"\n fmt=\"%s\"\n", e.what(), fmt);
|
|
|
|
fprintf(stderr, "%s", message.c_str());
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 10:46:54 +01:00
|
|
|
#define LogPrint(category, ...) do { \
|
|
|
|
if (LogAcceptCategory((category))) { \
|
2018-08-21 17:34:26 +02:00
|
|
|
LogPrintStr(SafeStringFormat(__VA_ARGS__)); \
|
2017-01-05 10:46:54 +01:00
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define LogPrintf(...) do { \
|
2018-08-21 17:34:26 +02:00
|
|
|
LogPrintStr(SafeStringFormat(__VA_ARGS__)); \
|
2017-01-05 10:46:54 +01:00
|
|
|
} while(0)
|
2016-05-05 08:19:35 +02:00
|
|
|
|
2016-07-29 12:32:38 +02:00
|
|
|
template<typename... Args>
|
|
|
|
bool error(const char* fmt, const Args&... args)
|
2016-05-05 08:19:35 +02:00
|
|
|
{
|
2018-08-21 17:34:26 +02:00
|
|
|
LogPrintStr("ERROR: " + SafeStringFormat(fmt, args...) + "\n");
|
2014-01-16 15:52:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-09 14:43:06 +02:00
|
|
|
|
2014-12-07 13:29:06 +01:00
|
|
|
void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
|
2012-02-06 18:37:49 +01:00
|
|
|
void ParseParameters(int argc, const char*const argv[]);
|
2016-09-21 14:13:33 +02:00
|
|
|
void FileCommit(FILE *file);
|
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);
|
2018-01-18 17:55:51 +01:00
|
|
|
boost::filesystem::path GetBackupsDir();
|
2015-03-03 16:49:12 +01:00
|
|
|
void ClearDatadirCache();
|
2016-10-03 13:16:40 +02:00
|
|
|
boost::filesystem::path GetConfigFile(const std::string& confPath);
|
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
|
2016-12-27 19:11:21 +01:00
|
|
|
void ReadConfigFile(const std::string& confPath);
|
2012-04-22 16:22:45 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
|
|
|
#endif
|
2015-05-15 21:31:14 +02:00
|
|
|
void OpenDebugLog();
|
2011-05-14 10:31:46 +02:00
|
|
|
void ShrinkDebugFile();
|
2015-05-31 15:36:44 +02:00
|
|
|
void runCommand(const 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
|
|
|
|
}
|
|
|
|
|
2016-12-27 19:11:21 +01:00
|
|
|
/**
|
|
|
|
* Return true if the given argument has been manually set
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @return true if the argument has been set
|
|
|
|
*/
|
|
|
|
bool IsArgSet(const std::string& strArg);
|
|
|
|
|
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
|
|
|
|
2018-02-21 17:32:08 +01:00
|
|
|
// Forces a arg setting
|
2016-12-27 19:11:21 +01:00
|
|
|
void ForceSetArg(const std::string& strArg, const std::string& strValue);
|
2017-09-21 01:18:39 +02:00
|
|
|
void ForceSetMultiArgs(const std::string& strArg, const std::vector<std::string>& values);
|
2016-12-27 19:11:21 +01:00
|
|
|
void ForceRemoveArg(const std::string& strArg);
|
|
|
|
|
2015-02-04 09:11:49 +01:00
|
|
|
/**
|
|
|
|
* Format a string to be used as group of options in help messages
|
|
|
|
*
|
|
|
|
* @param message Group name (e.g. "RPC server options:")
|
|
|
|
* @return the formatted string
|
|
|
|
*/
|
|
|
|
std::string HelpMessageGroup(const std::string& message);
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2013-08-08 11:58:57 +02:00
|
|
|
/**
|
2015-02-04 09:11:49 +01:00
|
|
|
* Format a string to be used as option description in help messages
|
|
|
|
*
|
|
|
|
* @param option Option message (e.g. "-rpcuser=<user>")
|
|
|
|
* @param message Option description (e.g. "Username for JSON-RPC connections")
|
|
|
|
* @return the formatted string
|
2013-08-08 11:58:57 +02:00
|
|
|
*/
|
2015-02-04 09:11:49 +01:00
|
|
|
std::string HelpMessageOpt(const std::string& option, const std::string& message);
|
|
|
|
|
2015-07-01 17:38:15 +02:00
|
|
|
/**
|
|
|
|
* Return the number of physical cores available on the current system.
|
|
|
|
* @note This does not count virtual cores, such as those provided by HyperThreading
|
|
|
|
* when boost is newer than 1.56.
|
|
|
|
*/
|
|
|
|
int GetNumCores();
|
|
|
|
|
2012-06-24 17:03:57 +02:00
|
|
|
void RenameThread(const char* name);
|
2016-03-14 22:04:48 +01:00
|
|
|
std::string GetThreadName();
|
2014-11-17 04:04:01 +01:00
|
|
|
|
2018-05-24 13:57:36 +02:00
|
|
|
namespace ctpl {
|
|
|
|
class thread_pool;
|
|
|
|
}
|
|
|
|
void RenameThreadPool(ctpl::thread_pool& tp, const char* baseName);
|
|
|
|
|
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
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const boost::thread_interrupted&)
|
2013-03-09 02:19:17 +01:00
|
|
|
{
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("%s thread interrupt\n", name);
|
2013-03-09 02:19:17 +01:00
|
|
|
throw;
|
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:20:11 +01:00
|
|
|
std::string CopyrightHolders(const std::string& strPrefix, unsigned int nStartYear, unsigned int nEndYear);
|
2017-07-04 19:31:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts version strings to 4-byte unsigned integer
|
|
|
|
* @param strVersion version in "x.x.x" format (decimal digits only)
|
|
|
|
* @return 4-byte unsigned integer, most significant byte is always 0
|
|
|
|
* Throws std::bad_cast if format doesn\t match.
|
|
|
|
*/
|
|
|
|
uint32_t StringVersionToInt(const std::string& strVersion);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts version as 4-byte unsigned integer to string
|
|
|
|
* @param nVersion 4-byte unsigned integer, most significant byte is always 0
|
|
|
|
* @return version string in "x.x.x" format (last 3 bytes as version parts)
|
|
|
|
* Throws std::bad_cast if format doesn\t match.
|
|
|
|
*/
|
|
|
|
std::string IntVersionToString(uint32_t nVersion);
|
|
|
|
|
|
|
|
|
2017-07-10 16:41:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Copy of the IntVersionToString, that returns "Invalid version" string
|
|
|
|
* instead of throwing std::bad_cast
|
|
|
|
* @param nVersion 4-byte unsigned integer, most significant byte is always 0
|
|
|
|
* @return version string in "x.x.x" format (last 3 bytes as version parts)
|
|
|
|
* or "Invalid version" if can't cast the given value
|
|
|
|
*/
|
|
|
|
std::string SafeIntVersionToString(uint32_t nVersion);
|
|
|
|
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UTIL_H
|