Bitcoin Core  0.18.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  FILE* m_fileout = nullptr;
64  std::mutex m_file_mutex;
65  std::list<std::string> m_msgs_before_open;
66 
72  std::atomic_bool m_started_new_line{true};
73 
75  std::atomic<uint32_t> m_categories{0};
76 
77  std::string LogTimestampStr(const std::string& str);
78 
79  public:
80  bool m_print_to_console = false;
81  bool m_print_to_file = false;
82 
83  bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
84  bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
85  bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
86 
87  fs::path m_file_path;
88  std::atomic<bool> m_reopen_file{false};
89 
91  void LogPrintStr(const std::string &str);
92 
94  bool Enabled() const { return m_print_to_console || m_print_to_file; }
95 
96  bool OpenDebugLog();
97  void ShrinkDebugFile();
98 
99  uint32_t GetCategoryMask() const { return m_categories.load(); }
100 
101  void EnableCategory(LogFlags flag);
102  bool EnableCategory(const std::string& str);
103  void DisableCategory(LogFlags flag);
104  bool DisableCategory(const std::string& str);
105 
106  bool WillLogCategory(LogFlags category) const;
107 
108  bool DefaultShrinkDebugFile() const;
109  };
110 
111 } // namespace BCLog
112 
114 
116 static inline bool LogAcceptCategory(BCLog::LogFlags category)
117 {
118  return LogInstance().WillLogCategory(category);
119 }
120 
122 std::string ListLogCategories();
123 
125 std::vector<CLogCategoryActive> ListActiveLogCategories();
126 
128 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
129 
130 // Be conservative when using LogPrintf/error or other things which
131 // unconditionally log to debug.log! It should not be the case that an inbound
132 // peer can fill up a user's disk with debug.log entries.
133 
134 template <typename... Args>
135 static inline void LogPrintf(const char* fmt, const Args&... args)
136 {
137  if (LogInstance().Enabled()) {
138  std::string log_msg;
139  try {
140  log_msg = tfm::format(fmt, args...);
141  } catch (tinyformat::format_error& fmterr) {
142  /* Original format string will have newline so don't add one here */
143  log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
144  }
145  LogInstance().LogPrintStr(log_msg);
146  }
147 }
148 
149 template <typename... Args>
150 static inline void LogPrint(const BCLog::LogFlags& category, const Args&... args)
151 {
152  if (LogAcceptCategory((category))) {
153  LogPrintf(args...);
154  }
155 }
156 
157 #endif // BITCOIN_LOGGING_H
fs::path m_file_path
Definition: logging.h:87
Definition: logging.h:33
bool fLogIPs
Definition: logging.cpp:35
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:165
BCLog::Logger & LogInstance()
Definition: logging.cpp:14
void LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:205
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:99
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:90
std::string ListLogCategories()
Returns a string with the log categories.
Definition: logging.cpp:150
not actually invalid
std::mutex m_file_mutex
Definition: logging.h:64
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:135
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:12
bool Enabled() const
Returns whether logs will be written to any output.
Definition: logging.h:94