Remove CValidationInterface::UpdatedTransaction
[bitcoinplatinum.git] / src / util.h
blob7998449feebc26e62133bb5865621fe9afe28728
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 "fs.h"
19 #include "tinyformat.h"
20 #include "utiltime.h"
22 #include <atomic>
23 #include <exception>
24 #include <map>
25 #include <stdint.h>
26 #include <string>
27 #include <vector>
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 fPrintToConsole;
46 extern bool fPrintToDebugLog;
48 extern bool fLogTimestamps;
49 extern bool fLogTimeMicros;
50 extern bool fLogIPs;
51 extern std::atomic<bool> fReopenDebugLog;
52 extern CTranslationInterface translationInterface;
54 extern const char * const BITCOIN_CONF_FILENAME;
55 extern const char * const BITCOIN_PID_FILENAME;
57 extern std::atomic<uint32_t> logCategories;
59 /**
60 * Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
61 * If no translation slot is registered, nothing is returned, and simply return the input.
63 inline std::string _(const char* psz)
65 boost::optional<std::string> rv = translationInterface.Translate(psz);
66 return rv ? (*rv) : psz;
69 void SetupEnvironment();
70 bool SetupNetworking();
72 namespace BCLog {
73 enum LogFlags : uint32_t {
74 NONE = 0,
75 NET = (1 << 0),
76 TOR = (1 << 1),
77 MEMPOOL = (1 << 2),
78 HTTP = (1 << 3),
79 BENCH = (1 << 4),
80 ZMQ = (1 << 5),
81 DB = (1 << 6),
82 RPC = (1 << 7),
83 ESTIMATEFEE = (1 << 8),
84 ADDRMAN = (1 << 9),
85 SELECTCOINS = (1 << 10),
86 REINDEX = (1 << 11),
87 CMPCTBLOCK = (1 << 12),
88 RAND = (1 << 13),
89 PRUNE = (1 << 14),
90 PROXY = (1 << 15),
91 MEMPOOLREJ = (1 << 16),
92 LIBEVENT = (1 << 17),
93 COINDB = (1 << 18),
94 QT = (1 << 19),
95 LEVELDB = (1 << 20),
96 ALL = ~(uint32_t)0,
99 /** Return true if log accepts specified category */
100 static inline bool LogAcceptCategory(uint32_t category)
102 return (logCategories.load(std::memory_order_relaxed) & category) != 0;
105 /** Returns a string with the supported log categories */
106 std::string ListLogCategories();
108 /** Return true if str parses as a log category and set the flags in f */
109 bool GetLogCategory(uint32_t *f, const std::string *str);
111 /** Send a string to the log output */
112 int LogPrintStr(const std::string &str);
114 /** Get format string from VA_ARGS for error reporting */
115 template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
117 #define LogPrintf(...) do { \
118 std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
119 try { \
120 _log_msg_ = tfm::format(__VA_ARGS__); \
121 } catch (tinyformat::format_error &fmterr) { \
122 /* Original format string will have newline so don't add one here */ \
123 _log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
125 LogPrintStr(_log_msg_); \
126 } while(0)
128 #define LogPrint(category, ...) do { \
129 if (LogAcceptCategory((category))) { \
130 LogPrintf(__VA_ARGS__); \
132 } while(0)
134 template<typename... Args>
135 bool error(const char* fmt, const Args&... args)
137 LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n");
138 return false;
141 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
142 void ParseParameters(int argc, const char*const argv[]);
143 void FileCommit(FILE *file);
144 bool TruncateFile(FILE *file, unsigned int length);
145 int RaiseFileDescriptorLimit(int nMinFD);
146 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
147 bool RenameOver(fs::path src, fs::path dest);
148 bool TryCreateDirectory(const fs::path& p);
149 fs::path GetDefaultDataDir();
150 const fs::path &GetDataDir(bool fNetSpecific = true);
151 void ClearDatadirCache();
152 fs::path GetConfigFile(const std::string& confPath);
153 #ifndef WIN32
154 fs::path GetPidFile();
155 void CreatePidFile(const fs::path &path, pid_t pid);
156 #endif
157 void ReadConfigFile(const std::string& confPath);
158 #ifdef WIN32
159 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
160 #endif
161 void OpenDebugLog();
162 void ShrinkDebugFile();
163 void runCommand(const std::string& strCommand);
165 inline bool IsSwitchChar(char c)
167 #ifdef WIN32
168 return c == '-' || c == '/';
169 #else
170 return c == '-';
171 #endif
175 * Return true if the given argument has been manually set
177 * @param strArg Argument to get (e.g. "-foo")
178 * @return true if the argument has been set
180 bool IsArgSet(const std::string& strArg);
183 * Return string argument or default value
185 * @param strArg Argument to get (e.g. "-foo")
186 * @param default (e.g. "1")
187 * @return command-line argument or default value
189 std::string GetArg(const std::string& strArg, const std::string& strDefault);
192 * Return integer argument or default value
194 * @param strArg Argument to get (e.g. "-foo")
195 * @param default (e.g. 1)
196 * @return command-line argument (0 if invalid number) or default value
198 int64_t GetArg(const std::string& strArg, int64_t nDefault);
201 * Return boolean argument or default value
203 * @param strArg Argument to get (e.g. "-foo")
204 * @param default (true or false)
205 * @return command-line argument or default value
207 bool GetBoolArg(const std::string& strArg, bool fDefault);
210 * Set an argument if it doesn't already have a value
212 * @param strArg Argument to set (e.g. "-foo")
213 * @param strValue Value (e.g. "1")
214 * @return true if argument gets set, false if it already had a value
216 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
219 * Set a boolean argument if it doesn't already have a value
221 * @param strArg Argument to set (e.g. "-foo")
222 * @param fValue Value (e.g. false)
223 * @return true if argument gets set, false if it already had a value
225 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
227 // Forces a arg setting, used only in testing
228 void ForceSetArg(const std::string& strArg, const std::string& strValue);
231 * Format a string to be used as group of options in help messages
233 * @param message Group name (e.g. "RPC server options:")
234 * @return the formatted string
236 std::string HelpMessageGroup(const std::string& message);
239 * Format a string to be used as option description in help messages
241 * @param option Option message (e.g. "-rpcuser=<user>")
242 * @param message Option description (e.g. "Username for JSON-RPC connections")
243 * @return the formatted string
245 std::string HelpMessageOpt(const std::string& option, const std::string& message);
248 * Return the number of physical cores available on the current system.
249 * @note This does not count virtual cores, such as those provided by HyperThreading
250 * when boost is newer than 1.56.
252 int GetNumCores();
254 void RenameThread(const char* name);
257 * .. and a wrapper that just calls func once
259 template <typename Callable> void TraceThread(const char* name, Callable func)
261 std::string s = strprintf("bitcoin-%s", name);
262 RenameThread(s.c_str());
265 LogPrintf("%s thread start\n", name);
266 func();
267 LogPrintf("%s thread exit\n", name);
269 catch (const boost::thread_interrupted&)
271 LogPrintf("%s thread interrupt\n", name);
272 throw;
274 catch (const std::exception& e) {
275 PrintExceptionContinue(&e, name);
276 throw;
278 catch (...) {
279 PrintExceptionContinue(NULL, name);
280 throw;
284 std::string CopyrightHolders(const std::string& strPrefix);
286 #endif // BITCOIN_UTIL_H