Bitcoin Core  0.20.99
P2P Digital Currency
logging.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <logging.h>
7 #include <util/threadnames.h>
8 #include <util/time.h>
9 
10 #include <mutex>
11 
12 const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
13 
15 {
31  static BCLog::Logger* g_logger{new BCLog::Logger()};
32  return *g_logger;
33 }
34 
36 
37 static int FileWriteStr(const std::string &str, FILE *fp)
38 {
39  return fwrite(str.data(), 1, str.size(), fp);
40 }
41 
43 {
44  std::lock_guard<std::mutex> scoped_lock(m_cs);
45 
46  assert(m_buffering);
47  assert(m_fileout == nullptr);
48 
49  if (m_print_to_file) {
50  assert(!m_file_path.empty());
52  if (!m_fileout) {
53  return false;
54  }
55 
56  setbuf(m_fileout, nullptr); // unbuffered
57 
58  // Add newlines to the logfile to distinguish this execution from the
59  // last one.
60  FileWriteStr("\n\n\n\n\n", m_fileout);
61  }
62 
63  // dump buffered messages from before we opened the log
64  m_buffering = false;
65  while (!m_msgs_before_open.empty()) {
66  const std::string& s = m_msgs_before_open.front();
67 
69  if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
70  for (const auto& cb : m_print_callbacks) {
71  cb(s);
72  }
73 
74  m_msgs_before_open.pop_front();
75  }
76  if (m_print_to_console) fflush(stdout);
77 
78  return true;
79 }
80 
82 {
83  std::lock_guard<std::mutex> scoped_lock(m_cs);
84  m_buffering = true;
85  if (m_fileout != nullptr) fclose(m_fileout);
86  m_fileout = nullptr;
87  m_print_callbacks.clear();
88 }
89 
91 {
92  m_categories |= flag;
93 }
94 
95 bool BCLog::Logger::EnableCategory(const std::string& str)
96 {
97  BCLog::LogFlags flag;
98  if (!GetLogCategory(flag, str)) {
99  if (str == "db") {
100  // DEPRECATION: Added in 0.20, should start returning an error in 0.21
101  LogPrintf("Warning: logging category 'db' is deprecated, use 'walletdb' instead\n");
103  return true;
104  }
105  return false;
106  }
107  EnableCategory(flag);
108  return true;
109 }
110 
112 {
113  m_categories &= ~flag;
114 }
115 
116 bool BCLog::Logger::DisableCategory(const std::string& str)
117 {
118  BCLog::LogFlags flag;
119  if (!GetLogCategory(flag, str)) return false;
120  DisableCategory(flag);
121  return true;
122 }
123 
125 {
126  return (m_categories.load(std::memory_order_relaxed) & category) != 0;
127 }
128 
130 {
131  return m_categories == BCLog::NONE;
132 }
133 
135 {
137  std::string category;
138 };
139 
141 {
142  {BCLog::NONE, "0"},
143  {BCLog::NONE, "none"},
144  {BCLog::NET, "net"},
145  {BCLog::TOR, "tor"},
146  {BCLog::MEMPOOL, "mempool"},
147  {BCLog::HTTP, "http"},
148  {BCLog::BENCH, "bench"},
149  {BCLog::ZMQ, "zmq"},
150  {BCLog::WALLETDB, "walletdb"},
151  {BCLog::RPC, "rpc"},
152  {BCLog::ESTIMATEFEE, "estimatefee"},
153  {BCLog::ADDRMAN, "addrman"},
154  {BCLog::SELECTCOINS, "selectcoins"},
155  {BCLog::REINDEX, "reindex"},
156  {BCLog::CMPCTBLOCK, "cmpctblock"},
157  {BCLog::RAND, "rand"},
158  {BCLog::PRUNE, "prune"},
159  {BCLog::PROXY, "proxy"},
160  {BCLog::MEMPOOLREJ, "mempoolrej"},
161  {BCLog::LIBEVENT, "libevent"},
162  {BCLog::COINDB, "coindb"},
163  {BCLog::QT, "qt"},
164  {BCLog::LEVELDB, "leveldb"},
165  {BCLog::VALIDATION, "validation"},
166  {BCLog::ALL, "1"},
167  {BCLog::ALL, "all"},
168 };
169 
170 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
171 {
172  if (str == "") {
173  flag = BCLog::ALL;
174  return true;
175  }
176  for (const CLogCategoryDesc& category_desc : LogCategories) {
177  if (category_desc.category == str) {
178  flag = category_desc.flag;
179  return true;
180  }
181  }
182  return false;
183 }
184 
185 std::vector<LogCategory> BCLog::Logger::LogCategoriesList()
186 {
187  std::vector<LogCategory> ret;
188  for (const CLogCategoryDesc& category_desc : LogCategories) {
189  // Omit the special cases.
190  if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
191  LogCategory catActive;
192  catActive.category = category_desc.category;
193  catActive.active = WillLogCategory(category_desc.flag);
194  ret.push_back(catActive);
195  }
196  }
197  return ret;
198 }
199 
200 std::string BCLog::Logger::LogTimestampStr(const std::string& str)
201 {
202  std::string strStamped;
203 
204  if (!m_log_timestamps)
205  return str;
206 
207  if (m_started_new_line) {
208  int64_t nTimeMicros = GetTimeMicros();
209  strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
210  if (m_log_time_micros) {
211  strStamped.pop_back();
212  strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
213  }
214  int64_t mocktime = GetMockTime();
215  if (mocktime) {
216  strStamped += " (mocktime: " + FormatISO8601DateTime(mocktime) + ")";
217  }
218  strStamped += ' ' + str;
219  } else
220  strStamped = str;
221 
222  return strStamped;
223 }
224 
225 namespace BCLog {
233  std::string LogEscapeMessage(const std::string& str) {
234  std::string ret;
235  for (char ch_in : str) {
236  uint8_t ch = (uint8_t)ch_in;
237  if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
238  ret += ch_in;
239  } else {
240  ret += strprintf("\\x%02x", ch);
241  }
242  }
243  return ret;
244  }
245 }
246 
247 void BCLog::Logger::LogPrintStr(const std::string& str)
248 {
249  std::lock_guard<std::mutex> scoped_lock(m_cs);
250  std::string str_prefixed = LogEscapeMessage(str);
251 
253  str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] ");
254  }
255 
256  str_prefixed = LogTimestampStr(str_prefixed);
257 
258  m_started_new_line = !str.empty() && str[str.size()-1] == '\n';
259 
260  if (m_buffering) {
261  // buffer if we haven't started logging yet
262  m_msgs_before_open.push_back(str_prefixed);
263  return;
264  }
265 
266  if (m_print_to_console) {
267  // print to console
268  fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
269  fflush(stdout);
270  }
271  for (const auto& cb : m_print_callbacks) {
272  cb(str_prefixed);
273  }
274  if (m_print_to_file) {
275  assert(m_fileout != nullptr);
276 
277  // reopen the log file, if requested
278  if (m_reopen_file) {
279  m_reopen_file = false;
280  FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
281  if (new_fileout) {
282  setbuf(new_fileout, nullptr); // unbuffered
283  fclose(m_fileout);
284  m_fileout = new_fileout;
285  }
286  }
287  FileWriteStr(str_prefixed, m_fileout);
288  }
289 }
290 
292 {
293  // Amount of debug.log to save at end when shrinking (must fit in memory)
294  constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
295 
296  assert(!m_file_path.empty());
297 
298  // Scroll debug.log if it's getting too big
299  FILE* file = fsbridge::fopen(m_file_path, "r");
300 
301  // Special files (e.g. device nodes) may not have a size.
302  size_t log_size = 0;
303  try {
304  log_size = fs::file_size(m_file_path);
305  } catch (const fs::filesystem_error&) {}
306 
307  // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
308  // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
309  if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
310  {
311  // Restart the file with some of the end
312  std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
313  if (fseek(file, -((long)vch.size()), SEEK_END)) {
314  LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
315  fclose(file);
316  return;
317  }
318  int nBytes = fread(vch.data(), 1, vch.size(), file);
319  fclose(file);
320 
321  file = fsbridge::fopen(m_file_path, "w");
322  if (file)
323  {
324  fwrite(vch.data(), 1, nBytes, file);
325  fclose(file);
326  }
327  }
328  else if (file != nullptr)
329  fclose(file);
330 }
void EnableCategory(LogFlags flag)
Definition: logging.cpp:90
BCLog::Logger & LogInstance()
Definition: logging.cpp:14
std::mutex m_cs
Definition: logging.h:64
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:19
std::vector< LogCategory > LogCategoriesList()
Returns a vector of the log categories.
Definition: logging.cpp:185
fs::path m_file_path
Definition: logging.h:92
Definition: timer.h:17
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
BCLog::LogFlags flag
Definition: logging.cpp:136
std::atomic< bool > m_reopen_file
Definition: logging.h:93
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:164
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:78
bool m_print_to_console
Definition: logging.h:85
bool m_print_to_file
Definition: logging.h:86
void LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:247
std::string category
Definition: logging.cpp:137
std::string category
Definition: logging.h:29
bool m_log_threadnames
Definition: logging.h:90
bool m_log_time_micros
Definition: logging.h:89
int64_t GetMockTime()
For testing.
Definition: time.cpp:52
void DisableCategory(LogFlags flag)
Definition: logging.cpp:111
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:124
std::atomic< uint32_t > m_categories
Log categories bitfield.
Definition: logging.h:77
void DisconnectTestLogger()
Only for testing.
Definition: logging.cpp:81
bool StartLogging()
Start logging (and flush all buffered messages)
Definition: logging.cpp:42
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:170
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:65
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:74
FILE * m_fileout
Definition: logging.h:65
const std::string & ThreadGetInternalName()
Get the thread&#39;s internal (in-memory) name; used e.g.
Definition: threadnames.cpp:53
static int FileWriteStr(const std::string &str, FILE *fp)
Definition: logging.cpp:37
const CLogCategoryDesc LogCategories[]
Definition: logging.cpp:140
bool fLogIPs
Definition: logging.cpp:35
bool active
Definition: logging.h:30
LogFlags
Definition: logging.h:34
std::list< std::string > m_msgs_before_open
Definition: logging.h:66
bool m_buffering
Buffer messages before logging can be started. GUARDED_BY(m_cs)
Definition: logging.h:67
bool m_log_timestamps
Definition: logging.h:88
static const bool DEFAULT_LOGIPS
Definition: logging.h:21
std::string LogEscapeMessage(const std::string &str)
Belts and suspenders: make sure outgoing log messages don&#39;t contain potentially suspicious characters...
Definition: logging.cpp:233
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:12
std::list< std::function< void(const std::string &)> > m_print_callbacks
Slots that connect to the print signal.
Definition: logging.h:82
void ShrinkDebugFile()
Definition: logging.cpp:291
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:200
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:129