2011-05-14 10:31:46 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2009-2016 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,
|
2017-05-14 20:18:26 +02:00
|
|
|
* logging, thread wrappers, startup time
|
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
|
|
|
*/
|
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)
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <config/bitcoin-config.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#endif
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <compat.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <sync.h>
|
|
|
|
#include <tinyformat.h>
|
|
|
|
#include <utiltime.h>
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2016-05-30 15:39:37 +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
|
|
|
|
2015-04-16 16:20:01 +02:00
|
|
|
#include <boost/signals2/signal.hpp>
|
2017-11-18 20:35:14 +01:00
|
|
|
#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2017-05-14 20:18:26 +02:00
|
|
|
// Application startup time (used for uptime calculation)
|
|
|
|
int64_t GetStartupTime();
|
|
|
|
|
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 bool fPrintToConsole;
|
2013-12-14 13:51:11 +01:00
|
|
|
extern bool fPrintToDebugLog;
|
2016-11-29 02:00:11 +01:00
|
|
|
|
2011-05-14 10:31:46 +02:00
|
|
|
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-30 15:39:37 +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;
|
|
|
|
|
2016-12-25 21:19:40 +01:00
|
|
|
extern std::atomic<uint32_t> logCategories;
|
|
|
|
|
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
|
|
|
|
2017-04-03 19:39:11 +02:00
|
|
|
struct CLogCategoryActive
|
|
|
|
{
|
|
|
|
std::string category;
|
|
|
|
bool active;
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:19:40 +01:00
|
|
|
namespace BCLog {
|
|
|
|
enum LogFlags : uint32_t {
|
|
|
|
NONE = 0,
|
|
|
|
NET = (1 << 0),
|
|
|
|
TOR = (1 << 1),
|
|
|
|
MEMPOOL = (1 << 2),
|
|
|
|
HTTP = (1 << 3),
|
|
|
|
BENCH = (1 << 4),
|
|
|
|
ZMQ = (1 << 5),
|
|
|
|
DB = (1 << 6),
|
|
|
|
RPC = (1 << 7),
|
|
|
|
ESTIMATEFEE = (1 << 8),
|
|
|
|
ADDRMAN = (1 << 9),
|
|
|
|
SELECTCOINS = (1 << 10),
|
|
|
|
REINDEX = (1 << 11),
|
|
|
|
CMPCTBLOCK = (1 << 12),
|
|
|
|
RAND = (1 << 13),
|
|
|
|
PRUNE = (1 << 14),
|
|
|
|
PROXY = (1 << 15),
|
|
|
|
MEMPOOLREJ = (1 << 16),
|
|
|
|
LIBEVENT = (1 << 17),
|
|
|
|
COINDB = (1 << 18),
|
|
|
|
QT = (1 << 19),
|
|
|
|
LEVELDB = (1 << 20),
|
|
|
|
ALL = ~(uint32_t)0,
|
|
|
|
};
|
|
|
|
}
|
2014-11-17 04:04:01 +01:00
|
|
|
/** Return true if log accepts specified category */
|
2016-12-25 21:19:40 +01:00
|
|
|
static inline bool LogAcceptCategory(uint32_t category)
|
|
|
|
{
|
|
|
|
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
|
|
|
|
}
|
|
|
|
|
2017-04-03 19:39:11 +02:00
|
|
|
/** Returns a string with the log categories. */
|
2016-12-25 21:19:40 +01:00
|
|
|
std::string ListLogCategories();
|
|
|
|
|
2017-04-03 19:39:11 +02:00
|
|
|
/** Returns a vector of the active log categories. */
|
|
|
|
std::vector<CLogCategoryActive> ListActiveLogCategories();
|
|
|
|
|
2016-12-25 21:19:40 +01:00
|
|
|
/** Return true if str parses as a log category and set the flags in f */
|
|
|
|
bool GetLogCategory(uint32_t *f, const std::string *str);
|
|
|
|
|
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);
|
|
|
|
|
2017-03-09 11:37:45 +01:00
|
|
|
/** Get format string from VA_ARGS for error reporting */
|
|
|
|
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
|
|
|
|
|
2017-06-02 23:13:08 +02:00
|
|
|
static inline void MarkUsed() {}
|
|
|
|
template<typename T, typename... Args> static inline void MarkUsed(const T& t, const Args&... args)
|
|
|
|
{
|
|
|
|
(void)t;
|
|
|
|
MarkUsed(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_COVERAGE
|
|
|
|
#define LogPrintf(...) do { MarkUsed(__VA_ARGS__); } while(0)
|
|
|
|
#define LogPrint(category, ...) do { MarkUsed(__VA_ARGS__); } while(0)
|
|
|
|
#else
|
2017-03-09 11:37:45 +01:00
|
|
|
#define LogPrintf(...) do { \
|
|
|
|
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
|
|
|
|
try { \
|
|
|
|
_log_msg_ = tfm::format(__VA_ARGS__); \
|
2017-03-16 17:33:58 +01:00
|
|
|
} catch (tinyformat::format_error &fmterr) { \
|
2017-03-09 11:37:45 +01:00
|
|
|
/* Original format string will have newline so don't add one here */ \
|
2017-03-16 17:33:58 +01:00
|
|
|
_log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
|
2016-12-23 23:22:46 +01:00
|
|
|
} \
|
2017-03-09 11:37:45 +01:00
|
|
|
LogPrintStr(_log_msg_); \
|
2016-12-23 23:22:46 +01:00
|
|
|
} while(0)
|
|
|
|
|
2017-03-09 11:37:45 +01:00
|
|
|
#define LogPrint(category, ...) do { \
|
|
|
|
if (LogAcceptCategory((category))) { \
|
|
|
|
LogPrintf(__VA_ARGS__); \
|
|
|
|
} \
|
2016-12-23 23:22:46 +01:00
|
|
|
} while(0)
|
2017-06-02 23:13:08 +02:00
|
|
|
#endif
|
2016-05-04 14:21:04 +02:00
|
|
|
|
2016-06-27 18:39:25 +02:00
|
|
|
template<typename... Args>
|
|
|
|
bool error(const char* fmt, const Args&... args)
|
2016-05-04 14:21:04 +02:00
|
|
|
{
|
2016-06-27 18:39:25 +02:00
|
|
|
LogPrintStr("ERROR: " + tfm::format(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);
|
2016-09-02 20:50:59 +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);
|
2017-03-01 17:05:50 +01:00
|
|
|
bool RenameOver(fs::path src, fs::path dest);
|
2017-02-22 10:10:00 +01:00
|
|
|
bool TryCreateDirectories(const fs::path& p);
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetDefaultDataDir();
|
|
|
|
const fs::path &GetDataDir(bool fNetSpecific = true);
|
2015-03-03 16:49:12 +01:00
|
|
|
void ClearDatadirCache();
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetConfigFile(const std::string& confPath);
|
2013-07-24 09:30:09 +02:00
|
|
|
#ifndef WIN32
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetPidFile();
|
|
|
|
void CreatePidFile(const fs::path &path, pid_t pid);
|
2013-07-24 09:30:09 +02:00
|
|
|
#endif
|
2012-04-22 16:22:45 +02:00
|
|
|
#ifdef WIN32
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
2012-04-22 16:22:45 +02:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2017-05-06 01:36:47 +02:00
|
|
|
class ArgsManager
|
|
|
|
{
|
|
|
|
protected:
|
2017-07-22 01:00:52 +02:00
|
|
|
mutable CCriticalSection cs_args;
|
2017-05-06 01:36:47 +02:00
|
|
|
std::map<std::string, std::string> mapArgs;
|
2017-07-22 01:00:52 +02:00
|
|
|
std::map<std::string, std::vector<std::string>> mapMultiArgs;
|
2017-05-06 01:36:47 +02:00
|
|
|
public:
|
|
|
|
void ParseParameters(int argc, const char*const argv[]);
|
|
|
|
void ReadConfigFile(const std::string& confPath);
|
2017-07-22 01:00:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a vector of strings of the given argument
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @return command-line arguments
|
|
|
|
*/
|
|
|
|
std::vector<std::string> GetArgs(const std::string& strArg) const;
|
2011-05-14 10:31:46 +02:00
|
|
|
|
2017-06-14 23:13:00 +02: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
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
bool IsArgSet(const std::string& strArg) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return string argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
2017-07-16 12:20:49 +02:00
|
|
|
* @param strDefault (e.g. "1")
|
2017-06-14 23:13:00 +02:00
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return integer argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
2017-07-16 12:20:49 +02:00
|
|
|
* @param nDefault (e.g. 1)
|
2017-06-14 23:13:00 +02:00
|
|
|
* @return command-line argument (0 if invalid number) or default value
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
int64_t GetArg(const std::string& strArg, int64_t nDefault) const;
|
2017-06-14 23:13:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return boolean argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
2017-07-16 12:20:49 +02:00
|
|
|
* @param fDefault (true or false)
|
2017-06-14 23:13:00 +02:00
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
2017-07-22 01:00:52 +02:00
|
|
|
bool GetBoolArg(const std::string& strArg, bool fDefault) const;
|
2017-06-14 23:13:00 +02: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
|
|
|
|
*/
|
|
|
|
bool SoftSetBoolArg(const std::string& strArg, bool fValue);
|
|
|
|
|
|
|
|
// Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
|
|
|
|
// been set. Also called directly in testing.
|
|
|
|
void ForceSetArg(const std::string& strArg, const std::string& strValue);
|
2017-05-06 01:36:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern ArgsManager gArgs;
|
|
|
|
|
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 (...) {
|
2017-08-07 07:36:37 +02:00
|
|
|
PrintExceptionContinue(nullptr, name);
|
2014-02-26 13:23:52 +01:00
|
|
|
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
|
|
|
|
2017-08-15 06:59:08 +02:00
|
|
|
//! Substitute for C++14 std::make_unique.
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
std::unique_ptr<T> MakeUnique(Args&&... args)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UTIL_H
|