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.
7 * Server/client environment: argument handling, config file parsing,
8 * logging, thread wrappers
10 #ifndef BITCOIN_UTIL_H
11 #define BITCOIN_UTIL_H
13 #if defined(HAVE_CONFIG_H)
14 #include "config/bitcoin-config.h"
18 #include "tinyformat.h"
28 #include <boost/filesystem/path.hpp>
29 #include <boost/signals2/signal.hpp>
30 #include <boost/thread/exceptions.hpp>
32 static const bool DEFAULT_LOGTIMEMICROS = false;
33 static const bool DEFAULT_LOGIPS = false;
34 static const bool DEFAULT_LOGTIMESTAMPS = true;
36 /** Signals for translation. */
37 class CTranslationInterface
40 /** Translate a message to the native language of the user. */
41 boost::signals2::signal<std::string (const char* psz)> Translate;
44 extern std::map<std::string, std::string> mapArgs;
45 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
47 extern bool fPrintToConsole;
48 extern bool fPrintToDebugLog;
50 extern std::string strMiscWarning;
51 extern bool fLogTimestamps;
53 extern std::atomic<bool> fReopenDebugLog;
54 extern CTranslationInterface translationInterface;
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.
60 inline std::string _(const char* psz)
62 boost::optional<std::string> rv = translationInterface.Translate(psz);
63 return rv ? (*rv) : psz;
66 void SetupEnvironment();
67 bool SetupNetworking();
69 /** Return true if log accepts specified category */
70 bool LogAcceptCategory(const char* category);
71 /** Send a string to the log output */
72 int LogPrintStr(const std::string &str);
74 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
77 * When we switch to C++11, this can be switched to variadic templates instead
78 * of this macro-based construction (see tinyformat.h).
80 #define MAKE_ERROR_AND_LOG_FUNC(n) \
81 /** Print to debug.log if -debug=category switch is given OR category is NULL. */ \
82 template<TINYFORMAT_ARGTYPES(n)> \
83 static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
85 if(!LogAcceptCategory(category)) return 0; \
86 return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
88 /** Log error and return false */ \
89 template<TINYFORMAT_ARGTYPES(n)> \
90 static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
92 LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
96 TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC)
99 * Zero-arg versions of logging and error, these are not covered by
100 * TINYFORMAT_FOREACH_ARGNUM
102 static inline int LogPrint(const char* category, const char* format)
104 if(!LogAcceptCategory(category)) return 0;
105 return LogPrintStr(format);
107 static inline bool error(const char* format)
109 LogPrintStr(std::string("ERROR: ") + format + "\n");
113 const boost::filesystem::path &ZC_GetParamsDir();
115 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
116 void ParseParameters(int argc, const char*const argv[]);
117 void FileCommit(FILE *fileout);
118 bool TruncateFile(FILE *file, unsigned int length);
119 int RaiseFileDescriptorLimit(int nMinFD);
120 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
121 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
122 bool TryCreateDirectory(const boost::filesystem::path& p);
123 boost::filesystem::path GetDefaultDataDir();
124 const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
125 void ClearDatadirCache();
126 boost::filesystem::path GetConfigFile();
128 boost::filesystem::path GetPidFile();
129 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
131 class missing_zcash_conf : public std::runtime_error {
133 missing_zcash_conf() : std::runtime_error("Missing komodo.conf") { }
135 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
137 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
139 boost::filesystem::path GetTempPath();
141 void ShrinkDebugFile();
142 void runCommand(const std::string& strCommand);
143 const boost::filesystem::path GetExportDir();
145 /** Returns privacy notice (for -version, -help and metrics screen) */
146 std::string PrivacyInfo();
148 /** Returns licensing information (for -version) */
149 std::string LicenseInfo();
151 inline bool IsSwitchChar(char c)
154 return c == '-' || c == '/';
161 * Return string argument or default value
163 * @param strArg Argument to get (e.g. "-foo")
164 * @param default (e.g. "1")
165 * @return command-line argument or default value
167 std::string GetArg(const std::string& strArg, const std::string& strDefault);
170 * Return integer argument or default value
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
176 int64_t GetArg(const std::string& strArg, int64_t nDefault);
179 * Return boolean argument or default value
181 * @param strArg Argument to get (e.g. "-foo")
182 * @param default (true or false)
183 * @return command-line argument or default value
185 bool GetBoolArg(const std::string& strArg, bool fDefault);
188 * Set an argument if it doesn't already have a value
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
194 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
197 * Set a boolean argument if it doesn't already have a value
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
203 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
206 * Format a string to be used as group of options in help messages
208 * @param message Group name (e.g. "RPC server options:")
209 * @return the formatted string
211 std::string HelpMessageGroup(const std::string& message);
214 * Format a string to be used as option description in help messages
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
220 std::string HelpMessageOpt(const std::string& option, const std::string& message);
223 * Return the number of physical cores available on the current system.
224 * @note This does not count virtual cores, such as those provided by HyperThreading
225 * when boost is newer than 1.56.
229 void SetThreadPriority(int nPriority);
230 void RenameThread(const char* name);
233 * .. and a wrapper that just calls func once
235 template <typename Callable> void TraceThread(const char* name, Callable func)
237 std::string s = strprintf("zcash-%s", name);
238 RenameThread(s.c_str());
241 LogPrintf("%s thread start\n", name);
243 LogPrintf("%s thread exit\n", name);
245 catch (const boost::thread_interrupted&)
247 LogPrintf("%s thread interrupt\n", name);
250 catch (const std::exception& e) {
251 PrintExceptionContinue(&e, name);
255 PrintExceptionContinue(NULL, name);
260 #define KOMODO_ASSETCHAIN_MAXLEN 65
263 #endif // BITCOIN_UTIL_H