Changeset View
Changeset View
Standalone View
Standalone View
src/util.h
Show All 10 Lines | |||||
#define BITCOIN_UTIL_H | #define BITCOIN_UTIL_H | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include "config/bitcoin-config.h" | #include "config/bitcoin-config.h" | ||||
#endif | #endif | ||||
#include "compat.h" | #include "compat.h" | ||||
#include "fs.h" | #include "fs.h" | ||||
#include "sync.h" | |||||
#include "tinyformat.h" | #include "tinyformat.h" | ||||
#include "utiltime.h" | #include "utiltime.h" | ||||
#include <atomic> | #include <atomic> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <exception> | #include <exception> | ||||
#include <map> | #include <map> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#include <boost/signals2/signal.hpp> | #include <boost/signals2/signal.hpp> | ||||
#include <boost/thread/exceptions.hpp> | #include <boost/thread/exceptions.hpp> | ||||
static const bool DEFAULT_LOGTIMEMICROS = false; | static const bool DEFAULT_LOGTIMEMICROS = false; | ||||
static const bool DEFAULT_LOGIPS = false; | static const bool DEFAULT_LOGIPS = false; | ||||
static const bool DEFAULT_LOGTIMESTAMPS = true; | static const bool DEFAULT_LOGTIMESTAMPS = true; | ||||
/** Signals for translation. */ | /** Signals for translation. */ | ||||
class CTranslationInterface { | class CTranslationInterface { | ||||
public: | public: | ||||
/** Translate a message to the native language of the user. */ | /** Translate a message to the native language of the user. */ | ||||
boost::signals2::signal<std::string(const char *psz)> Translate; | boost::signals2::signal<std::string(const char *psz)> Translate; | ||||
}; | }; | ||||
extern const std::map<std::string, std::vector<std::string>> &mapMultiArgs; | |||||
extern bool fPrintToConsole; | extern bool fPrintToConsole; | ||||
extern bool fPrintToDebugLog; | extern bool fPrintToDebugLog; | ||||
extern bool fLogTimestamps; | extern bool fLogTimestamps; | ||||
extern bool fLogTimeMicros; | extern bool fLogTimeMicros; | ||||
extern bool fLogIPs; | extern bool fLogIPs; | ||||
extern std::atomic<bool> fReopenDebugLog; | extern std::atomic<bool> fReopenDebugLog; | ||||
extern CTranslationInterface translationInterface; | extern CTranslationInterface translationInterface; | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | #define LogPrintf(...) \ | ||||
} while (0) | } while (0) | ||||
template <typename... Args> bool error(const char *fmt, const Args &... args) { | template <typename... Args> bool error(const char *fmt, const Args &... args) { | ||||
LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n"); | LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n"); | ||||
return false; | return false; | ||||
} | } | ||||
void PrintExceptionContinue(const std::exception *pex, const char *pszThread); | void PrintExceptionContinue(const std::exception *pex, const char *pszThread); | ||||
void ParseParameters(int argc, const char *const argv[]); | |||||
void FileCommit(FILE *file); | void FileCommit(FILE *file); | ||||
bool TruncateFile(FILE *file, unsigned int length); | bool TruncateFile(FILE *file, unsigned int length); | ||||
int RaiseFileDescriptorLimit(int nMinFD); | int RaiseFileDescriptorLimit(int nMinFD); | ||||
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); | void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); | ||||
bool RenameOver(fs::path src, fs::path dest); | bool RenameOver(fs::path src, fs::path dest); | ||||
bool TryCreateDirectories(const fs::path &p); | bool TryCreateDirectories(const fs::path &p); | ||||
fs::path GetDefaultDataDir(); | fs::path GetDefaultDataDir(); | ||||
const fs::path &GetDataDir(bool fNetSpecific = true); | const fs::path &GetDataDir(bool fNetSpecific = true); | ||||
void ClearDatadirCache(); | void ClearDatadirCache(); | ||||
fs::path GetConfigFile(const std::string &confPath); | fs::path GetConfigFile(const std::string &confPath); | ||||
#ifndef WIN32 | #ifndef WIN32 | ||||
fs::path GetPidFile(); | fs::path GetPidFile(); | ||||
void CreatePidFile(const fs::path &path, pid_t pid); | void CreatePidFile(const fs::path &path, pid_t pid); | ||||
#endif | #endif | ||||
void ReadConfigFile(const std::string &confPath); | |||||
#ifdef WIN32 | #ifdef WIN32 | ||||
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); | fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); | ||||
#endif | #endif | ||||
void OpenDebugLog(); | void OpenDebugLog(); | ||||
void ShrinkDebugFile(); | void ShrinkDebugFile(); | ||||
void runCommand(const std::string &strCommand); | void runCommand(const std::string &strCommand); | ||||
inline bool IsSwitchChar(char c) { | inline bool IsSwitchChar(char c) { | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
return c == '-' || c == '/'; | return c == '-' || c == '/'; | ||||
#else | #else | ||||
return c == '-'; | return c == '-'; | ||||
#endif | #endif | ||||
} | } | ||||
class ArgsManager { | |||||
protected: | |||||
CCriticalSection cs_args; | |||||
std::map<std::string, std::string> mapArgs; | |||||
std::map<std::string, std::vector<std::string>> mapMultiArgs; | |||||
public: | |||||
void ParseParameters(int argc, const char *const argv[]); | |||||
void ReadConfigFile(const std::string &confPath); | |||||
std::vector<std::string> GetArgs(const std::string &strArg); | |||||
/** | /** | ||||
* Return true if the given argument has been manually set. | * Return true if the given argument has been manually set. | ||||
* | * | ||||
* @param strArg Argument to get (e.g. "-foo") | * @param strArg Argument to get (e.g. "-foo") | ||||
* @return true if the argument has been set | * @return true if the argument has been set | ||||
*/ | */ | ||||
bool IsArgSet(const std::string &strArg); | bool IsArgSet(const std::string &strArg); | ||||
/** | /** | ||||
* Return string argument or default value. | * Return string argument or default value. | ||||
* | * | ||||
* @param strArg Argument to get (e.g. "-foo") | * @param strArg Argument to get (e.g. "-foo") | ||||
* @param default (e.g. "1") | * @param default (e.g. "1") | ||||
* @return command-line argument or default value | * @return command-line argument or default value | ||||
*/ | */ | ||||
std::string GetArg(const std::string &strArg, const std::string &strDefault); | std::string GetArg(const std::string &strArg, | ||||
const std::string &strDefault); | |||||
/** | /** | ||||
* Return integer argument or default value. | * Return integer argument or default value. | ||||
* | * | ||||
* @param strArg Argument to get (e.g. "-foo") | * @param strArg Argument to get (e.g. "-foo") | ||||
* @param default (e.g. 1) | * @param default (e.g. 1) | ||||
* @return command-line argument (0 if invalid number) or default value | * @return command-line argument (0 if invalid number) or default value | ||||
*/ | */ | ||||
int64_t GetArg(const std::string &strArg, int64_t nDefault); | int64_t GetArg(const std::string &strArg, int64_t nDefault); | ||||
/** | /** | ||||
* Return boolean argument or default value. | * Return boolean argument or default value. | ||||
* | * | ||||
* @param strArg Argument to get (e.g. "-foo") | * @param strArg Argument to get (e.g. "-foo") | ||||
* @param default (true or false) | * @param default (true or false) | ||||
* @return command-line argument or default value | * @return command-line argument or default value | ||||
*/ | */ | ||||
bool GetBoolArg(const std::string &strArg, bool fDefault); | bool GetBoolArg(const std::string &strArg, bool fDefault); | ||||
/** | /** | ||||
* Set an argument if it doesn't already have a value. | * Set an argument if it doesn't already have a value. | ||||
* | * | ||||
* @param strArg Argument to set (e.g. "-foo") | * @param strArg Argument to set (e.g. "-foo") | ||||
* @param strValue Value (e.g. "1") | * @param strValue Value (e.g. "1") | ||||
* @return true if argument gets set, false if it already had a value | * @return true if argument gets set, false if it already had a value | ||||
*/ | */ | ||||
bool SoftSetArg(const std::string &strArg, const std::string &strValue); | bool SoftSetArg(const std::string &strArg, const std::string &strValue); | ||||
/** | /** | ||||
* Set a boolean argument if it doesn't already have a value. | * Set a boolean argument if it doesn't already have a value. | ||||
* | * | ||||
* @param strArg Argument to set (e.g. "-foo") | * @param strArg Argument to set (e.g. "-foo") | ||||
* @param fValue Value (e.g. false) | * @param fValue Value (e.g. false) | ||||
* @return true if argument gets set, false if it already had a value | * @return true if argument gets set, false if it already had a value | ||||
*/ | */ | ||||
bool SoftSetBoolArg(const std::string &strArg, bool fValue); | bool SoftSetBoolArg(const std::string &strArg, bool fValue); | ||||
// Forces a arg setting, used only in testing | // Forces a arg setting, used only in testing | ||||
void ForceSetArg(const std::string &strArg, const std::string &strValue); | void ForceSetArg(const std::string &strArg, const std::string &strValue); | ||||
// Forces a multi arg setting, used only in testing | // Forces a multi arg setting, used only in testing | ||||
void ForceSetMultiArg(const std::string &strArg, const std::string &strValue); | void ForceSetMultiArg(const std::string &strArg, | ||||
const std::string &strValue); | |||||
// Remove an arg setting, used only in testing | // Remove an arg setting, used only in testing | ||||
void ClearArg(const std::string &strArg); | void ClearArg(const std::string &strArg); | ||||
}; | |||||
extern ArgsManager gArgs; | |||||
// wrappers using the global ArgsManager: | |||||
static inline void ParseParameters(int argc, const char *const argv[]) { | |||||
gArgs.ParseParameters(argc, argv); | |||||
} | |||||
static inline void ReadConfigFile(const std::string &confPath) { | |||||
gArgs.ReadConfigFile(confPath); | |||||
} | |||||
static inline bool SoftSetArg(const std::string &strArg, | |||||
const std::string &strValue) { | |||||
return gArgs.SoftSetArg(strArg, strValue); | |||||
} | |||||
static inline void ForceSetArg(const std::string &strArg, | |||||
const std::string &strValue) { | |||||
gArgs.ForceSetArg(strArg, strValue); | |||||
} | |||||
static inline void ForceSetMultiArg(const std::string &strArg, | |||||
const std::string &strValue) { | |||||
gArgs.ForceSetMultiArg(strArg, strValue); | |||||
} | |||||
static inline void ClearArg(const std::string &strArg) { | |||||
gArgs.ClearArg(strArg); | |||||
} | |||||
static inline bool IsArgSet(const std::string &strArg) { | |||||
return gArgs.IsArgSet(strArg); | |||||
} | |||||
static inline std::string GetArg(const std::string &strArg, | |||||
const std::string &strDefault) { | |||||
return gArgs.GetArg(strArg, strDefault); | |||||
} | |||||
static inline int64_t GetArg(const std::string &strArg, int64_t nDefault) { | |||||
return gArgs.GetArg(strArg, nDefault); | |||||
} | |||||
static inline bool GetBoolArg(const std::string &strArg, bool fDefault) { | |||||
return gArgs.GetBoolArg(strArg, fDefault); | |||||
} | |||||
static inline bool SoftSetBoolArg(const std::string &strArg, bool fValue) { | |||||
return gArgs.SoftSetBoolArg(strArg, fValue); | |||||
} | |||||
/** | /** | ||||
* Format a string to be used as group of options in help messages. | * Format a string to be used as group of options in help messages. | ||||
* | * | ||||
* @param message Group name (e.g. "RPC server options:") | * @param message Group name (e.g. "RPC server options:") | ||||
* @return the formatted string | * @return the formatted string | ||||
*/ | */ | ||||
std::string HelpMessageGroup(const std::string &message); | std::string HelpMessageGroup(const std::string &message); | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |