]> Git Repo - VerusCoin.git/blame - src/util.h
Replace http with https: in links to the MIT license.
[VerusCoin.git] / src / util.h
CommitLineData
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
32static const bool DEFAULT_LOGTIMEMICROS = false;
33static const bool DEFAULT_LOGIPS = false;
34static const bool DEFAULT_LOGTIMESTAMPS = true;
35
b74dcb3b
JT
36/** Signals for translation. */
37class CTranslationInterface
38{
39public:
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
44extern std::map<std::string, std::string> mapArgs;
45extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
46extern bool fDebug;
47extern bool fPrintToConsole;
9e9056cd 48extern bool fPrintToDebugLog;
1f2e0df8 49extern bool fServer;
1f2e0df8 50extern std::string strMiscWarning;
1f2e0df8 51extern bool fLogTimestamps;
2e36866f 52extern bool fLogIPs;
bf673640 53extern std::atomic<bool> fReopenDebugLog;
b74dcb3b
JT
54extern 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 */
62inline std::string _(const char* psz)
63{
64 boost::optional<std::string> rv = translationInterface.Translate(psz);
65 return rv ? (*rv) : psz;
66}
1f2e0df8 67
5248ff40 68void SetupEnvironment();
167b6231 69bool SetupNetworking();
e51321fb 70
c63a73d1 71/** Return true if log accepts specified category */
b77dfdc9 72bool LogAcceptCategory(const char* category);
c63a73d1 73/** Send a string to the log output */
b77dfdc9
WL
74int 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
98TINYFORMAT_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 */
104static inline int LogPrint(const char* category, const char* format)
105{
106 if(!LogAcceptCategory(category)) return 0;
107 return LogPrintStr(format);
108}
109static 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
115const boost::filesystem::path &ZC_GetParamsDir();
116
27df4123 117void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
3ae07355 118void ParseParameters(int argc, const char*const argv[]);
768e5d52 119void FileCommit(FILE *fileout);
1eb57879 120bool TruncateFile(FILE *file, unsigned int length);
ba29a559 121int RaiseFileDescriptorLimit(int nMinFD);
bba89aa8 122void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
768e5d52 123bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
2b7709dc 124bool TryCreateDirectory(const boost::filesystem::path& p);
ee12c3d6
PW
125boost::filesystem::path GetDefaultDataDir();
126const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
51598b26 127void ClearDatadirCache();
ee12c3d6 128boost::filesystem::path GetConfigFile();
a034c7eb 129#ifndef WIN32
d6712db3 130boost::filesystem::path GetPidFile();
ee12c3d6 131void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
a034c7eb 132#endif
d87f00c4
JG
133class missing_zcash_conf : public std::runtime_error {
134public:
135 missing_zcash_conf() : std::runtime_error("Missing zcash.conf") { }
136};
f4203de3 137void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
3e468840
PK
138#ifdef WIN32
139boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
140#endif
597fa4cd 141boost::filesystem::path GetTempPath();
8550bcfe 142void OpenDebugLog();
1f2e0df8 143void ShrinkDebugFile();
db954a65 144void runCommand(const std::string& strCommand);
9064d73b 145const boost::filesystem::path GetExportDir();
1f2e0df8 146
f0d1accb
DH
147/** Returns privacy notice (for -version, -help and metrics screen) */
148std::string PrivacyInfo();
149
af021144
S
150/** Returns licensing information (for -version) */
151std::string LicenseInfo();
152
1f2e0df8
WL
153inline 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 */
169std::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 178int64_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 187bool 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 */
196bool 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 205bool 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 */
213std::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 */
222std::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 */
229int GetNumCores();
230
610a8c07 231void SetThreadPriority(int nPriority);
96931d6f 232void RenameThread(const char* name);
1f2e0df8 233
c63a73d1
MF
234/**
235 * .. and a wrapper that just calls func once
236 */
72f14d26
GA
237template <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
This page took 0.277191 seconds and 4 git commands to generate.