2011-05-14 10:31:46 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin 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)
|
2014-06-23 20:04:24 +02:00
|
|
|
#include "config/bitcoin-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>
|
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-05-05 06:40:28 +02:00
|
|
|
#include <signal.h>
|
|
|
|
|
2015-10-23 19:07:36 +02:00
|
|
|
static const bool DEFAULT_LOGTIMEMICROS = false;
|
2015-11-09 19:16:38 +01:00
|
|
|
static const bool DEFAULT_LOGIPS = false;
|
|
|
|
static const bool DEFAULT_LOGTIMESTAMPS = true;
|
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;
|
|
|
|
};
|
|
|
|
|
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;
|
2015-10-23 19:07:36 +02:00
|
|
|
extern bool fLogTimeMicros;
|
2014-02-27 02:55:04 +01:00
|
|
|
extern bool fLogIPs;
|
2016-05-05 06:40:28 +02:00
|
|
|
extern volatile sig_atomic_t 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);
|
|
|
|
|
2013-09-18 12:38:08 +02:00
|
|
|
#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
|
2012-03-18 23:14:03 +01:00
|
|
|
|
2016-05-04 14:21:04 +02:00
|
|
|
template<typename T1, typename... Args>
|
|
|
|
static inline int LogPrint(const char* category, const char* fmt, const T1& v1, const Args&... args)
|
|
|
|
{
|
|
|
|
if(!LogAcceptCategory(category)) return 0; \
|
|
|
|
return LogPrintStr(tfm::format(fmt, v1, args...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T1, typename... Args>
|
|
|
|
bool error(const char* fmt, const T1& v1, const Args&... args)
|
|
|
|
{
|
|
|
|
LogPrintStr("ERROR: " + tfm::format(fmt, v1, args...) + "\n");
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-16 15:52:37 +01:00
|
|
|
|
2014-11-17 04:04:01 +01:00
|
|
|
/**
|
|
|
|
* Zero-arg versions of logging and error, these are not covered by
|
2016-05-04 14:21:04 +02:00
|
|
|
* the variadic templates above (and don't take format arguments but
|
|
|
|
* bare strings).
|
2014-01-16 15:52:37 +01:00
|
|
|
*/
|
2016-05-04 14:21:04 +02:00
|
|
|
static inline int LogPrint(const char* category, const char* s)
|
2014-01-16 15:52:37 +01:00
|
|
|
{
|
|
|
|
if(!LogAcceptCategory(category)) return 0;
|
2016-05-04 14:21:04 +02:00
|
|
|
return LogPrintStr(s);
|
2014-01-16 15:52:37 +01:00
|
|
|
}
|
2016-05-04 14:21:04 +02:00
|
|
|
static inline bool error(const char* s)
|
2014-01-16 15:52:37 +01:00
|
|
|
{
|
2016-05-04 14:21:04 +02:00
|
|
|
LogPrintStr(std::string("ERROR: ") + s + "\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[]);
|
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);
|
2015-03-03 16:49:12 +01:00
|
|
|
void ClearDatadirCache();
|
2012-04-09 23:50:56 +02:00
|
|
|
boost::filesystem::path GetConfigFile();
|
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
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
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);
|
2011-05-14 10:31:46 +02: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)
|
|
|
|
{
|
|
|
|
std::string s = strprintf("bitcoin-%s", name);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 06:38:27 +01:00
|
|
|
std::string CopyrightHolders(const std::string& strPrefix);
|
2015-12-22 13:29:13 +01:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UTIL_H
|