Bitcoin ABC  0.22.12
P2P Digital Currency
logging.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 // Copyright (c) 2017-2019 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_LOGGING_H
8 #define BITCOIN_LOGGING_H
9 
10 #include <fs.h>
11 #include <threadsafety.h>
12 #include <tinyformat.h>
13 
14 #include <atomic>
15 #include <cstdint>
16 #include <list>
17 #include <mutex>
18 #include <string>
19 
20 static const bool DEFAULT_LOGTIMEMICROS = false;
21 static const bool DEFAULT_LOGIPS = false;
22 static const bool DEFAULT_LOGTIMESTAMPS = true;
23 static const bool DEFAULT_LOGTHREADNAMES = false;
24 
25 extern bool fLogIPs;
26 extern const char *const DEFAULT_DEBUGLOGFILE;
27 
29  std::string category;
30  bool active;
31 };
32 
33 namespace BCLog {
34 
35 enum LogFlags : uint32_t {
36  NONE = 0,
37  NET = (1 << 0),
38  TOR = (1 << 1),
39  MEMPOOL = (1 << 2),
40  HTTP = (1 << 3),
41  BENCH = (1 << 4),
42  ZMQ = (1 << 5),
43  WALLETDB = (1 << 6),
44  RPC = (1 << 7),
45  ESTIMATEFEE = (1 << 8),
46  ADDRMAN = (1 << 9),
47  SELECTCOINS = (1 << 10),
48  REINDEX = (1 << 11),
49  CMPCTBLOCK = (1 << 12),
50  RAND = (1 << 13),
51  PRUNE = (1 << 14),
52  PROXY = (1 << 15),
53  MEMPOOLREJ = (1 << 16),
54  LIBEVENT = (1 << 17),
55  COINDB = (1 << 18),
56  QT = (1 << 19),
57  LEVELDB = (1 << 20),
58  VALIDATION = (1 << 21),
59  ALL = ~uint32_t(0),
60 };
61 
62 class Logger {
63 private:
64  // Can not use Mutex from sync.h because in debug mode it would cause a
65  // deadlock when a potential deadlock was detected
66  mutable StdMutex m_cs;
67 
68  FILE *m_fileout GUARDED_BY(m_cs) = nullptr;
69  std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
71  bool m_buffering GUARDED_BY(m_cs) = true;
72 
77  std::atomic_bool m_started_new_line{true};
78 
82  std::atomic<uint32_t> m_categories{0};
83 
84  std::string LogTimestampStr(const std::string &str);
85 
87  std::list<std::function<void(const std::string &)>>
88  m_print_callbacks GUARDED_BY(m_cs){};
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  bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
97 
98  fs::path m_file_path;
99  std::atomic<bool> m_reopen_file{false};
100 
101  ~Logger();
102 
104  void LogPrintStr(const std::string &str);
105 
107  bool Enabled() const {
108  StdLockGuard scoped_lock(m_cs);
109  return m_buffering || m_print_to_console || m_print_to_file ||
110  !m_print_callbacks.empty();
111  }
112 
114  std::list<std::function<void(const std::string &)>>::iterator
115  PushBackCallback(std::function<void(const std::string &)> fun) {
116  StdLockGuard scoped_lock(m_cs);
117  m_print_callbacks.push_back(std::move(fun));
118  return --m_print_callbacks.end();
119  }
120 
123  std::list<std::function<void(const std::string &)>>::iterator it) {
124  StdLockGuard scoped_lock(m_cs);
125  m_print_callbacks.erase(it);
126  }
127 
129  bool StartLogging();
131  void DisconnectTestLogger();
132 
133  void ShrinkDebugFile();
134 
135  uint32_t GetCategoryMask() const { return m_categories.load(); }
136 
137  void EnableCategory(LogFlags category);
138  bool EnableCategory(const std::string &str);
139  void DisableCategory(LogFlags category);
140  bool DisableCategory(const std::string &str);
141 
143  bool WillLogCategory(LogFlags category) const;
144 
146  bool DefaultShrinkDebugFile() const;
147 };
148 
149 } // namespace BCLog
150 
152 
155  return LogInstance().WillLogCategory(category);
156 }
157 
159 std::string ListLogCategories();
160 
162 std::vector<CLogCategoryActive> ListActiveLogCategories();
163 
165 bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str);
166 
167 // Be conservative when using LogPrintf/error or other things which
168 // unconditionally log to debug.log! It should not be the case that an inbound
169 // peer can fill up a user's disk with debug.log entries.
170 template <typename... Args>
171 static inline void LogPrintf(const char *fmt, const Args &... args) {
172  if (LogInstance().Enabled()) {
173  std::string log_msg;
174  try {
175  log_msg = tfm::format(fmt, args...);
176  } catch (tinyformat::format_error &fmterr) {
180  log_msg = "Error \"" + std::string(fmterr.what()) +
181  "\" while formatting log message: " + fmt;
182  }
183  LogInstance().LogPrintStr(log_msg);
184  }
185 }
186 
187 // Use a macro instead of a function for conditional logging to prevent
188 // evaluating arguments when logging for the category is not enabled.
189 #define LogPrint(category, ...) \
190  do { \
191  if (LogAcceptCategory((category))) { \
192  LogPrintf(__VA_ARGS__); \
193  } \
194  } while (0)
195 
201 #define LogPrintfToBeContinued LogPrintf
202 #define LogPrintToBeContinued LogPrint
203 
204 #endif // BITCOIN_LOGGING_H
StdMutex m_cs
Definition: logging.h:66
std::list< std::function< void(const std::string &)> > m_print_callbacks GUARDED_BY(m_cs)
Slots that connect to the print signal.
Definition: logging.h:88
fs::path m_file_path
Definition: logging.h:98
Definition: timer.h:16
bool fLogIPs
Definition: logging.cpp:12
static const bool DEFAULT_LOGTHREADNAMES
Definition: logging.h:23
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:159
static const bool DEFAULT_LOGTIMEMICROS
Definition: logging.h:20
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:171
std::list< std::function< void(const std::string &)> >::iterator PushBackCallback(std::function< void(const std::string &)> fun)
Connect a slot to the print signal and return the connection.
Definition: logging.h:115
BCLog::Logger & LogInstance()
Definition: logging.cpp:15
void LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:229
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:1111
uint32_t GetCategoryMask() const
Definition: logging.h:135
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
Definition: logging.cpp:338
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:154
std::string ListLogCategories()
Returns a string with the log categories.
Definition: logging.cpp:142
void DeleteCallback(std::list< std::function< void(const std::string &)>>::iterator it)
Delete a connection.
Definition: logging.h:122
LogFlags
Definition: logging.h:35
std::string category
Definition: logging.h:29
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:128
#define GUARDED_BY(x)
Definition: threadsafety.h:45
static const bool DEFAULT_LOGIPS
Definition: logging.h:21
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:13
bool Enabled() const
Returns whether logs will be written to any output.
Definition: logging.h:107
static const bool DEFAULT_LOGTIMESTAMPS
Definition: logging.h:22