Bitcoin
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 
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);
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 
125 static inline bool LogAcceptCategory(BCLog::LogFlags category)
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
bool m_log_threadnames
Definition: logging.h:86
Definition: logging.h:37
Definition: logging.h:39
std::atomic_bool m_started_new_line
Definition: logging.h:73
Definition: logging.h:45
std::mutex m_cs
Definition: logging.h:63
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:117
Definition: logging.h:27
Definition: logging.h:56
bool m_log_time_micros
Definition: logging.h:85
Definition: logging.h:49
bool fLogIPs
Definition: logging.cpp:35
Definition: logging.h:35
Definition: logging.h:57
static const bool DEFAULT_LOGTHREADNAMES
Definition: logging.h:22
std::atomic< bool > m_reopen_file
Definition: logging.h:89
Definition: logging.h:43
std::vector< CLogCategoryActive > ListActiveLogCategories()
Definition: logging.cpp:187
static const bool DEFAULT_LOGTIMEMICROS
Definition: logging.h:19
bool m_print_to_console
Definition: logging.h:81
Definition: logging.h:47
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:144
BCLog::Logger & LogInstance()
Definition: logging.cpp:14
void EnableCategory(LogFlags flag)
Definition: logging.cpp:86
void LogPrintStr(const std::string &str)
Definition: logging.cpp:227
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:112
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:202
args
Definition: filter-lcov.py:10
bool m_log_timestamps
Definition: logging.h:84
bool active
Definition: logging.h:30
std::list< std::string > m_msgs_before_open
Definition: logging.h:65
std::string category
Definition: logging.h:29
fs::path m_file_path
Definition: logging.h:88
void DisconnectTestLogger()
Definition: logging.cpp:78
bool m_buffering
Buffer messages before logging can be started. GUARDED_BY(m_cs)
Definition: logging.h:66
unsigned int uint32_t
Definition: stdint.h:21
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
FILE * m_fileout
Definition: logging.h:64
static void LogPrint(const BCLog::LogFlags &category, const Args &... args)
Definition: logging.h:159
Definition: tinyformat.h:167
uint32_t GetCategoryMask() const
Definition: logging.h:108
void ShrinkDebugFile()
Definition: logging.cpp:268
static bool LogAcceptCategory(BCLog::LogFlags category)
Definition: logging.h:125
Definition: logging.h:40
bool StartLogging()
Definition: logging.cpp:42
Definition: logging.h:54
Definition: logging.h:52
std::string ListLogCategories()
Definition: logging.cpp:172
LogFlags
Definition: logging.h:34
Definition: logging.h:55
Definition: logging.h:48
Definition: logging.h:44
Definition: logging.h:33
Definition: logging.h:38
void DisableCategory(LogFlags flag)
Definition: logging.cpp:99
Definition: logging.h:60
bool Enabled() const
Definition: logging.h:95
Definition: logging.h:46
bool m_print_to_file
Definition: logging.h:82
Definition: logging.h:41
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Definition: logging.cpp:157
Definition: logging.h:53
Definition: logging.h:42
static const bool DEFAULT_LOGIPS
Definition: logging.h:20
Definition: logging.h:51
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:12
Definition: logging.h:36
std::atomic< uint32_t > m_categories
Definition: logging.h:76
Definition: logging.h:50
static const bool DEFAULT_LOGTIMESTAMPS
Definition: logging.h:21