]> Git Repo - VerusCoin.git/blob - src/util.h
Implement accurate memory accounting for mempool
[VerusCoin.git] / src / util.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 /**
7  * Server/client environment: argument handling, config file parsing,
8  * logging, thread wrappers
9  */
10 #ifndef BITCOIN_UTIL_H
11 #define BITCOIN_UTIL_H
12
13 #if defined(HAVE_CONFIG_H)
14 #include "config/bitcoin-config.h"
15 #endif
16
17 #include "compat.h"
18 #include "tinyformat.h"
19 #include "utiltime.h"
20
21 #include <atomic>
22 #include <exception>
23 #include <map>
24 #include <stdint.h>
25 #include <string>
26 #include <vector>
27
28 #include <boost/filesystem/path.hpp>
29 #include <boost/signals2/signal.hpp>
30 #include <boost/thread/exceptions.hpp>
31
32 static const bool DEFAULT_LOGTIMEMICROS = false;
33 static const bool DEFAULT_LOGIPS        = false;
34 static const bool DEFAULT_LOGTIMESTAMPS = true;
35
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
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;
48 extern bool fPrintToDebugLog;
49 extern bool fServer;
50 extern std::string strMiscWarning;
51 extern bool fLogTimestamps;
52 extern bool fLogIPs;
53 extern std::atomic<bool> fReopenDebugLog;
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 }
65
66 void SetupEnvironment();
67
68 /** Return true if log accepts specified category */
69 bool LogAcceptCategory(const char* category);
70 /** Send a string to the log output */
71 int LogPrintStr(const std::string &str);
72
73 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
74
75 /**
76  * When we switch to C++11, this can be switched to variadic templates instead
77  * of this macro-based construction (see tinyformat.h).
78  */
79 #define MAKE_ERROR_AND_LOG_FUNC(n)                                        \
80     /**   Print to debug.log if -debug=category switch is given OR category is NULL. */ \
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     }                                                                         \
87     /**   Log error and return false */                                        \
88     template<TINYFORMAT_ARGTYPES(n)>                                          \
89     static inline bool error(const char* format, TINYFORMAT_VARARGS(n))                     \
90     {                                                                         \
91         LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
92         return false;                                                         \
93     }
94
95 TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC)
96
97 /**
98  * Zero-arg versions of logging and error, these are not covered by
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 {
108     LogPrintStr(std::string("ERROR: ") + format + "\n");
109     return false;
110 }
111
112 const boost::filesystem::path &ZC_GetParamsDir();
113
114 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
115 void ParseParameters(int argc, const char*const argv[]);
116 void FileCommit(FILE *fileout);
117 bool TruncateFile(FILE *file, unsigned int length);
118 int RaiseFileDescriptorLimit(int nMinFD);
119 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
120 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
121 bool TryCreateDirectory(const boost::filesystem::path& p);
122 boost::filesystem::path GetDefaultDataDir();
123 const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
124 void ClearDatadirCache();
125 boost::filesystem::path GetConfigFile();
126 #ifndef WIN32
127 boost::filesystem::path GetPidFile();
128 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
129 #endif
130 class missing_zcash_conf : public std::runtime_error {
131 public:
132     missing_zcash_conf() : std::runtime_error("Missing zcash.conf") { }
133 };
134 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
135 #ifdef WIN32
136 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
137 #endif
138 boost::filesystem::path GetTempPath();
139 void OpenDebugLog();
140 void ShrinkDebugFile();
141 void runCommand(const std::string& strCommand);
142 const boost::filesystem::path GetExportDir();
143
144 /** Returns licensing information (for -version) */
145 std::string LicenseInfo();
146
147 inline bool IsSwitchChar(char c)
148 {
149 #ifdef WIN32
150     return c == '-' || c == '/';
151 #else
152     return c == '-';
153 #endif
154 }
155
156 /**
157  * Return string argument or default value
158  *
159  * @param strArg Argument to get (e.g. "-foo")
160  * @param default (e.g. "1")
161  * @return command-line argument or default value
162  */
163 std::string GetArg(const std::string& strArg, const std::string& strDefault);
164
165 /**
166  * Return integer argument or default value
167  *
168  * @param strArg Argument to get (e.g. "-foo")
169  * @param default (e.g. 1)
170  * @return command-line argument (0 if invalid number) or default value
171  */
172 int64_t GetArg(const std::string& strArg, int64_t nDefault);
173
174 /**
175  * Return boolean argument or default value
176  *
177  * @param strArg Argument to get (e.g. "-foo")
178  * @param default (true or false)
179  * @return command-line argument or default value
180  */
181 bool GetBoolArg(const std::string& strArg, bool fDefault);
182
183 /**
184  * Set an argument if it doesn't already have a value
185  *
186  * @param strArg Argument to set (e.g. "-foo")
187  * @param strValue Value (e.g. "1")
188  * @return true if argument gets set, false if it already had a value
189  */
190 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
191
192 /**
193  * Set a boolean argument if it doesn't already have a value
194  *
195  * @param strArg Argument to set (e.g. "-foo")
196  * @param fValue Value (e.g. false)
197  * @return true if argument gets set, false if it already had a value
198  */
199 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
200
201 /**
202  * Format a string to be used as group of options in help messages
203  *
204  * @param message Group name (e.g. "RPC server options:")
205  * @return the formatted string
206  */
207 std::string HelpMessageGroup(const std::string& message);
208
209 /**
210  * Format a string to be used as option description in help messages
211  *
212  * @param option Option message (e.g. "-rpcuser=<user>")
213  * @param message Option description (e.g. "Username for JSON-RPC connections")
214  * @return the formatted string
215  */
216 std::string HelpMessageOpt(const std::string& option, const std::string& message);
217
218 void SetThreadPriority(int nPriority);
219 void RenameThread(const char* name);
220
221 /**
222  * .. and a wrapper that just calls func once
223  */
224 template <typename Callable> void TraceThread(const char* name,  Callable func)
225 {
226     std::string s = strprintf("zcash-%s", name);
227     RenameThread(s.c_str());
228     try
229     {
230         LogPrintf("%s thread start\n", name);
231         func();
232         LogPrintf("%s thread exit\n", name);
233     }
234     catch (const boost::thread_interrupted&)
235     {
236         LogPrintf("%s thread interrupt\n", name);
237         throw;
238     }
239     catch (const std::exception& e) {
240         PrintExceptionContinue(&e, name);
241         throw;
242     }
243     catch (...) {
244         PrintExceptionContinue(NULL, name);
245         throw;
246     }
247 }
248
249 #endif // BITCOIN_UTIL_H
This page took 0.035851 seconds and 4 git commands to generate.