Bitcoin Core  0.19.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::string ListLogCategories()
186 {
187  std::string ret;
188  int outcount = 0;
189  for (const CLogCategoryDesc& category_desc : LogCategories) {
190  // Omit the special cases.
191  if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
192  if (outcount != 0) ret += ", ";
193  ret += category_desc.category;
194  outcount++;
195  }
196  }
197  return ret;
198 }
199 
200 std::vector<CLogCategoryActive> ListActiveLogCategories()
201 {
202  std::vector<CLogCategoryActive> ret;
203  for (const CLogCategoryDesc& category_desc : LogCategories) {
204  // Omit the special cases.
205  if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
206  CLogCategoryActive catActive;
207  catActive.category = category_desc.category;
208  catActive.active = LogAcceptCategory(category_desc.flag);
209  ret.push_back(catActive);
210  }
211  }
212  return ret;
213 }
214 
215 std::string BCLog::Logger::LogTimestampStr(const std::string& str)
216 {
217  std::string strStamped;
218 
219  if (!m_log_timestamps)
220  return str;
221 
222  if (m_started_new_line) {
223  int64_t nTimeMicros = GetTimeMicros();
224  strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
225  if (m_log_time_micros) {
226  strStamped.pop_back();
227  strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
228  }
229  int64_t mocktime = GetMockTime();
230  if (mocktime) {
231  strStamped += " (mocktime: " + FormatISO8601DateTime(mocktime) + ")";
232  }
233  strStamped += ' ' + str;
234  } else
235  strStamped = str;
236 
237  return strStamped;
238 }
239 
240 namespace BCLog {
248  std::string LogEscapeMessage(const std::string& str) {
249  std::string ret;
250  for (char ch_in : str) {
251  uint8_t ch = (uint8_t)ch_in;
252  if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
253  ret += ch_in;
254  } else {
255  ret += strprintf("\\x%02x", ch);
256  }
257  }
258  return ret;
259  }
260 }
261 
262 void BCLog::Logger::LogPrintStr(const std::string& str)
263 {
264  std::lock_guard<std::mutex> scoped_lock(m_cs);
265  std::string str_prefixed = LogEscapeMessage(str);
266 
268  str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] ");
269  }
270 
271  str_prefixed = LogTimestampStr(str_prefixed);
272 
273  m_started_new_line = !str.empty() && str[str.size()-1] == '\n';
274 
275  if (m_buffering) {
276  // buffer if we haven't started logging yet
277  m_msgs_before_open.push_back(str_prefixed);
278  return;
279  }
280 
281  if (m_print_to_console) {
282  // print to console
283  fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
284  fflush(stdout);
285  }
286  for (const auto& cb : m_print_callbacks) {
287  cb(str_prefixed);
288  }
289  if (m_print_to_file) {
290  assert(m_fileout != nullptr);
291 
292  // reopen the log file, if requested
293  if (m_reopen_file) {
294  m_reopen_file = false;
295  FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
296  if (new_fileout) {
297  setbuf(new_fileout, nullptr); // unbuffered
298  fclose(m_fileout);
299  m_fileout = new_fileout;
300  }
301  }
302  FileWriteStr(str_prefixed, m_fileout);
303  }
304 }
305 
307 {
308  // Amount of debug.log to save at end when shrinking (must fit in memory)
309  constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
310 
311  assert(!m_file_path.empty());
312 
313  // Scroll debug.log if it's getting too big
314  FILE* file = fsbridge::fopen(m_file_path, "r");
315 
316  // Special files (e.g. device nodes) may not have a size.
317  size_t log_size = 0;
318  try {
319  log_size = fs::file_size(m_file_path);
320  } catch (const fs::filesystem_error&) {}
321 
322  // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
323  // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
324  if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
325  {
326  // Restart the file with some of the end
327  std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
328  if (fseek(file, -((long)vch.size()), SEEK_END)) {
329  LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
330  fclose(file);
331  return;
332  }
333  int nBytes = fread(vch.data(), 1, vch.size(), file);
334  fclose(file);
335 
336  file = fsbridge::fopen(m_file_path, "w");
337  if (file)
338  {
339  fwrite(vch.data(), 1, nBytes, file);
340  fclose(file);
341  }
342  }
343  else if (file != nullptr)
344  fclose(file);
345 }
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
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
std::string ListLogCategories()
Returns a string with the log categories.
Definition: logging.cpp:185
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:163
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:93
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:262
std::string category
Definition: logging.cpp:137
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:49
void DisableCategory(LogFlags flag)
Definition: logging.cpp:111
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:124
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
Definition: logging.h:144
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:62
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
LogFlags
Definition: logging.h:34
std::string category
Definition: logging.h:29
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:20
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:248
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:12
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:200
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:306
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:215
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:129