]>
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 |
3a25a2b9 | 4 | // file COPYING or http://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 | ||
56 | /** | |
57 | * Translation function: Call Translate signal on UI interface, which returns a boost::optional result. | |
58 | * If no translation slot is registered, nothing is returned, and simply return the input. | |
59 | */ | |
60 | inline std::string _(const char* psz) | |
61 | { | |
62 | boost::optional<std::string> rv = translationInterface.Translate(psz); | |
63 | return rv ? (*rv) : psz; | |
64 | } | |
1f2e0df8 | 65 | |
5248ff40 | 66 | void SetupEnvironment(); |
e51321fb | 67 | |
c63a73d1 | 68 | /** Return true if log accepts specified category */ |
b77dfdc9 | 69 | bool LogAcceptCategory(const char* category); |
c63a73d1 | 70 | /** Send a string to the log output */ |
b77dfdc9 WL |
71 | int LogPrintStr(const std::string &str); |
72 | ||
881a85a2 | 73 | #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__) |
52d3a481 | 74 | |
c63a73d1 MF |
75 | /** |
76 | * When we switch to C++11, this can be switched to variadic templates instead | |
b77dfdc9 | 77 | * of this macro-based construction (see tinyformat.h). |
b0a90fbb | 78 | */ |
b77dfdc9 | 79 | #define MAKE_ERROR_AND_LOG_FUNC(n) \ |
c63a73d1 | 80 | /** Print to debug.log if -debug=category switch is given OR category is NULL. */ \ |
b77dfdc9 WL |
81 | template<TINYFORMAT_ARGTYPES(n)> \ |
82 | static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \ | |
83 | { \ | |
84 | if(!LogAcceptCategory(category)) return 0; \ | |
85 | return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \ | |
86 | } \ | |
c63a73d1 | 87 | /** Log error and return false */ \ |
b77dfdc9 WL |
88 | template<TINYFORMAT_ARGTYPES(n)> \ |
89 | static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \ | |
90 | { \ | |
2383e488 | 91 | LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \ |
b77dfdc9 WL |
92 | return false; \ |
93 | } | |
94 | ||
95 | TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC) | |
96 | ||
c63a73d1 MF |
97 | /** |
98 | * Zero-arg versions of logging and error, these are not covered by | |
b77dfdc9 WL |
99 | * TINYFORMAT_FOREACH_ARGNUM |
100 | */ | |
101 | static inline int LogPrint(const char* category, const char* format) | |
102 | { | |
103 | if(!LogAcceptCategory(category)) return 0; | |
104 | return LogPrintStr(format); | |
105 | } | |
106 | static inline bool error(const char* format) | |
107 | { | |
2383e488 | 108 | LogPrintStr(std::string("ERROR: ") + format + "\n"); |
b77dfdc9 WL |
109 | return false; |
110 | } | |
b0a90fbb | 111 | |
4f1c3798 SB |
112 | const boost::filesystem::path &ZC_GetParamsDir(); |
113 | ||
27df4123 | 114 | void PrintExceptionContinue(const std::exception *pex, const char* pszThread); |
3ae07355 | 115 | void ParseParameters(int argc, const char*const argv[]); |
768e5d52 | 116 | void FileCommit(FILE *fileout); |
1eb57879 | 117 | bool TruncateFile(FILE *file, unsigned int length); |
ba29a559 | 118 | int RaiseFileDescriptorLimit(int nMinFD); |
bba89aa8 | 119 | void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); |
768e5d52 | 120 | bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest); |
2b7709dc | 121 | bool TryCreateDirectory(const boost::filesystem::path& p); |
ee12c3d6 PW |
122 | boost::filesystem::path GetDefaultDataDir(); |
123 | const boost::filesystem::path &GetDataDir(bool fNetSpecific = true); | |
51598b26 | 124 | void ClearDatadirCache(); |
ee12c3d6 | 125 | boost::filesystem::path GetConfigFile(); |
a034c7eb | 126 | #ifndef WIN32 |
d6712db3 | 127 | boost::filesystem::path GetPidFile(); |
ee12c3d6 | 128 | void CreatePidFile(const boost::filesystem::path &path, pid_t pid); |
a034c7eb | 129 | #endif |
d87f00c4 JG |
130 | class missing_zcash_conf : public std::runtime_error { |
131 | public: | |
3ced9364 | 132 | missing_zcash_conf() : std::runtime_error("Missing komodo.conf") { } |
d87f00c4 | 133 | }; |
f4203de3 | 134 | void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet); |
3e468840 PK |
135 | #ifdef WIN32 |
136 | boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true); | |
137 | #endif | |
597fa4cd | 138 | boost::filesystem::path GetTempPath(); |
1f2e0df8 | 139 | void ShrinkDebugFile(); |
429039d4 | 140 | void runCommand(std::string strCommand); |
9064d73b | 141 | const boost::filesystem::path GetExportDir(); |
1f2e0df8 | 142 | |
af021144 S |
143 | /** Returns licensing information (for -version) */ |
144 | std::string LicenseInfo(); | |
1f2e0df8 | 145 | |
1f2e0df8 WL |
146 | inline bool IsSwitchChar(char c) |
147 | { | |
6853e627 | 148 | #ifdef WIN32 |
1f2e0df8 WL |
149 | return c == '-' || c == '/'; |
150 | #else | |
151 | return c == '-'; | |
152 | #endif | |
153 | } | |
154 | ||
3ae07355 GA |
155 | /** |
156 | * Return string argument or default value | |
157 | * | |
158 | * @param strArg Argument to get (e.g. "-foo") | |
159 | * @param default (e.g. "1") | |
160 | * @return command-line argument or default value | |
161 | */ | |
162 | std::string GetArg(const std::string& strArg, const std::string& strDefault); | |
1f2e0df8 | 163 | |
3ae07355 GA |
164 | /** |
165 | * Return integer argument or default value | |
166 | * | |
167 | * @param strArg Argument to get (e.g. "-foo") | |
168 | * @param default (e.g. 1) | |
169 | * @return command-line argument (0 if invalid number) or default value | |
170 | */ | |
51ed9ec9 | 171 | int64_t GetArg(const std::string& strArg, int64_t nDefault); |
1f2e0df8 | 172 | |
3ae07355 GA |
173 | /** |
174 | * Return boolean argument or default value | |
175 | * | |
176 | * @param strArg Argument to get (e.g. "-foo") | |
177 | * @param default (true or false) | |
178 | * @return command-line argument or default value | |
179 | */ | |
3260b4c0 | 180 | bool GetBoolArg(const std::string& strArg, bool fDefault); |
1f2e0df8 | 181 | |
0fcf91ea GA |
182 | /** |
183 | * Set an argument if it doesn't already have a value | |
184 | * | |
185 | * @param strArg Argument to set (e.g. "-foo") | |
186 | * @param strValue Value (e.g. "1") | |
187 | * @return true if argument gets set, false if it already had a value | |
188 | */ | |
189 | bool SoftSetArg(const std::string& strArg, const std::string& strValue); | |
190 | ||
191 | /** | |
192 | * Set a boolean argument if it doesn't already have a value | |
193 | * | |
194 | * @param strArg Argument to set (e.g. "-foo") | |
195 | * @param fValue Value (e.g. false) | |
196 | * @return true if argument gets set, false if it already had a value | |
197 | */ | |
7bf8b7c2 | 198 | bool SoftSetBoolArg(const std::string& strArg, bool fValue); |
1f2e0df8 | 199 | |
1fdb9fa3 LV |
200 | /** |
201 | * Format a string to be used as group of options in help messages | |
202 | * | |
203 | * @param message Group name (e.g. "RPC server options:") | |
204 | * @return the formatted string | |
205 | */ | |
206 | std::string HelpMessageGroup(const std::string& message); | |
207 | ||
208 | /** | |
209 | * Format a string to be used as option description in help messages | |
210 | * | |
211 | * @param option Option message (e.g. "-rpcuser=<user>") | |
212 | * @param message Option description (e.g. "Username for JSON-RPC connections") | |
213 | * @return the formatted string | |
214 | */ | |
215 | std::string HelpMessageOpt(const std::string& option, const std::string& message); | |
216 | ||
610a8c07 | 217 | void SetThreadPriority(int nPriority); |
96931d6f | 218 | void RenameThread(const char* name); |
1f2e0df8 | 219 | |
c63a73d1 MF |
220 | /** |
221 | * .. and a wrapper that just calls func once | |
222 | */ | |
72f14d26 GA |
223 | template <typename Callable> void TraceThread(const char* name, Callable func) |
224 | { | |
58c4c0bb | 225 | std::string s = strprintf("zcash-%s", name); |
72f14d26 GA |
226 | RenameThread(s.c_str()); |
227 | try | |
228 | { | |
881a85a2 | 229 | LogPrintf("%s thread start\n", name); |
72f14d26 | 230 | func(); |
881a85a2 | 231 | LogPrintf("%s thread exit\n", name); |
72f14d26 | 232 | } |
27df4123 | 233 | catch (const boost::thread_interrupted&) |
72f14d26 | 234 | { |
881a85a2 | 235 | LogPrintf("%s thread interrupt\n", name); |
72f14d26 GA |
236 | throw; |
237 | } | |
27df4123 | 238 | catch (const std::exception& e) { |
44235713 WL |
239 | PrintExceptionContinue(&e, name); |
240 | throw; | |
72f14d26 GA |
241 | } |
242 | catch (...) { | |
44235713 WL |
243 | PrintExceptionContinue(NULL, name); |
244 | throw; | |
72f14d26 GA |
245 | } |
246 | } | |
247 | ||
093303a8 | 248 | #endif // BITCOIN_UTIL_H |