4
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
6
6
#include < logging.h>
7
+ #include < memusage.h>
7
8
#include < util/fs.h>
8
9
#include < util/string.h>
9
10
#include < util/threadnames.h>
@@ -71,6 +72,9 @@ bool BCLog::Logger::StartLogging()
71
72
72
73
// dump buffered messages from before we opened the log
73
74
m_buffering = false ;
75
+ if (m_buffer_lines_discarded > 0 ) {
76
+ LogPrintStr_ (strprintf (" Early logging buffer overflowed, %d log lines discarded.\n " , m_buffer_lines_discarded), __func__, __FILE__, __LINE__, BCLog::ALL, Level::Info);
77
+ }
74
78
while (!m_msgs_before_open.empty ()) {
75
79
const std::string& s = m_msgs_before_open.front ();
76
80
@@ -82,6 +86,7 @@ bool BCLog::Logger::StartLogging()
82
86
83
87
m_msgs_before_open.pop_front ();
84
88
}
89
+ m_cur_buffer_memusage = 0 ;
85
90
if (m_print_to_console) fflush (stdout);
86
91
87
92
return true ;
@@ -94,6 +99,11 @@ void BCLog::Logger::DisconnectTestLogger()
94
99
if (m_fileout != nullptr ) fclose (m_fileout);
95
100
m_fileout = nullptr ;
96
101
m_print_callbacks.clear ();
102
+ m_max_buffer_memusage = DEFAULT_MAX_LOG_BUFFER;
103
+ m_cur_buffer_memusage = 0 ;
104
+ m_buffer_lines_discarded = 0 ;
105
+ m_msgs_before_open.clear ();
106
+
97
107
}
98
108
99
109
void BCLog::Logger::DisableLogging ()
@@ -362,9 +372,19 @@ std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level l
362
372
return s;
363
373
}
364
374
375
+ static size_t MemUsage (const std::string& str)
376
+ {
377
+ return str.size () + memusage::MallocUsage (sizeof (memusage::list_node<std::string>));
378
+ }
379
+
365
380
void BCLog::Logger::LogPrintStr (const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
366
381
{
367
382
StdLockGuard scoped_lock (m_cs);
383
+ return LogPrintStr_ (str, logging_function, source_file, source_line, category, level);
384
+ }
385
+
386
+ void BCLog::Logger::LogPrintStr_ (const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
387
+ {
368
388
std::string str_prefixed = LogEscapeMessage (str);
369
389
370
390
if (m_started_new_line) {
@@ -387,6 +407,17 @@ void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& loggi
387
407
if (m_buffering) {
388
408
// buffer if we haven't started logging yet
389
409
m_msgs_before_open.push_back (str_prefixed);
410
+
411
+ m_cur_buffer_memusage += MemUsage (str_prefixed);
412
+ while (m_cur_buffer_memusage > m_max_buffer_memusage) {
413
+ if (m_msgs_before_open.empty ()) {
414
+ m_cur_buffer_memusage = 0 ;
415
+ break ;
416
+ }
417
+ m_cur_buffer_memusage -= MemUsage (m_msgs_before_open.front ());
418
+ m_msgs_before_open.pop_front ();
419
+ ++m_buffer_lines_discarded;
420
+ }
390
421
return ;
391
422
}
392
423
0 commit comments