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"
27 #include <boost/filesystem/path.hpp>
28 #include <boost/signals2/signal.hpp>
29 #include <boost/thread/exceptions.hpp>
31 static const bool DEFAULT_LOGTIMEMICROS
= false;
33 /** Signals for translation. */
34 class CTranslationInterface
37 /** Translate a message to the native language of the user. */
38 boost::signals2::signal
<std::string (const char* psz
)> Translate
;
41 extern std::map
<std::string
, std::string
> mapArgs
;
42 extern std::map
<std::string
, std::vector
<std::string
> > mapMultiArgs
;
44 extern bool fPrintToConsole
;
45 extern bool fPrintToDebugLog
;
47 extern std::string strMiscWarning
;
48 extern bool fLogTimestamps
;
49 extern bool fLogTimeMicros
;
51 extern volatile bool fReopenDebugLog
;
52 extern CTranslationInterface translationInterface
;
55 * Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
56 * If no translation slot is registered, nothing is returned, and simply return the input.
58 inline std::string
_(const char* psz
)
60 boost::optional
<std::string
> rv
= translationInterface
.Translate(psz
);
61 return rv
? (*rv
) : psz
;
64 void SetupEnvironment();
65 bool SetupNetworking();
67 /** Return true if log accepts specified category */
68 bool LogAcceptCategory(const char* category
);
69 /** Send a string to the log output */
70 int LogPrintStr(const std::string
&str
);
72 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
75 * When we switch to C++11, this can be switched to variadic templates instead
76 * of this macro-based construction (see tinyformat.h).
78 #define MAKE_ERROR_AND_LOG_FUNC(n) \
79 /** Print to debug.log if -debug=category switch is given OR category is NULL. */ \
80 template<TINYFORMAT_ARGTYPES(n)> \
81 static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
83 if(!LogAcceptCategory(category)) return 0; \
84 return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
86 /** Log error and return false */ \
87 template<TINYFORMAT_ARGTYPES(n)> \
88 static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
90 LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
94 TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC
)
97 * Zero-arg versions of logging and error, these are not covered by
98 * TINYFORMAT_FOREACH_ARGNUM
100 static inline int LogPrint(const char* category
, const char* format
)
102 if(!LogAcceptCategory(category
)) return 0;
103 return LogPrintStr(format
);
105 static inline bool error(const char* format
)
107 LogPrintStr(std::string("ERROR: ") + format
+ "\n");
111 void PrintExceptionContinue(const std::exception
*pex
, const char* pszThread
);
112 void ParseParameters(int argc
, const char*const argv
[]);
113 void FileCommit(FILE *fileout
);
114 bool TruncateFile(FILE *file
, unsigned int length
);
115 int RaiseFileDescriptorLimit(int nMinFD
);
116 void AllocateFileRange(FILE *file
, unsigned int offset
, unsigned int length
);
117 bool RenameOver(boost::filesystem::path src
, boost::filesystem::path dest
);
118 bool TryCreateDirectory(const boost::filesystem::path
& p
);
119 boost::filesystem::path
GetDefaultDataDir();
120 const boost::filesystem::path
&GetDataDir(bool fNetSpecific
= true);
121 void ClearDatadirCache();
122 boost::filesystem::path
GetConfigFile();
124 boost::filesystem::path
GetPidFile();
125 void CreatePidFile(const boost::filesystem::path
&path
, pid_t pid
);
127 void ReadConfigFile(std::map
<std::string
, std::string
>& mapSettingsRet
, std::map
<std::string
, std::vector
<std::string
> >& mapMultiSettingsRet
);
129 boost::filesystem::path
GetSpecialFolderPath(int nFolder
, bool fCreate
= true);
131 boost::filesystem::path
GetTempPath();
133 void ShrinkDebugFile();
134 void runCommand(const std::string
& strCommand
);
136 inline bool IsSwitchChar(char c
)
139 return c
== '-' || c
== '/';
146 * Return string argument or default value
148 * @param strArg Argument to get (e.g. "-foo")
149 * @param default (e.g. "1")
150 * @return command-line argument or default value
152 std::string
GetArg(const std::string
& strArg
, const std::string
& strDefault
);
155 * Return integer argument or default value
157 * @param strArg Argument to get (e.g. "-foo")
158 * @param default (e.g. 1)
159 * @return command-line argument (0 if invalid number) or default value
161 int64_t GetArg(const std::string
& strArg
, int64_t nDefault
);
164 * Return boolean argument or default value
166 * @param strArg Argument to get (e.g. "-foo")
167 * @param default (true or false)
168 * @return command-line argument or default value
170 bool GetBoolArg(const std::string
& strArg
, bool fDefault
);
173 * Set an argument if it doesn't already have a value
175 * @param strArg Argument to set (e.g. "-foo")
176 * @param strValue Value (e.g. "1")
177 * @return true if argument gets set, false if it already had a value
179 bool SoftSetArg(const std::string
& strArg
, const std::string
& strValue
);
182 * Set a boolean argument if it doesn't already have a value
184 * @param strArg Argument to set (e.g. "-foo")
185 * @param fValue Value (e.g. false)
186 * @return true if argument gets set, false if it already had a value
188 bool SoftSetBoolArg(const std::string
& strArg
, bool fValue
);
191 * Format a string to be used as group of options in help messages
193 * @param message Group name (e.g. "RPC server options:")
194 * @return the formatted string
196 std::string
HelpMessageGroup(const std::string
& message
);
199 * Format a string to be used as option description in help messages
201 * @param option Option message (e.g. "-rpcuser=<user>")
202 * @param message Option description (e.g. "Username for JSON-RPC connections")
203 * @return the formatted string
205 std::string
HelpMessageOpt(const std::string
& option
, const std::string
& message
);
208 * Return the number of physical cores available on the current system.
209 * @note This does not count virtual cores, such as those provided by HyperThreading
210 * when boost is newer than 1.56.
214 void SetThreadPriority(int nPriority
);
215 void RenameThread(const char* name
);
218 * .. and a wrapper that just calls func once
220 template <typename Callable
> void TraceThread(const char* name
, Callable func
)
222 std::string s
= strprintf("bitcoin-%s", name
);
223 RenameThread(s
.c_str());
226 LogPrintf("%s thread start\n", name
);
228 LogPrintf("%s thread exit\n", name
);
230 catch (const boost::thread_interrupted
&)
232 LogPrintf("%s thread interrupt\n", name
);
235 catch (const std::exception
& e
) {
236 PrintExceptionContinue(&e
, name
);
240 PrintExceptionContinue(NULL
, name
);
245 #endif // BITCOIN_UTIL_H