PRCYCoin  2.0.0.7rc1
P2P Digital Currency
logging.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin developers
3 // Copyright (c) 2015-2020 The PIVX developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
11 #ifndef BITCOIN_LOGGING_H
12 #define BITCOIN_LOGGING_H
13 
14 #include "fs.h"
15 #include "tinyformat.h"
16 
17 #include <atomic>
18 #include <cstdint>
19 #include <list>
20 #include <mutex>
21 #include <vector>
22 
23 
24 static const bool DEFAULT_LOGTIMEMICROS = false;
25 static const bool DEFAULT_LOGIPS = false;
26 static const bool DEFAULT_LOGTIMESTAMPS = true;
27 extern const char * const DEFAULT_DEBUGLOGFILE;
28 
29 extern bool fLogIPs;
30 
32 {
33  std::string category;
34  bool active;
35 };
36 
37 namespace BCLog {
38  enum LogFlags : uint32_t {
39  NONE = 0,
40  NET = (1 << 0),
41  TOR = (1 << 1),
42  MEMPOOL = (1 << 2),
43  HTTP = (1 << 3),
44  BENCH = (1 << 4),
45  ZMQ = (1 << 5),
46  DB = (1 << 6),
47  RPC = (1 << 7),
48  ESTIMATEFEE = (1 << 8),
49  ADDRMAN = (1 << 9),
50  SELECTCOINS = (1 << 10),
51  REINDEX = (1 << 11),
52  CMPCTBLOCK = (1 << 12),
53  RAND = (1 << 13),
54  PRUNE = (1 << 14),
55  PROXY = (1 << 15),
56  MEMPOOLREJ = (1 << 16),
57  LIBEVENT = (1 << 17),
58  COINDB = (1 << 18),
59  QT = (1 << 19),
60  LEVELDB = (1 << 20),
61  STAKING = (1 << 21),
62  MASTERNODE = (1 << 22),
63  MNBUDGET = (1 << 23),
64  MNPING = (1 << 24),
65  POA = (1 << 25),
66  SUPPLY = (1 << 26),
67  DELETETX = (1 << 27),
68  ALL = ~(uint32_t)0,
69  };
70 
71  class Logger
72  {
73  private:
74  FILE* m_fileout = nullptr;
75  std::mutex m_file_mutex;
76  std::list<std::string> m_msgs_before_open;
77 
83  std::atomic_bool m_started_new_line{true};
84 
86  std::atomic<uint32_t> m_categories{0};
87 
88  std::string LogTimestampStr(const std::string& str);
89 
90  public:
91  bool m_print_to_console = false;
92  bool m_print_to_file = false;
93 
94  bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
95  bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
96 
97  fs::path m_file_path;
98  std::atomic<bool> m_reopen_file{false};
99 
101  int LogPrintStr(const std::string &str);
102 
104  bool Enabled() const { return m_print_to_console || m_print_to_file; }
105 
106  bool OpenDebugLog();
107  void ShrinkDebugFile();
108 
109  uint32_t GetCategoryMask() const { return m_categories.load(); }
110 
111  void EnableCategory(LogFlags flag);
112  bool EnableCategory(const std::string& str);
113  void DisableCategory(LogFlags flag);
114  bool DisableCategory(const std::string& str);
115 
116  bool WillLogCategory(LogFlags category) const;
117 
118  bool DefaultShrinkDebugFile() const;
119  };
120 
121 } // namespace BCLog
122 
123 extern BCLog::Logger* const g_logger;
124 
126 static inline bool LogAcceptCategory(BCLog::LogFlags category)
127 {
128  return g_logger->WillLogCategory(category);
129 }
130 
132 std::string ListLogCategories();
133 
135 std::vector<CLogCategoryActive> ListActiveLogCategories();
136 
138 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
139 
141 template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
142 
143 // Be conservative when using LogPrintf/error or other things which
144 // unconditionally log to debug.log! It should not be the case that an inbound
145 // peer can fill up a user's disk with debug.log entries.
146 
147 #define LogPrintf(...) do { \
148  if(g_logger->Enabled()) { \
149  std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
150  try { \
151  _log_msg_ = tfm::format(__VA_ARGS__); \
152  } catch (tinyformat::format_error &e) { \
153  /* Original format string will have newline so don't add one here */ \
154  _log_msg_ = "Error \"" + std::string(e.what()) + \
155  "\" while formatting log message: " + \
156  FormatStringFromLogArgs(__VA_ARGS__); \
157  } \
158  g_logger->LogPrintStr(_log_msg_); \
159  } \
160 } while(0)
161 
162 #define LogPrint(category, ...) do { \
163  if (LogAcceptCategory((category))) { \
164  LogPrintf(__VA_ARGS__); \
165  } \
166 } while(0)
167 
168 #endif // BITCOIN_LOGGING_H
g_logger
BCLog::Logger *const g_logger
NOTE: the logger instances is leaked on exit.
Definition: logging.cpp:28
BCLog::LogFlags
LogFlags
Definition: logging.h:38
BCLog::Logger::m_print_to_file
bool m_print_to_file
Definition: logging.h:92
BCLog::ZMQ
@ ZMQ
Definition: logging.h:45
BCLog::HTTP
@ HTTP
Definition: logging.h:43
fs.h
BCLog::Logger::WillLogCategory
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:84
BCLog::Logger::m_categories
std::atomic< uint32_t > m_categories
Log categories bitfield.
Definition: logging.h:86
BCLog::Logger::OpenDebugLog
bool OpenDebugLog()
Definition: logging.cpp:38
DEFAULT_DEBUGLOGFILE
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:12
BCLog::BENCH
@ BENCH
Definition: logging.h:44
BCLog::Logger::m_file_mutex
std::mutex m_file_mutex
Definition: logging.h:75
BCLog::Logger::m_fileout
FILE * m_fileout
Definition: logging.h:74
BCLog::Logger::GetCategoryMask
uint32_t GetCategoryMask() const
Definition: logging.h:109
BCLog::ESTIMATEFEE
@ ESTIMATEFEE
Definition: logging.h:48
ListActiveLogCategories
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:164
BCLog::PROXY
@ PROXY
Definition: logging.h:55
BCLog::PRUNE
@ PRUNE
Definition: logging.h:54
BCLog::MEMPOOLREJ
@ MEMPOOLREJ
Definition: logging.h:56
tinyformat.h
BCLog::RPC
@ RPC
Definition: logging.h:47
BCLog::Logger::m_log_timestamps
bool m_log_timestamps
Definition: logging.h:94
CLogCategoryActive
Definition: logging.h:31
BCLog::Logger::m_started_new_line
std::atomic_bool m_started_new_line
m_started_new_line is a state variable that will suppress printing of the timestamp when multiple cal...
Definition: logging.h:83
BCLog::POA
@ POA
Definition: logging.h:65
BCLog::MNPING
@ MNPING
Definition: logging.h:64
BCLog::SUPPLY
@ SUPPLY
Definition: logging.h:66
BCLog::LEVELDB
@ LEVELDB
Definition: logging.h:60
BCLog::Logger::ShrinkDebugFile
void ShrinkDebugFile()
Definition: logging.cpp:231
BCLog::LIBEVENT
@ LIBEVENT
Definition: logging.h:57
BCLog::Logger::LogTimestampStr
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:179
FormatStringFromLogArgs
std::string FormatStringFromLogArgs(const char *fmt, const Args &... args)
Get format string from VA_ARGS for error reporting.
Definition: logging.h:141
BCLog::Logger::m_msgs_before_open
std::list< std::string > m_msgs_before_open
Definition: logging.h:76
BCLog::Logger::EnableCategory
void EnableCategory(LogFlags flag)
Definition: logging.cpp:58
BCLog::Logger::m_reopen_file
std::atomic< bool > m_reopen_file
Definition: logging.h:98
BCLog::TOR
@ TOR
Definition: logging.h:41
BCLog::REINDEX
@ REINDEX
Definition: logging.h:51
BCLog::QT
@ QT
Definition: logging.h:59
BCLog::ALL
@ ALL
Definition: logging.h:68
BCLog::DB
@ DB
Definition: logging.h:46
BCLog::Logger::Enabled
bool Enabled() const
Returns whether logs will be written to any output.
Definition: logging.h:104
BCLog::Logger::m_file_path
fs::path m_file_path
Definition: logging.h:97
BCLog::RAND
@ RAND
Definition: logging.h:53
fLogIPs
bool fLogIPs
Definition: logging.cpp:30
BCLog::COINDB
@ COINDB
Definition: logging.h:58
BCLog::Logger::DefaultShrinkDebugFile
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:89
GetLogCategory
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
Definition: logging.cpp:134
BCLog
Definition: logging.h:37
BCLog::MEMPOOL
@ MEMPOOL
Definition: logging.h:42
BCLog::MASTERNODE
@ MASTERNODE
Definition: logging.h:62
BCLog::Logger::m_print_to_console
bool m_print_to_console
Definition: logging.h:91
BCLog::Logger
Definition: logging.h:71
BCLog::STAKING
@ STAKING
Definition: logging.h:61
BCLog::Logger::LogPrintStr
int LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:199
ListLogCategories
std::string ListLogCategories()
Returns a string with the supported log categories.
Definition: logging.cpp:149
BCLog::Logger::m_log_time_micros
bool m_log_time_micros
Definition: logging.h:95
CLogCategoryActive::active
bool active
Definition: logging.h:34
BCLog::SELECTCOINS
@ SELECTCOINS
Definition: logging.h:50
BCLog::ADDRMAN
@ ADDRMAN
Definition: logging.h:49
CLogCategoryActive::category
std::string category
Definition: logging.h:33
BCLog::DELETETX
@ DELETETX
Definition: logging.h:67
BCLog::MNBUDGET
@ MNBUDGET
Definition: logging.h:63
BCLog::Logger::DisableCategory
void DisableCategory(LogFlags flag)
Definition: logging.cpp:71
BCLog::NET
@ NET
Definition: logging.h:40
BCLog::CMPCTBLOCK
@ CMPCTBLOCK
Definition: logging.h:52
BCLog::NONE
@ NONE
Definition: logging.h:39