Bitcoin Core  0.19.99
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 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 
6 #ifndef BITCOIN_LOGGING_H
7 #define BITCOIN_LOGGING_H
8 
9 #include <fs.h>
10 #include <tinyformat.h>
11 
12 #include <atomic>
13 #include <cstdint>
14 #include <list>
15 #include <mutex>
16 #include <string>
17 #include <vector>
18 
19 static const bool DEFAULT_LOGTIMEMICROS = false;
20 static const bool DEFAULT_LOGIPS = false;
21 static const bool DEFAULT_LOGTIMESTAMPS = true;
22 static const bool DEFAULT_LOGTHREADNAMES = false;
23 extern const char * const DEFAULT_DEBUGLOGFILE;
24 
25 extern bool fLogIPs;
26 
28 {
29  std::string category;
30  bool active;
31 };
32 
33 namespace BCLog {
34  enum LogFlags : uint32_t {
35  NONE = 0,
36  NET = (1 << 0),
37  TOR = (1 << 1),
38  MEMPOOL = (1 << 2),
39  HTTP = (1 << 3),
40  BENCH = (1 << 4),
41  ZMQ = (1 << 5),
42  DB = (1 << 6),
43  RPC = (1 << 7),
44  ESTIMATEFEE = (1 << 8),
45  ADDRMAN = (1 << 9),
46  SELECTCOINS = (1 << 10),
47  REINDEX = (1 << 11),
48  CMPCTBLOCK = (1 << 12),
49  RAND = (1 << 13),
50  PRUNE = (1 << 14),
51  PROXY = (1 << 15),
52  MEMPOOLREJ = (1 << 16),
53  LIBEVENT = (1 << 17),
54  COINDB = (1 << 18),
55  QT = (1 << 19),
56  LEVELDB = (1 << 20),
57  ALL = ~(uint32_t)0,
58  };
59 
60  class Logger
61  {
62  private:
63  mutable std::mutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected
64  FILE* m_fileout = nullptr; // GUARDED_BY(m_cs)
65  std::list<std::string> m_msgs_before_open; // GUARDED_BY(m_cs)
66  bool m_buffering{true};
67 
73  std::atomic_bool m_started_new_line{true};
74 
76  std::atomic<uint32_t> m_categories{0};
77 
78  std::string LogTimestampStr(const std::string& str);
79 
80  public:
81  bool m_print_to_console = false;
82  bool m_print_to_file = false;
83 
84  bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
85  bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
86  bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
87 
88  fs::path m_file_path;
89  std::atomic<bool> m_reopen_file{false};
90 
92  void LogPrintStr(const std::string& str);
93 
95  bool Enabled() const
96  {
97  std::lock_guard<std::mutex> scoped_lock(m_cs);
98  return m_buffering || m_print_to_console || m_print_to_file;
99  }
100 
102  bool StartLogging();
104  void DisconnectTestLogger();
105 
106  void ShrinkDebugFile();
107 
108  uint32_t GetCategoryMask() const { return m_categories.load(); }
109 
110  void EnableCategory(LogFlags flag);
111  bool EnableCategory(const std::string& str);
112  void DisableCategory(LogFlags flag);
113  bool DisableCategory(const std::string& str);
114 
115  bool WillLogCategory(LogFlags category) const;
116 
117  bool DefaultShrinkDebugFile() const;
118  };
119 
120 } // namespace BCLog
121 
123 
126 {
127  return LogInstance().WillLogCategory(category);
128 }
129 
131 std::string ListLogCategories();
132 
134 std::vector<CLogCategoryActive> ListActiveLogCategories();
135 
137 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
138 
139 // Be conservative when using LogPrintf/error or other things which
140 // unconditionally log to debug.log! It should not be the case that an inbound
141 // peer can fill up a user's disk with debug.log entries.
142 
143 template <typename... Args>
144 static inline void LogPrintf(const char* fmt, const Args&... args)
145 {
146  if (LogInstance().Enabled()) {
147  std::string log_msg;
148  try {
149  log_msg = tfm::format(fmt, args...);
150  } catch (tinyformat::format_error& fmterr) {
151  /* Original format string will have newline so don't add one here */
152  log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
153  }
154  LogInstance().LogPrintStr(log_msg);
155  }
156 }
157 
158 template <typename... Args>
159 static inline void LogPrint(const BCLog::LogFlags& category, const Args&... args)
160 {
161  if (LogAcceptCategory((category))) {
162  LogPrintf(args...);
163  }
164 }
165 
166 #endif // BITCOIN_LOGGING_H
std::mutex m_cs
Definition: logging.h:63
fs::path m_file_path
Definition: logging.h:88
Definition: logging.h:33
bool fLogIPs
Definition: logging.cpp:35
static const bool DEFAULT_LOGTHREADNAMES
Definition: logging.h:22
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:187
static const bool DEFAULT_LOGTIMEMICROS
Definition: logging.h:19
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:144
BCLog::Logger & LogInstance()
Definition: logging.cpp:14
void LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:227
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:967
uint32_t GetCategoryMask() const
Definition: logging.h:108
static void LogPrint(const BCLog::LogFlags &category, const Args &... args)
Definition: logging.h:159
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:112
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:125
std::string ListLogCategories()
Returns a string with the log categories.
Definition: logging.cpp:172
not actually invalid
LogFlags
Definition: logging.h:34
std::string category
Definition: logging.h:29
std::list< std::string > m_msgs_before_open
Definition: logging.h:65
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:157
static const bool DEFAULT_LOGIPS
Definition: logging.h:20
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:12
bool Enabled() const
Returns whether logs will be written to any output.
Definition: logging.h:95
static const bool DEFAULT_LOGTIMESTAMPS
Definition: logging.h:21