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