1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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
;
49 extern std::string strMiscWarning
;
50 extern bool fLogTimestamps
;
51 extern bool fLogTimeMicros
;
53 extern std::atomic
<bool> fReopenDebugLog
;
54 extern CTranslationInterface translationInterface
;
56 extern const char * const BITCOIN_CONF_FILENAME
;
57 extern const char * const BITCOIN_PID_FILENAME
;
60 * Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
61 * If no translation slot is registered, nothing is returned, and simply return the input.
63 inline std::string
_(const char* psz
)
65 boost::optional
<std::string
> rv
= translationInterface
.Translate(psz
);
66 return rv
? (*rv
) : psz
;
69 void SetupEnvironment();
70 bool SetupNetworking();
72 /** Return true if log accepts specified category */
73 bool LogAcceptCategory(const char* category
);
74 /** Send a string to the log output */
75 int LogPrintStr(const std::string
&str
);
77 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
79 template<typename
... Args
>
80 static inline int LogPrint(const char* category
, const char* fmt
, const Args
&... args
)
82 if(!LogAcceptCategory(category
)) return 0; \
83 return LogPrintStr(tfm::format(fmt
, args
...));
86 template<typename
... Args
>
87 bool error(const char* fmt
, const Args
&... args
)
89 LogPrintStr("ERROR: " + tfm::format(fmt
, args
...) + "\n");
93 void PrintExceptionContinue(const std::exception
*pex
, const char* pszThread
);
94 void ParseParameters(int argc
, const char*const argv
[]);
95 void FileCommit(FILE *file
);
96 bool TruncateFile(FILE *file
, unsigned int length
);
97 int RaiseFileDescriptorLimit(int nMinFD
);
98 void AllocateFileRange(FILE *file
, unsigned int offset
, unsigned int length
);
99 bool RenameOver(boost::filesystem::path src
, boost::filesystem::path dest
);
100 bool TryCreateDirectory(const boost::filesystem::path
& p
);
101 boost::filesystem::path
GetDefaultDataDir();
102 const boost::filesystem::path
&GetDataDir(bool fNetSpecific
= true);
103 void ClearDatadirCache();
104 boost::filesystem::path
GetConfigFile(const std::string
& confPath
);
106 boost::filesystem::path
GetPidFile();
107 void CreatePidFile(const boost::filesystem::path
&path
, pid_t pid
);
109 void ReadConfigFile(const std::string
& confPath
, std::map
<std::string
, std::string
>& mapSettingsRet
, std::map
<std::string
, std::vector
<std::string
> >& mapMultiSettingsRet
);
111 boost::filesystem::path
GetSpecialFolderPath(int nFolder
, bool fCreate
= true);
114 void ShrinkDebugFile();
115 void runCommand(const std::string
& strCommand
);
117 inline bool IsSwitchChar(char c
)
120 return c
== '-' || c
== '/';
127 * Return string argument or default value
129 * @param strArg Argument to get (e.g. "-foo")
130 * @param default (e.g. "1")
131 * @return command-line argument or default value
133 std::string
GetArg(const std::string
& strArg
, const std::string
& strDefault
);
136 * Return integer argument or default value
138 * @param strArg Argument to get (e.g. "-foo")
139 * @param default (e.g. 1)
140 * @return command-line argument (0 if invalid number) or default value
142 int64_t GetArg(const std::string
& strArg
, int64_t nDefault
);
145 * Return boolean argument or default value
147 * @param strArg Argument to get (e.g. "-foo")
148 * @param default (true or false)
149 * @return command-line argument or default value
151 bool GetBoolArg(const std::string
& strArg
, bool fDefault
);
154 * Set an argument if it doesn't already have a value
156 * @param strArg Argument to set (e.g. "-foo")
157 * @param strValue Value (e.g. "1")
158 * @return true if argument gets set, false if it already had a value
160 bool SoftSetArg(const std::string
& strArg
, const std::string
& strValue
);
163 * Set a boolean argument if it doesn't already have a value
165 * @param strArg Argument to set (e.g. "-foo")
166 * @param fValue Value (e.g. false)
167 * @return true if argument gets set, false if it already had a value
169 bool SoftSetBoolArg(const std::string
& strArg
, bool fValue
);
172 * Format a string to be used as group of options in help messages
174 * @param message Group name (e.g. "RPC server options:")
175 * @return the formatted string
177 std::string
HelpMessageGroup(const std::string
& message
);
180 * Format a string to be used as option description in help messages
182 * @param option Option message (e.g. "-rpcuser=<user>")
183 * @param message Option description (e.g. "Username for JSON-RPC connections")
184 * @return the formatted string
186 std::string
HelpMessageOpt(const std::string
& option
, const std::string
& message
);
189 * Return the number of physical cores available on the current system.
190 * @note This does not count virtual cores, such as those provided by HyperThreading
191 * when boost is newer than 1.56.
195 void RenameThread(const char* name
);
198 * .. and a wrapper that just calls func once
200 template <typename Callable
> void TraceThread(const char* name
, Callable func
)
202 std::string s
= strprintf("bitcoin-%s", name
);
203 RenameThread(s
.c_str());
206 LogPrintf("%s thread start\n", name
);
208 LogPrintf("%s thread exit\n", name
);
210 catch (const boost::thread_interrupted
&)
212 LogPrintf("%s thread interrupt\n", name
);
215 catch (const std::exception
& e
) {
216 PrintExceptionContinue(&e
, name
);
220 PrintExceptionContinue(NULL
, name
);
225 std::string
CopyrightHolders(const std::string
& strPrefix
);
227 #endif // BITCOIN_UTIL_H