Remove unused var UNLIKELY_PCT from fees.h
[bitcoinplatinum.git] / src / util.h
blobbbb9b5db825deb27833948d4b75e8465b2c21934
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 std::map<std::string, std::string> mapArgs;
45 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
46 extern bool fDebug;
47 extern bool fPrintToConsole;
48 extern bool fPrintToDebugLog;
49 extern bool fServer;
50 extern std::string strMiscWarning;
51 extern bool fLogTimestamps;
52 extern bool fLogTimeMicros;
53 extern bool fLogIPs;
54 extern std::atomic<bool> fReopenDebugLog;
55 extern CTranslationInterface translationInterface;
57 extern const char * const BITCOIN_CONF_FILENAME;
58 extern const char * const BITCOIN_PID_FILENAME;
60 /**
61 * Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
62 * If no translation slot is registered, nothing is returned, and simply return the input.
64 inline std::string _(const char* psz)
66 boost::optional<std::string> rv = translationInterface.Translate(psz);
67 return rv ? (*rv) : psz;
70 void SetupEnvironment();
71 bool SetupNetworking();
73 /** Return true if log accepts specified category */
74 bool LogAcceptCategory(const char* category);
75 /** Send a string to the log output */
76 int LogPrintStr(const std::string &str);
78 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
80 template<typename... Args>
81 static inline int LogPrint(const char* category, const char* fmt, const Args&... args)
83 if(!LogAcceptCategory(category)) return 0; \
84 return LogPrintStr(tfm::format(fmt, args...));
87 template<typename... Args>
88 bool error(const char* fmt, const Args&... args)
90 LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n");
91 return false;
94 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
95 void ParseParameters(int argc, const char*const argv[]);
96 void FileCommit(FILE *file);
97 bool TruncateFile(FILE *file, unsigned int length);
98 int RaiseFileDescriptorLimit(int nMinFD);
99 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
100 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
101 bool TryCreateDirectory(const boost::filesystem::path& p);
102 boost::filesystem::path GetDefaultDataDir();
103 const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
104 void ClearDatadirCache();
105 boost::filesystem::path GetConfigFile(const std::string& confPath);
106 #ifndef WIN32
107 boost::filesystem::path GetPidFile();
108 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
109 #endif
110 void ReadConfigFile(const std::string& confPath, std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
111 #ifdef WIN32
112 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
113 #endif
114 void OpenDebugLog();
115 void ShrinkDebugFile();
116 void runCommand(const std::string& strCommand);
118 inline bool IsSwitchChar(char c)
120 #ifdef WIN32
121 return c == '-' || c == '/';
122 #else
123 return c == '-';
124 #endif
128 * Return string argument or default value
130 * @param strArg Argument to get (e.g. "-foo")
131 * @param default (e.g. "1")
132 * @return command-line argument or default value
134 std::string GetArg(const std::string& strArg, const std::string& strDefault);
137 * Return integer argument or default value
139 * @param strArg Argument to get (e.g. "-foo")
140 * @param default (e.g. 1)
141 * @return command-line argument (0 if invalid number) or default value
143 int64_t GetArg(const std::string& strArg, int64_t nDefault);
146 * Return boolean argument or default value
148 * @param strArg Argument to get (e.g. "-foo")
149 * @param default (true or false)
150 * @return command-line argument or default value
152 bool GetBoolArg(const std::string& strArg, bool fDefault);
155 * Set an argument if it doesn't already have a value
157 * @param strArg Argument to set (e.g. "-foo")
158 * @param strValue Value (e.g. "1")
159 * @return true if argument gets set, false if it already had a value
161 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
164 * Set a boolean argument if it doesn't already have a value
166 * @param strArg Argument to set (e.g. "-foo")
167 * @param fValue Value (e.g. false)
168 * @return true if argument gets set, false if it already had a value
170 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
173 * Format a string to be used as group of options in help messages
175 * @param message Group name (e.g. "RPC server options:")
176 * @return the formatted string
178 std::string HelpMessageGroup(const std::string& message);
181 * Format a string to be used as option description in help messages
183 * @param option Option message (e.g. "-rpcuser=<user>")
184 * @param message Option description (e.g. "Username for JSON-RPC connections")
185 * @return the formatted string
187 std::string HelpMessageOpt(const std::string& option, const std::string& message);
190 * Return the number of physical cores available on the current system.
191 * @note This does not count virtual cores, such as those provided by HyperThreading
192 * when boost is newer than 1.56.
194 int GetNumCores();
196 void RenameThread(const char* name);
199 * .. and a wrapper that just calls func once
201 template <typename Callable> void TraceThread(const char* name, Callable func)
203 std::string s = strprintf("bitcoin-%s", name);
204 RenameThread(s.c_str());
207 LogPrintf("%s thread start\n", name);
208 func();
209 LogPrintf("%s thread exit\n", name);
211 catch (const boost::thread_interrupted&)
213 LogPrintf("%s thread interrupt\n", name);
214 throw;
216 catch (const std::exception& e) {
217 PrintExceptionContinue(&e, name);
218 throw;
220 catch (...) {
221 PrintExceptionContinue(NULL, name);
222 throw;
226 std::string CopyrightHolders(const std::string& strPrefix);
228 #endif // BITCOIN_UTIL_H