Fabcoin Core  0.16.2
P2P Digital Currency
util.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_UTIL_H
11 #define FABCOIN_UTIL_H
12 
13 #if defined(HAVE_CONFIG_H)
14 #include <config/fabcoin-config.h>
15 #endif
16 
17 #include <compat.h>
18 #include <fs.h>
19 #include <sync.h>
20 #include <tinyformat.h>
21 #include <utiltime.h>
22 
23 #include <atomic>
24 #include <exception>
25 #include <map>
26 #include <stdint.h>
27 #include <string>
28 #include <vector>
29 
30 #include <boost/signals2/signal.hpp>
31 
32 #ifndef WIN32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #endif
37 
38 // Application startup time (used for uptime calculation)
39 int64_t GetStartupTime();
40 
41 static const bool DEFAULT_LOGTIMEMICROS = false;
42 static const bool DEFAULT_LOGIPS = false;
43 static const bool DEFAULT_LOGTIMESTAMPS = true;
44 
47 {
48 public:
50  boost::signals2::signal<std::string (const char* psz)> Translate;
51 };
52 
53 extern bool fPrintToConsole;
54 extern bool fPrintToDebugLog;
55 
56 extern bool fLogTimestamps;
57 extern bool fLogTimeMicros;
58 extern bool fLogIPs;
59 extern std::atomic<bool> fReopenDebugLog;
61 
62 extern const char * const FABCOIN_CONF_FILENAME;
63 extern const char * const FABCOIN_PID_FILENAME;
64 
65 extern std::atomic<uint32_t> logCategories;
66 
71 inline std::string _(const char* psz)
72 {
73  boost::optional<std::string> rv = translationInterface.Translate(psz);
74  return rv ? (*rv) : psz;
75 }
76 
77 void SetupEnvironment();
78 bool SetupNetworking();
79 
81 {
82  std::string category;
83  bool active;
84 };
85 
86 namespace BCLog {
87  enum LogFlags : uint32_t {
88  NONE = 0,
89  NET = (1 << 0),
90  TOR = (1 << 1),
91  MEMPOOL = (1 << 2),
92  HTTP = (1 << 3),
93  BENCH = (1 << 4),
94  ZMQ = (1 << 5),
95  DB = (1 << 6),
96  RPC = (1 << 7),
97  ESTIMATEFEE = (1 << 8),
98  ADDRMAN = (1 << 9),
99  SELECTCOINS = (1 << 10),
100  REINDEX = (1 << 11),
101  CMPCTBLOCK = (1 << 12),
102  RAND = (1 << 13),
103  PRUNE = (1 << 14),
104  PROXY = (1 << 15),
105  MEMPOOLREJ = (1 << 16),
106  LIBEVENT = (1 << 17),
107  COINDB = (1 << 18),
108  QT = (1 << 19),
109  LEVELDB = (1 << 20),
110 
111  HTTPPOLL = (1 << 22),
112  POW = (1 << 30),
113  ALL = ~(uint32_t)0,
114  };
115 }
116 
118 bool LogAcceptCategoryChar(const char* category);
119 
121 static inline bool LogAcceptCategory(uint32_t category)
122 {
123  return (logCategories.load(std::memory_order_relaxed) & category) != 0;
124 }
125 
127 std::string ListLogCategories();
128 
130 std::vector<CLogCategoryActive> ListActiveLogCategories();
131 
133 bool GetLogCategory(uint32_t *f, const std::string *str);
134 
136 //int LogPrintStr(const std::string &str );
137 int LogPrintStr(const std::string &str, bool useVMLog = false); // fasc
138 
140 template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
141 
142 static inline void MarkUsed() {}
143 template<typename T, typename... Args> static inline void MarkUsed(const T& t, const Args&... args)
144 {
145  (void)t;
146  MarkUsed(args...);
147 }
148 
149 #ifdef USE_COVERAGE
150 #define LogPrintf(...) do { MarkUsed(__VA_ARGS__); } while(0)
151 #define LogPrint(category, ...) do { MarkUsed(__VA_ARGS__); } while(0)
152 #else
153 #define LogPrintf(...) do { \
154  std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
155  try { \
156  _log_msg_ = tfm::format(__VA_ARGS__); \
157  } catch (tinyformat::format_error &fmterr) { \
158  /* Original format string will have newline so don't add one here */ \
159  _log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
160  } \
161  LogPrintStr(_log_msg_); \
162 } while(0)
163 
164 #define LogPrint(category, ...) do { \
165  if (LogAcceptCategory((category))) { \
166  LogPrintf(__VA_ARGS__); \
167  } \
168 } while(0)
169 #endif
170 
171 #define LogPrintChar(category, ...) do { \
172  if (LogAcceptCategoryChar((category))) { \
173  LogPrintStr(tfm::format(__VA_ARGS__)); \
174  } \
175 } while(0)
176 
177 template<typename... Args>
178 bool error(const char* fmt, const Args&... args)
179 {
180  LogPrintStr("ERROR: " + tfm::format(fmt, args...) + "\n");
181  return false;
182 }
183 
184 void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
185 void FileCommit(FILE *file);
186 bool TruncateFile(FILE *file, unsigned int length);
187 int RaiseFileDescriptorLimit(int nMinFD);
188 void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
189 bool RenameOver(fs::path src, fs::path dest);
190 bool TryCreateDirectories(const fs::path& p);
191 fs::path GetDefaultDataDir();
192 const fs::path &GetDataDir(bool fNetSpecific = true);
193 void ClearDatadirCache();
194 fs::path GetConfigFile(const std::string& confPath);
195 #ifndef WIN32
196 fs::path GetPidFile();
197 void CreatePidFile(const fs::path &path, pid_t pid);
198 #endif
199 #ifdef WIN32
200 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
201 #endif
202 void OpenDebugLog();
203 void ShrinkDebugFile();
204 void runCommand(const std::string& strCommand);
205 
206 inline bool IsSwitchChar(char c)
207 {
208 #ifdef WIN32
209  return c == '-' || c == '/';
210 #else
211  return c == '-';
212 #endif
213 }
214 
216 {
217 protected:
219  std::map<std::string, std::string> mapArgs;
220  std::map<std::string, std::vector<std::string> > mapMultiArgs;
221 public:
222  void ParseParameters(int argc, const char*const argv[]);
223  void ReadConfigFile(const std::string& confPath);
224  std::vector<std::string> GetArgs(const std::string& strArg);
225 
232  bool IsArgSet(const std::string& strArg);
233 
241  std::string GetArg(const std::string& strArg, const std::string& strDefault);
242 
250  int64_t GetArg(const std::string& strArg, int64_t nDefault);
251 
259  bool GetBoolArg(const std::string& strArg, bool fDefault);
260 
268  bool SoftSetArg(const std::string& strArg, const std::string& strValue);
269 
277  bool SoftSetBoolArg(const std::string& strArg, bool fValue);
278 
279  // Forces an arg setting. Called by SoftSetArg() if the arg hasn't already
280  // been set. Also called directly in testing.
281  void ForceSetArg(const std::string& strArg, const std::string& strValue);
282 };
283 
284 extern ArgsManager gArgs;
285 
292 std::string HelpMessageGroup(const std::string& message);
293 
301 std::string HelpMessageOpt(const std::string& option, const std::string& message);
302 
308 int GetNumCores();
309 
310 void SetThreadPriority(int nPriority);
311 void RenameThread(const char* name);
312 
316 template <typename Callable> void TraceThread(const char* name, Callable func)
317 {
318  std::string s = strprintf("fabcoin-%s", name);
319  RenameThread(s.c_str());
320  try
321  {
322  LogPrintf("%s thread start\n", name);
323  func();
324  LogPrintf("%s thread exit\n", name);
325  }
326  catch (const boost::thread_interrupted&)
327  {
328  LogPrintf("%s thread interrupt\n", name);
329  throw;
330  }
331  catch (const std::exception& e) {
332  PrintExceptionContinue(&e, name);
333  throw;
334  }
335  catch (...) {
336  PrintExceptionContinue(nullptr, name);
337  throw;
338  }
339 }
340 
341 std::string CopyrightHolders(const std::string& strPrefix);
342 
343 bool CheckHex(const std::string& str);
344 #endif // FABCOIN_UTIL_H
bool error(const char *fmt, const Args &...args)
Definition: util.h:178
int64_t GetStartupTime()
Server/client environment: argument handling, config file parsing, logging, thread wrappers...
Definition: util.cpp:980
Definition: util.h:95
Definition: util.h:86
void ShrinkDebugFile()
Definition: util.cpp:838
#define strprintf
Definition: tinyformat.h:1054
bool fPrintToConsole
Definition: util.cpp:95
#define T(i, x)
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: util.cpp:306
int LogPrintStr(const std::string &str, bool useVMLog=false)
Send a string to the log output.
Definition: util.cpp:388
bool SetupNetworking()
Definition: util.cpp:933
void RenameThread(const char *name)
Definition: util.cpp:888
#define c(i)
std::atomic< uint32_t > logCategories
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: util.cpp:559
void SetupEnvironment()
Definition: util.cpp:904
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by Boost&#39;s create_directories if the requested directory exists...
Definition: util.cpp:730
bool GetLogCategory(uint32_t *f, const std::string *str)
Return true if str parses as a log category and set the flags in f.
Definition: util.cpp:274
fs::path GetDefaultDataDir()
Definition: util.cpp:593
#define LogPrintf(...)
Definition: util.h:153
void CreatePidFile(const fs::path &path, pid_t pid)
Definition: util.cpp:703
std::string FormatStringFromLogArgs(const char *fmt, const Args &...args)
Get format string from VA_ARGS for error reporting.
Definition: util.h:140
void OpenDebugLog()
Definition: util.cpp:198
const char * name
Definition: rest.cpp:36
std::string ListLogCategories()
Returns a string with the log categories.
Definition: util.cpp:291
void TraceThread(const char *name, Callable func)
Definition: util.h:316
fs::path GetPidFile()
Definition: util.cpp:696
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:959
CCriticalSection cs_args
Definition: util.h:218
bool fLogIPs
Definition: util.cpp:100
const fs::path & GetDataDir(bool fNetSpecific=true)
Definition: util.cpp:623
void runCommand(const std::string &strCommand)
Definition: util.cpp:881
void SetThreadPriority(int nPriority)
Definition: util.cpp:945
bool LogAcceptCategoryChar(const char *category)
Return true if log accepts specified category.
Definition: util.cpp:321
void ClearDatadirCache()
Definition: util.cpp:652
const char *const FABCOIN_CONF_FILENAME
Definition: util.cpp:91
std::map< std::string, std::string > mapArgs
Definition: util.h:219
fs::path GetConfigFile(const std::string &confPath)
Definition: util.cpp:660
#define f(x)
Definition: gost.cpp:57
bool IsSwitchChar(char c)
Definition: util.h:206
std::atomic< bool > fReopenDebugLog
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:586
void FileCommit(FILE *file)
Definition: util.cpp:744
boost::signals2::signal< std::string(const char *psz)> Translate
Translate a message to the native language of the user.
Definition: util.h:50
std::map< std::string, std::vector< std::string > > mapMultiArgs
Definition: util.h:220
std::string CopyrightHolders(const std::string &strPrefix)
Definition: util.cpp:968
LogFlags
Definition: util.h:87
Signals for translation.
Definition: util.h:46
std::string category
Definition: util.h:82
bool fLogTimeMicros
Definition: util.cpp:99
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:714
bool CheckHex(const std::string &str)
Definition: util.cpp:985
#define e(i)
Definition: sha.cpp:733
int RaiseFileDescriptorLimit(int nMinFD)
this function tries to raise the file descriptor limit to the requested number.
Definition: util.cpp:773
ArgsManager gArgs
Definition: util.cpp:94
const char *const FABCOIN_PID_FILENAME
Definition: util.cpp:92
uint8_t format
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:796
CTranslationInterface translationInterface
Definition: util.cpp:102
bool TruncateFile(FILE *file, unsigned int length)
Definition: util.cpp:761
bool fLogTimestamps
Definition: util.cpp:98
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:563
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:71
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:91
bool fPrintToDebugLog
Definition: util.cpp:96