]>
Commit | Line | Data |
---|---|---|
1f2e0df8 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
c63a73d1 | 3 | // Distributed under the MIT software license, see the accompanying |
bc909a7a | 4 | // file COPYING or https://www.opensource.org/licenses/mit-license.php . |
51ed9ec9 | 5 | |
ad49c256 WL |
6 | /** |
7 | * Server/client environment: argument handling, config file parsing, | |
8 | * logging, thread wrappers | |
9 | */ | |
1f2e0df8 WL |
10 | #ifndef BITCOIN_UTIL_H |
11 | #define BITCOIN_UTIL_H | |
12 | ||
51ed9ec9 | 13 | #if defined(HAVE_CONFIG_H) |
f3967bcc | 14 | #include "config/bitcoin-config.h" |
51ed9ec9 BD |
15 | #endif |
16 | ||
17 | #include "compat.h" | |
b77dfdc9 | 18 | #include "tinyformat.h" |
611116d4 | 19 | #include "utiltime.h" |
1f2e0df8 | 20 | |
bf673640 | 21 | #include <atomic> |
51ed9ec9 | 22 | #include <exception> |
51ed9ec9 | 23 | #include <map> |
51ed9ec9 BD |
24 | #include <stdint.h> |
25 | #include <string> | |
51ed9ec9 | 26 | #include <vector> |
65ec9eab | 27 | |
ee12c3d6 | 28 | #include <boost/filesystem/path.hpp> |
b74dcb3b | 29 | #include <boost/signals2/signal.hpp> |
ad49c256 | 30 | #include <boost/thread/exceptions.hpp> |
1f2e0df8 | 31 | |
7a4e0e09 CD |
32 | static const bool DEFAULT_LOGTIMEMICROS = false; |
33 | static const bool DEFAULT_LOGIPS = false; | |
34 | static const bool DEFAULT_LOGTIMESTAMPS = true; | |
35 | ||
b74dcb3b JT |
36 | /** Signals for translation. */ |
37 | class CTranslationInterface | |
38 | { | |
39 | public: | |
40 | /** Translate a message to the native language of the user. */ | |
41 | boost::signals2::signal<std::string (const char* psz)> Translate; | |
42 | }; | |
43 | ||
1f2e0df8 WL |
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; | |
9e9056cd | 48 | extern bool fPrintToDebugLog; |
1f2e0df8 | 49 | extern bool fServer; |
1f2e0df8 | 50 | extern std::string strMiscWarning; |
1f2e0df8 | 51 | extern bool fLogTimestamps; |
2e36866f | 52 | extern bool fLogIPs; |
bf673640 | 53 | extern std::atomic<bool> fReopenDebugLog; |
b74dcb3b JT |
54 | extern CTranslationInterface translationInterface; |
55 | ||
aeb089ec JG |
56 | [[noreturn]] extern void new_handler_terminate(); |
57 | ||
b74dcb3b JT |
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. | |
61 | */ | |
62 | inline std::string _(const char* psz) | |
63 | { | |
64 | boost::optional<std::string> rv = translationInterface.Translate(psz); | |
65 | return rv ? (*rv) : psz; | |
66 | } | |
1f2e0df8 | 67 | |
5248ff40 | 68 | void SetupEnvironment(); |
167b6231 | 69 | bool SetupNetworking(); |
e51321fb | 70 | |
c63a73d1 | 71 | /** Return true if log accepts specified category */ |
b77dfdc9 | 72 | bool LogAcceptCategory(const char* category); |
c63a73d1 | 73 | /** Send a string to the log output */ |
b77dfdc9 WL |
74 | int LogPrintStr(const std::string &str); |
75 | ||
881a85a2 | 76 | #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__) |
52d3a481 | 77 | |
c63a73d1 MF |
78 | /** |
79 | * When we switch to C++11, this can be switched to variadic templates instead | |
b77dfdc9 | 80 | * of this macro-based construction (see tinyformat.h). |
b0a90fbb | 81 | */ |
b77dfdc9 | 82 | #define MAKE_ERROR_AND_LOG_FUNC(n) \ |
c63a73d1 | 83 | /** Print to debug.log if -debug=category switch is given OR category is NULL. */ \ |
b77dfdc9 WL |
84 | template<TINYFORMAT_ARGTYPES(n)> \ |
85 | static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \ | |
86 | { \ | |
87 | if(!LogAcceptCategory(category)) return 0; \ | |
88 | return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \ | |
89 | } \ | |
c63a73d1 | 90 | /** Log error and return false */ \ |
b77dfdc9 WL |
91 | template<TINYFORMAT_ARGTYPES(n)> \ |
92 | static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \ | |
93 | { \ | |
2383e488 | 94 | LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \ |
b77dfdc9 WL |
95 | return false; \ |
96 | } | |
97 | ||
98 | TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC) | |
99 | ||
c63a73d1 MF |
100 | /** |
101 | * Zero-arg versions of logging and error, these are not covered by | |
b77dfdc9 WL |
102 | * TINYFORMAT_FOREACH_ARGNUM |
103 | */ | |
104 | static inline int LogPrint(const char* category, const char* format) | |
105 | { | |
106 | if(!LogAcceptCategory(category)) return 0; | |
107 | return LogPrintStr(format); | |
108 | } | |
109 | static inline bool error(const char* format) | |
110 | { | |
2383e488 | 111 | LogPrintStr(std::string("ERROR: ") + format + "\n"); |
b77dfdc9 WL |
112 | return false; |
113 | } | |
b0a90fbb | 114 | |
4f1c3798 SB |
115 | const boost::filesystem::path &ZC_GetParamsDir(); |
116 | ||
27df4123 | 117 | void PrintExceptionContinue(const std::exception *pex, const char* pszThread); |
3ae07355 | 118 | void ParseParameters(int argc, const char*const argv[]); |
768e5d52 | 119 | void FileCommit(FILE *fileout); |
1eb57879 | 120 | bool TruncateFile(FILE *file, unsigned int length); |
ba29a559 | 121 | int RaiseFileDescriptorLimit(int nMinFD); |
bba89aa8 | 122 | void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); |
768e5d52 | 123 | bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest); |
2b7709dc | 124 | bool TryCreateDirectory(const boost::filesystem::path& p); |
ee12c3d6 PW |
125 | boost::filesystem::path GetDefaultDataDir(); |
126 | const boost::filesystem::path &GetDataDir(bool fNetSpecific = true); | |
51598b26 | 127 | void ClearDatadirCache(); |
ee12c3d6 | 128 | boost::filesystem::path GetConfigFile(); |
a034c7eb | 129 | #ifndef WIN32 |
d6712db3 | 130 | boost::filesystem::path GetPidFile(); |
ee12c3d6 | 131 | void CreatePidFile(const boost::filesystem::path &path, pid_t pid); |
a034c7eb | 132 | #endif |
d87f00c4 JG |
133 | class missing_zcash_conf : public std::runtime_error { |
134 | public: | |
135 | missing_zcash_conf() : std::runtime_error("Missing zcash.conf") { } | |
136 | }; | |
f4203de3 | 137 | void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet); |
3e468840 PK |
138 | #ifdef WIN32 |
139 | boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true); | |
140 | #endif | |
597fa4cd | 141 | boost::filesystem::path GetTempPath(); |
8550bcfe | 142 | void OpenDebugLog(); |
1f2e0df8 | 143 | void ShrinkDebugFile(); |
db954a65 | 144 | void runCommand(const std::string& strCommand); |
9064d73b | 145 | const boost::filesystem::path GetExportDir(); |
1f2e0df8 | 146 | |
f0d1accb DH |
147 | /** Returns privacy notice (for -version, -help and metrics screen) */ |
148 | std::string PrivacyInfo(); | |
149 | ||
af021144 S |
150 | /** Returns licensing information (for -version) */ |
151 | std::string LicenseInfo(); | |
152 | ||
1f2e0df8 WL |
153 | inline bool IsSwitchChar(char c) |
154 | { | |
6853e627 | 155 | #ifdef WIN32 |
1f2e0df8 WL |
156 | return c == '-' || c == '/'; |
157 | #else | |
158 | return c == '-'; | |
159 | #endif | |
160 | } | |
161 | ||
3ae07355 GA |
162 | /** |
163 | * Return string argument or default value | |
164 | * | |
165 | * @param strArg Argument to get (e.g. "-foo") | |
166 | * @param default (e.g. "1") | |
167 | * @return command-line argument or default value | |
168 | */ | |
169 | std::string GetArg(const std::string& strArg, const std::string& strDefault); | |
1f2e0df8 | 170 | |
3ae07355 GA |
171 | /** |
172 | * Return integer argument or default value | |
173 | * | |
174 | * @param strArg Argument to get (e.g. "-foo") | |
175 | * @param default (e.g. 1) | |
176 | * @return command-line argument (0 if invalid number) or default value | |
177 | */ | |
51ed9ec9 | 178 | int64_t GetArg(const std::string& strArg, int64_t nDefault); |
1f2e0df8 | 179 | |
3ae07355 GA |
180 | /** |
181 | * Return boolean argument or default value | |
182 | * | |
183 | * @param strArg Argument to get (e.g. "-foo") | |
184 | * @param default (true or false) | |
185 | * @return command-line argument or default value | |
186 | */ | |
3260b4c0 | 187 | bool GetBoolArg(const std::string& strArg, bool fDefault); |
1f2e0df8 | 188 | |
0fcf91ea GA |
189 | /** |
190 | * Set an argument if it doesn't already have a value | |
191 | * | |
192 | * @param strArg Argument to set (e.g. "-foo") | |
193 | * @param strValue Value (e.g. "1") | |
194 | * @return true if argument gets set, false if it already had a value | |
195 | */ | |
196 | bool SoftSetArg(const std::string& strArg, const std::string& strValue); | |
197 | ||
198 | /** | |
199 | * Set a boolean argument if it doesn't already have a value | |
200 | * | |
201 | * @param strArg Argument to set (e.g. "-foo") | |
202 | * @param fValue Value (e.g. false) | |
203 | * @return true if argument gets set, false if it already had a value | |
204 | */ | |
7bf8b7c2 | 205 | bool SoftSetBoolArg(const std::string& strArg, bool fValue); |
1f2e0df8 | 206 | |
1fdb9fa3 LV |
207 | /** |
208 | * Format a string to be used as group of options in help messages | |
209 | * | |
210 | * @param message Group name (e.g. "RPC server options:") | |
211 | * @return the formatted string | |
212 | */ | |
213 | std::string HelpMessageGroup(const std::string& message); | |
214 | ||
215 | /** | |
216 | * Format a string to be used as option description in help messages | |
217 | * | |
218 | * @param option Option message (e.g. "-rpcuser=<user>") | |
219 | * @param message Option description (e.g. "Username for JSON-RPC connections") | |
220 | * @return the formatted string | |
221 | */ | |
222 | std::string HelpMessageOpt(const std::string& option, const std::string& message); | |
223 | ||
da1357e6 WL |
224 | /** |
225 | * Return the number of physical cores available on the current system. | |
226 | * @note This does not count virtual cores, such as those provided by HyperThreading | |
227 | * when boost is newer than 1.56. | |
228 | */ | |
229 | int GetNumCores(); | |
230 | ||
610a8c07 | 231 | void SetThreadPriority(int nPriority); |
96931d6f | 232 | void RenameThread(const char* name); |
1f2e0df8 | 233 | |
c63a73d1 MF |
234 | /** |
235 | * .. and a wrapper that just calls func once | |
236 | */ | |
72f14d26 GA |
237 | template <typename Callable> void TraceThread(const char* name, Callable func) |
238 | { | |
58c4c0bb | 239 | std::string s = strprintf("zcash-%s", name); |
72f14d26 GA |
240 | RenameThread(s.c_str()); |
241 | try | |
242 | { | |
881a85a2 | 243 | LogPrintf("%s thread start\n", name); |
72f14d26 | 244 | func(); |
881a85a2 | 245 | LogPrintf("%s thread exit\n", name); |
72f14d26 | 246 | } |
27df4123 | 247 | catch (const boost::thread_interrupted&) |
72f14d26 | 248 | { |
881a85a2 | 249 | LogPrintf("%s thread interrupt\n", name); |
72f14d26 GA |
250 | throw; |
251 | } | |
27df4123 | 252 | catch (const std::exception& e) { |
44235713 WL |
253 | PrintExceptionContinue(&e, name); |
254 | throw; | |
72f14d26 GA |
255 | } |
256 | catch (...) { | |
44235713 WL |
257 | PrintExceptionContinue(NULL, name); |
258 | throw; | |
72f14d26 GA |
259 | } |
260 | } | |
261 | ||
093303a8 | 262 | #endif // BITCOIN_UTIL_H |