[Wallet] split the keypool in an internal and external part
[bitcoinplatinum.git] / src / util.h
blob61dc0d366c9477860edfc102e07530c266fc5e57
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 /**
7 * Server/client environment: argument handling, config file parsing,
8 * logging, thread wrappers
9 */
10 #ifndef BITCOIN_UTIL_H
11 #define BITCOIN_UTIL_H
13 #if defined(HAVE_CONFIG_H)
14 #include "config/bitcoin-config.h"
15 #endif
17 #include "compat.h"
18 #include "tinyformat.h"
19 #include "utiltime.h"
21 #include <atomic>
22 #include <exception>
23 #include <map>
24 #include <stdint.h>
25 #include <string>
26 #include <vector>
28 #include <boost/filesystem/path.hpp>
29 #include <boost/signals2/signal.hpp>
30 #include <boost/thread/exceptions.hpp>
32 static const bool DEFAULT_LOGTIMEMICROS = false;
33 static const bool DEFAULT_LOGIPS = false;
34 static const bool DEFAULT_LOGTIMESTAMPS = true;
36 /** Signals for translation. */
37 class CTranslationInterface
39 public:
40 /** Translate a message to the native language of the user. */
41 boost::signals2::signal<std::string (const char* psz)> Translate;
44 extern const std::map<std::string, std::vector<std::string> >& mapMultiArgs;
45 extern bool fDebug;
46 extern bool fPrintToConsole;
47 extern bool fPrintToDebugLog;
49 extern bool fLogTimestamps;
50 extern bool fLogTimeMicros;
51 extern bool fLogIPs;
52 extern std::atomic<bool> fReopenDebugLog;
53 extern CTranslationInterface translationInterface;
55 extern const char * const BITCOIN_CONF_FILENAME;
56 extern const char * const BITCOIN_PID_FILENAME;
58 /**
59 * Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
60 * If no translation slot is registered, nothing is returned, and simply return the input.
62 inline std::string _(const char* psz)
64 boost::optional<std::string> rv = translationInterface.Translate(psz);
65 return rv ? (*rv) : psz;
68 void SetupEnvironment();
69 bool SetupNetworking();
71 /** Return true if log accepts specified category */
72 bool LogAcceptCategory(const char* category);
73 /** Send a string to the log output */
74 int LogPrintStr(const std::string &str);
76 /** Get format string from VA_ARGS for error reporting */
77 template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
79 #define LogPrintf(...) do { \
80 std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
81 try { \
82 _log_msg_ = tfm::format(__VA_ARGS__); \
83 } catch (tinyformat::format_error &fmterr) { \
84 /* Original format string will have newline so don't add one here */ \
85 _log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
86 } \
87 LogPrintStr(_log_msg_); \
88 } while(0)
90 #define LogPrint(category, ...) do { \
91 if (LogAcceptCategory((category))) { \
92 LogPrintf(__VA_ARGS__); \
93 } \
94 } while(0)
96 template<typename... Args>
97 bool error(const char* fmt, const Args&... args)
99 LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n");
100 return false;
103 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
104 void ParseParameters(int argc, const char*const argv[]);
105 void FileCommit(FILE *file);
106 bool TruncateFile(FILE *file, unsigned int length);
107 int RaiseFileDescriptorLimit(int nMinFD);
108 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
109 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
110 bool TryCreateDirectory(const boost::filesystem::path& p);
111 boost::filesystem::path GetDefaultDataDir();
112 const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
113 void ClearDatadirCache();
114 boost::filesystem::path GetConfigFile(const std::string& confPath);
115 #ifndef WIN32
116 boost::filesystem::path GetPidFile();
117 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
118 #endif
119 void ReadConfigFile(const std::string& confPath);
120 #ifdef WIN32
121 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
122 #endif
123 void OpenDebugLog();
124 void ShrinkDebugFile();
125 void runCommand(const std::string& strCommand);
127 inline bool IsSwitchChar(char c)
129 #ifdef WIN32
130 return c == '-' || c == '/';
131 #else
132 return c == '-';
133 #endif
137 * Return true if the given argument has been manually set
139 * @param strArg Argument to get (e.g. "-foo")
140 * @return true if the argument has been set
142 bool IsArgSet(const std::string& strArg);
145 * Return string argument or default value
147 * @param strArg Argument to get (e.g. "-foo")
148 * @param default (e.g. "1")
149 * @return command-line argument or default value
151 std::string GetArg(const std::string& strArg, const std::string& strDefault);
154 * Return integer argument or default value
156 * @param strArg Argument to get (e.g. "-foo")
157 * @param default (e.g. 1)
158 * @return command-line argument (0 if invalid number) or default value
160 int64_t GetArg(const std::string& strArg, int64_t nDefault);
163 * Return boolean argument or default value
165 * @param strArg Argument to get (e.g. "-foo")
166 * @param default (true or false)
167 * @return command-line argument or default value
169 bool GetBoolArg(const std::string& strArg, bool fDefault);
172 * Set an argument if it doesn't already have a value
174 * @param strArg Argument to set (e.g. "-foo")
175 * @param strValue Value (e.g. "1")
176 * @return true if argument gets set, false if it already had a value
178 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
181 * Set a boolean argument if it doesn't already have a value
183 * @param strArg Argument to set (e.g. "-foo")
184 * @param fValue Value (e.g. false)
185 * @return true if argument gets set, false if it already had a value
187 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
189 // Forces a arg setting, used only in testing
190 void ForceSetArg(const std::string& strArg, const std::string& strValue);
193 * Format a string to be used as group of options in help messages
195 * @param message Group name (e.g. "RPC server options:")
196 * @return the formatted string
198 std::string HelpMessageGroup(const std::string& message);
201 * Format a string to be used as option description in help messages
203 * @param option Option message (e.g. "-rpcuser=<user>")
204 * @param message Option description (e.g. "Username for JSON-RPC connections")
205 * @return the formatted string
207 std::string HelpMessageOpt(const std::string& option, const std::string& message);
210 * Return the number of physical cores available on the current system.
211 * @note This does not count virtual cores, such as those provided by HyperThreading
212 * when boost is newer than 1.56.
214 int GetNumCores();
216 void RenameThread(const char* name);
219 * .. and a wrapper that just calls func once
221 template <typename Callable> void TraceThread(const char* name, Callable func)
223 std::string s = strprintf("bitcoin-%s", name);
224 RenameThread(s.c_str());
227 LogPrintf("%s thread start\n", name);
228 func();
229 LogPrintf("%s thread exit\n", name);
231 catch (const boost::thread_interrupted&)
233 LogPrintf("%s thread interrupt\n", name);
234 throw;
236 catch (const std::exception& e) {
237 PrintExceptionContinue(&e, name);
238 throw;
240 catch (...) {
241 PrintExceptionContinue(NULL, name);
242 throw;
246 std::string CopyrightHolders(const std::string& strPrefix);
248 #endif // BITCOIN_UTIL_H