Bitcoin ABC  0.24.7
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 #include <util/string.h>
14 
15 #include <atomic>
16 #include <cstdint>
17 #include <list>
18 #include <mutex>
19 #include <string>
20 
21 static const bool DEFAULT_LOGTIMEMICROS = false;
22 static const bool DEFAULT_LOGIPS = false;
23 static const bool DEFAULT_LOGTIMESTAMPS = true;
24 static const bool DEFAULT_LOGTHREADNAMES = false;
25 
26 extern bool fLogIPs;
27 extern const char *const DEFAULT_DEBUGLOGFILE;
28 
29 struct LogCategory {
30  std::string category;
31  bool active;
32 };
33 
34 namespace BCLog {
35 
36 enum LogFlags : uint32_t {
37  NONE = 0,
38  NET = (1 << 0),
39  TOR = (1 << 1),
40  MEMPOOL = (1 << 2),
41  HTTP = (1 << 3),
42  BENCH = (1 << 4),
43  ZMQ = (1 << 5),
44  WALLETDB = (1 << 6),
45  RPC = (1 << 7),
46  ESTIMATEFEE = (1 << 8),
47  ADDRMAN = (1 << 9),
48  SELECTCOINS = (1 << 10),
49  REINDEX = (1 << 11),
50  CMPCTBLOCK = (1 << 12),
51  RAND = (1 << 13),
52  PRUNE = (1 << 14),
53  PROXY = (1 << 15),
54  MEMPOOLREJ = (1 << 16),
55  LIBEVENT = (1 << 17),
56  COINDB = (1 << 18),
57  QT = (1 << 19),
58  LEVELDB = (1 << 20),
59  VALIDATION = (1 << 21),
60  AVALANCHE = (1 << 22),
61  ALL = ~uint32_t(0),
62 };
63 
64 class Logger {
65 private:
66  // Can not use Mutex from sync.h because in debug mode it would cause a
67  // deadlock when a potential deadlock was detected
68  mutable StdMutex m_cs;
69 
70  FILE *m_fileout GUARDED_BY(m_cs) = nullptr;
71  std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
73  bool m_buffering GUARDED_BY(m_cs) = true;
74 
79  std::atomic_bool m_started_new_line{true};
80 
84  std::atomic<uint32_t> m_categories{0};
85 
86  std::string LogTimestampStr(const std::string &str);
87 
89  std::list<std::function<void(const std::string &)>>
90  m_print_callbacks GUARDED_BY(m_cs){};
91 
92 public:
93  bool m_print_to_console = false;
94  bool m_print_to_file = false;
95 
99 
100  fs::path m_file_path;
101  std::atomic<bool> m_reopen_file{false};
102 
103  ~Logger();
104 
106  void LogPrintStr(const std::string &str);
107 
109  bool Enabled() const {
110  StdLockGuard scoped_lock(m_cs);
111  return m_buffering || m_print_to_console || m_print_to_file ||
112  !m_print_callbacks.empty();
113  }
114 
116  std::list<std::function<void(const std::string &)>>::iterator
117  PushBackCallback(std::function<void(const std::string &)> fun) {
118  StdLockGuard scoped_lock(m_cs);
119  m_print_callbacks.push_back(std::move(fun));
120  return --m_print_callbacks.end();
121  }
122 
125  std::list<std::function<void(const std::string &)>>::iterator it) {
126  StdLockGuard scoped_lock(m_cs);
127  m_print_callbacks.erase(it);
128  }
129 
131  bool StartLogging();
133  void DisconnectTestLogger();
134 
135  void ShrinkDebugFile();
136 
137  uint32_t GetCategoryMask() const { return m_categories.load(); }
138 
139  void EnableCategory(LogFlags category);
140  bool EnableCategory(const std::string &str);
141  void DisableCategory(LogFlags category);
142  bool DisableCategory(const std::string &str);
143 
145  bool WillLogCategory(LogFlags category) const;
146 
148  std::vector<LogCategory> LogCategoriesList();
150  std::string LogCategoriesString() {
151  return Join(LogCategoriesList(), ", ",
152  [&](const LogCategory &i) { return i.category; });
153  };
154 
156  bool DefaultShrinkDebugFile() const;
157 };
158 
159 } // namespace BCLog
160 
162 
164 static inline bool LogAcceptCategory(BCLog::LogFlags category) {
165  return LogInstance().WillLogCategory(category);
166 }
167 
169 bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str);
170 
171 // Be conservative when using LogPrintf/error or other things which
172 // unconditionally log to debug.log! It should not be the case that an inbound
173 // peer can fill up a user's disk with debug.log entries.
174 template <typename... Args>
175 static inline void LogPrintf(const char *fmt, const Args &... args) {
176  if (LogInstance().Enabled()) {
177  std::string log_msg;
178  try {
179  log_msg = tfm::format(fmt, args...);
180  } catch (tinyformat::format_error &fmterr) {
184  log_msg = "Error \"" + std::string(fmterr.what()) +
185  "\" while formatting log message: " + fmt;
186  }
187  LogInstance().LogPrintStr(log_msg);
188  }
189 }
190 
191 // Use a macro instead of a function for conditional logging to prevent
192 // evaluating arguments when logging for the category is not enabled.
193 #define LogPrint(category, ...) \
194  do { \
195  if (LogAcceptCategory((category))) { \
196  LogPrintf(__VA_ARGS__); \
197  } \
198  } while (0)
199 
205 #define LogPrintfToBeContinued LogPrintf
206 #define LogPrintToBeContinued LogPrint
207 
208 #endif // BITCOIN_LOGGING_H
DEFAULT_LOGTIMEMICROS
static const bool DEFAULT_LOGTIMEMICROS
Definition: logging.h:21
BCLog::LogFlags
LogFlags
Definition: logging.h:36
BCLog::Logger::m_print_to_file
bool m_print_to_file
Definition: logging.h:94
BCLog::Logger::LogPrintStr
void LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:213
BCLog::Logger::DeleteCallback
void DeleteCallback(std::list< std::function< void(const std::string &)>>::iterator it)
Delete a connection.
Definition: logging.h:124
BCLog::ZMQ
@ ZMQ
Definition: logging.h:43
tinyformat::format
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
BCLog::HTTP
@ HTTP
Definition: logging.h:41
LogCategory::active
bool active
Definition: logging.h:31
fs.h
BCLog::Logger::WillLogCategory
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
Definition: logging.cpp:322
BCLog::Logger::m_categories
std::atomic< uint32_t > m_categories
Log categories bitfield.
Definition: logging.h:84
BCLog::Logger::StartLogging
bool StartLogging()
Start logging (and flush all buffered messages)
Definition: logging.cpp:39
string.h
DEFAULT_LOGIPS
static const bool DEFAULT_LOGIPS
Definition: logging.h:22
DEFAULT_DEBUGLOGFILE
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:13
LogCategory::category
std::string category
Definition: logging.h:30
BCLog::BENCH
@ BENCH
Definition: logging.h:42
BCLog::Logger::GetCategoryMask
uint32_t GetCategoryMask() const
Definition: logging.h:137
LogInstance
BCLog::Logger & LogInstance()
Definition: logging.cpp:15
DEFAULT_LOGTIMESTAMPS
static const bool DEFAULT_LOGTIMESTAMPS
Definition: logging.h:23
BCLog::Logger::LogCategoriesString
std::string LogCategoriesString()
Returns a string with the log categories.
Definition: logging.h:150
BCLog::Logger::~Logger
~Logger()
Definition: logging.cpp:158
BCLog::ESTIMATEFEE
@ ESTIMATEFEE
Definition: logging.h:46
BCLog::PROXY
@ PROXY
Definition: logging.h:53
BCLog::PRUNE
@ PRUNE
Definition: logging.h:52
BCLog::Logger::DisconnectTestLogger
void DisconnectTestLogger()
Only for testing.
Definition: logging.cpp:84
BCLog::MEMPOOLREJ
@ MEMPOOLREJ
Definition: logging.h:54
tinyformat.h
BCLog::Logger::m_cs
StdMutex m_cs
Definition: logging.h:68
BCLog::RPC
@ RPC
Definition: logging.h:45
BCLog::Logger::m_log_timestamps
bool m_log_timestamps
Definition: logging.h:96
StdLockGuard
Definition: threadsafety.h:77
BCLog::WALLETDB
@ WALLETDB
Definition: logging.h:44
threadsafety.h
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:79
BCLog::Logger::LogCategoriesList
std::vector< LogCategory > LogCategoriesList()
Returns a vector of the log categories.
Definition: logging.cpp:143
BCLog::LEVELDB
@ LEVELDB
Definition: logging.h:58
BCLog::Logger::ShrinkDebugFile
void ShrinkDebugFile()
Definition: logging.cpp:257
BCLog::LIBEVENT
@ LIBEVENT
Definition: logging.h:55
BCLog::Logger::LogTimestampStr
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:164
LogCategory
Definition: logging.h:29
BCLog::Logger::m_reopen_file
std::atomic< bool > m_reopen_file
Definition: logging.h:101
BCLog::TOR
@ TOR
Definition: logging.h:39
BCLog::REINDEX
@ REINDEX
Definition: logging.h:49
BCLog::QT
@ QT
Definition: logging.h:57
BCLog::Logger::PushBackCallback
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:117
BCLog::ALL
@ ALL
Definition: logging.h:61
BCLog::VALIDATION
@ VALIDATION
Definition: logging.h:59
StdMutex
Definition: threadsafety.h:64
BCLog::Logger::Enabled
bool Enabled() const
Returns whether logs will be written to any output.
Definition: logging.h:109
BCLog::Logger::GUARDED_BY
FILE *m_fileout GUARDED_BY(m_cs)
BCLog::Logger::GUARDED_BY
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:90
tinyformat::format_error
Definition: tinyformat.h:184
BCLog::Logger::EnableCategory
void EnableCategory(LogFlags category)
Definition: logging.cpp:296
BCLog::Logger::m_file_path
fs::path m_file_path
Definition: logging.h:100
BCLog::RAND
@ RAND
Definition: logging.h:51
DEFAULT_LOGTHREADNAMES
static const bool DEFAULT_LOGTHREADNAMES
Definition: logging.h:24
Join
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:36
fLogIPs
bool fLogIPs
Definition: logging.cpp:12
BCLog::COINDB
@ COINDB
Definition: logging.h:56
BCLog::Logger::DefaultShrinkDebugFile
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.
Definition: logging.cpp:334
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:129
LogAcceptCategory
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:164
BCLog
Definition: timer.h:16
BCLog::MEMPOOL
@ MEMPOOL
Definition: logging.h:40
BCLog::AVALANCHE
@ AVALANCHE
Definition: logging.h:60
BCLog::Logger::m_print_to_console
bool m_print_to_console
Definition: logging.h:93
BCLog::Logger
Definition: logging.h:64
BCLog::Logger::m_log_time_micros
bool m_log_time_micros
Definition: logging.h:97
BCLog::SELECTCOINS
@ SELECTCOINS
Definition: logging.h:48
BCLog::ADDRMAN
@ ADDRMAN
Definition: logging.h:47
BCLog::NET
@ NET
Definition: logging.h:38
BCLog::CMPCTBLOCK
@ CMPCTBLOCK
Definition: logging.h:50
LogPrintf
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:175
BCLog::Logger::m_log_threadnames
bool m_log_threadnames
Definition: logging.h:98
BCLog::Logger::DisableCategory
void DisableCategory(LogFlags category)
Definition: logging.cpp:309
BCLog::NONE
@ NONE
Definition: logging.h:37