]> 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();
e51321fb 67
c63a73d1 68/** Return true if log accepts specified category */
b77dfdc9 69bool LogAcceptCategory(const char* category);
c63a73d1 70/** Send a string to the log output */
b77dfdc9
WL
71int 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
95TINYFORMAT_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 */
101static inline int LogPrint(const char* category, const char* format)
102{
103 if(!LogAcceptCategory(category)) return 0;
104 return LogPrintStr(format);
105}
106static 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
112const boost::filesystem::path &ZC_GetParamsDir();
113
27df4123 114void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
3ae07355 115void ParseParameters(int argc, const char*const argv[]);
768e5d52 116void FileCommit(FILE *fileout);
1eb57879 117bool TruncateFile(FILE *file, unsigned int length);
ba29a559 118int RaiseFileDescriptorLimit(int nMinFD);
bba89aa8 119void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
768e5d52 120bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
2b7709dc 121bool TryCreateDirectory(const boost::filesystem::path& p);
ee12c3d6
PW
122boost::filesystem::path GetDefaultDataDir();
123const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
51598b26 124void ClearDatadirCache();
ee12c3d6 125boost::filesystem::path GetConfigFile();
a034c7eb 126#ifndef WIN32
d6712db3 127boost::filesystem::path GetPidFile();
ee12c3d6 128void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
a034c7eb 129#endif
d87f00c4
JG
130class missing_zcash_conf : public std::runtime_error {
131public:
3ced9364 132 missing_zcash_conf() : std::runtime_error("Missing komodo.conf") { }
d87f00c4 133};
f4203de3 134void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
3e468840
PK
135#ifdef WIN32
136boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
137#endif
597fa4cd 138boost::filesystem::path GetTempPath();
1f2e0df8 139void ShrinkDebugFile();
429039d4 140void runCommand(std::string strCommand);
9064d73b 141const boost::filesystem::path GetExportDir();
1f2e0df8 142
af021144
S
143/** Returns licensing information (for -version) */
144std::string LicenseInfo();
1f2e0df8 145
1f2e0df8
WL
146inline 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 */
162std::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 171int64_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 180bool 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 */
189bool 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 198bool 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 */
206std::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 */
215std::string HelpMessageOpt(const std::string& option, const std::string& message);
216
610a8c07 217void SetThreadPriority(int nPriority);
96931d6f 218void RenameThread(const char* name);
1f2e0df8 219
c63a73d1
MF
220/**
221 * .. and a wrapper that just calls func once
222 */
72f14d26
GA
223template <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
This page took 0.254423 seconds and 4 git commands to generate.