Dogecoin Core  1.14.2
P2P Digital Currency
util.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 
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 
38 {
39 public:
41  boost::signals2::signal<std::string (const char* psz)> Translate;
42 };
43 
44 extern const std::map<std::string, std::vector<std::string> >& mapMultiArgs;
45 extern bool fDebug;
46 extern bool fPrintToConsole;
47 extern bool fPrintToDebugLog;
48 
49 extern bool fLogTimestamps;
50 extern bool fLogTimeMicros;
51 extern bool fLogIPs;
52 extern std::atomic<bool> fReopenDebugLog;
54 
55 extern const char * const BITCOIN_CONF_FILENAME;
56 extern const char * const BITCOIN_PID_FILENAME;
57 
62 inline std::string _(const char* psz)
63 {
64  boost::optional<std::string> rv = translationInterface.Translate(psz);
65  return rv ? (*rv) : psz;
66 }
67 
68 void SetupEnvironment();
69 bool SetupNetworking();
70 
72 bool LogAcceptCategory(const char* category);
74 int LogPrintStr(const std::string &str);
75 
76 #define LogPrint(category, ...) do { \
77  if (LogAcceptCategory((category))) { \
78  LogPrintStr(tfm::format(__VA_ARGS__)); \
79  } \
80 } while(0)
81 
82 #define LogPrintf(...) do { \
83  LogPrintStr(tfm::format(__VA_ARGS__)); \
84 } while(0)
85 
86 template<typename... Args>
87 bool error(const char* fmt, const Args&... args)
88 {
89  LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n");
90  return false;
91 }
92 
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);
105 #ifndef WIN32
106 boost::filesystem::path GetPidFile();
107 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
108 #endif
109 void ReadConfigFile(const std::string& confPath);
110 #ifdef WIN32
111 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
112 #endif
113 void OpenDebugLog();
114 void ShrinkDebugFile();
115 void runCommand(const std::string& strCommand);
116 
117 inline bool IsSwitchChar(char c)
118 {
119 #ifdef WIN32
120  return c == '-' || c == '/';
121 #else
122  return c == '-';
123 #endif
124 }
125 
132 bool IsArgSet(const std::string& strArg);
133 
141 std::string GetArg(const std::string& strArg, const std::string& strDefault);
142 
150 int64_t GetArg(const std::string& strArg, int64_t nDefault);
151 
159 bool GetBoolArg(const std::string& strArg, bool fDefault);
160 
168 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
169 
177 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
178 
179 // Forces a arg setting, used only in testing
180 void ForceSetArg(const std::string& strArg, const std::string& strValue);
181 
188 std::string HelpMessageGroup(const std::string& message);
189 
197 std::string HelpMessageOpt(const std::string& option, const std::string& message);
198 
204 int GetNumCores();
205 
206 void RenameThread(const char* name);
207 
211 template <typename Callable> void TraceThread(const char* name, Callable func)
212 {
213  std::string s = strprintf("dogecoin-%s", name);
214  RenameThread(s.c_str());
215  try
216  {
217  LogPrintf("%s thread start\n", name);
218  func();
219  LogPrintf("%s thread exit\n", name);
220  }
221  catch (const boost::thread_interrupted&)
222  {
223  LogPrintf("%s thread interrupt\n", name);
224  throw;
225  }
226  catch (const std::exception& e) {
227  PrintExceptionContinue(&e, name);
228  throw;
229  }
230  catch (...) {
231  PrintExceptionContinue(NULL, name);
232  throw;
233  }
234 }
235 
236 std::string CopyrightHolders(const std::string& strPrefix);
237 
238 #endif // BITCOIN_UTIL_H
Signals for translation.
Definition: util.h:38
boost::signals2::signal< std::string(const char *psz)> Translate
Translate a message to the native language of the user.
Definition: util.h:41
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:948
#define strprintf
Definition: tinyformat.h:1047
std::atomic< bool > fReopenDebugLog
CTranslationInterface translationInterface
Definition: util.cpp:121
const boost::filesystem::path & GetDataDir(bool fNetSpecific=true)
Definition: util.cpp:513
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
Definition: util.cpp:594
boost::filesystem::path GetConfigFile(const std::string &confPath)
Definition: util.cpp:551
void OpenDebugLog()
Definition: util.cpp:212
bool fLogTimeMicros
Definition: util.cpp:118
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:395
bool fDebug
Definition: util.cpp:113
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: util.cpp:436
bool LogAcceptCategory(const char *category)
Return true if log accepts specified category.
Definition: util.cpp:234
std::string CopyrightHolders(const std::string &strPrefix)
Definition: util.cpp:847
void ShrinkDebugFile()
Definition: util.cpp:729
bool TryCreateDirectory(const boost::filesystem::path &p)
Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
Definition: util.cpp:621
void ParseParameters(int argc, const char *const argv[])
Definition: util.cpp:353
void ReadConfigFile(const std::string &confPath)
Definition: util.cpp:560
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
Definition: util.cpp:411
bool SetupNetworking()
Definition: util.cpp:826
int RaiseFileDescriptorLimit(int nMinFD)
this function tries to raise the file descriptor limit to the requested number.
Definition: util.cpp:664
const std::map< std::string, std::vector< std::string > > & mapMultiArgs
Definition: util.cpp:112
void RenameThread(const char *name)
Definition: util.cpp:781
bool fLogIPs
Definition: util.cpp:119
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
this function tries to make a particular range of a file allocated (corresponding to disk space) it i...
Definition: util.cpp:687
boost::filesystem::path GetPidFile()
Definition: util.cpp:587
bool error(const char *fmt, const Args &... args)
Definition: util.h:87
const char *const BITCOIN_PID_FILENAME
Definition: util.cpp:107
bool fPrintToConsole
Definition: util.cpp:114
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: util.cpp:419
void SetupEnvironment()
Definition: util.cpp:797
bool fLogTimestamps
Definition: util.cpp:117
void runCommand(const std::string &strCommand)
Definition: util.cpp:774
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:475
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result.
Definition: util.h:62
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: util.cpp:448
boost::filesystem::path GetDefaultDataDir()
Definition: util.cpp:482
const char *const BITCOIN_CONF_FILENAME
Definition: util.cpp:106
void ClearDatadirCache()
Definition: util.cpp:543
bool IsSwitchChar(char c)
Definition: util.h:117
void TraceThread(const char *name, Callable func)
Definition: util.h:211
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
Definition: util.cpp:428
bool TruncateFile(FILE *file, unsigned int length)
Definition: util.cpp:652
bool IsArgSet(const std::string &strArg)
Return true if the given argument has been manually set.
Definition: util.cpp:389
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
Definition: util.cpp:605
bool fPrintToDebugLog
Definition: util.cpp:115
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:838
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: util.cpp:452
void FileCommit(FILE *file)
Definition: util.cpp:635
#define LogPrintf(...)
Definition: util.h:82
int LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: util.cpp:295