44// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
66#include < logging.h>
7+ #include < memusage.h>
78#include < util/fs.h>
89#include < util/string.h>
910#include < util/threadnames.h>
@@ -71,6 +72,9 @@ bool BCLog::Logger::StartLogging()
7172
7273 // dump buffered messages from before we opened the log
7374 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+ }
7478 while (!m_msgs_before_open.empty ()) {
7579 const std::string& s = m_msgs_before_open.front ();
7680
@@ -82,6 +86,7 @@ bool BCLog::Logger::StartLogging()
8286
8387 m_msgs_before_open.pop_front ();
8488 }
89+ m_cur_buffer_memusage = 0 ;
8590 if (m_print_to_console) fflush (stdout);
8691
8792 return true ;
@@ -94,6 +99,11 @@ void BCLog::Logger::DisconnectTestLogger()
9499 if (m_fileout != nullptr ) fclose (m_fileout);
95100 m_fileout = nullptr ;
96101 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+
97107}
98108
99109void BCLog::Logger::DisableLogging ()
@@ -362,9 +372,19 @@ std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level l
362372 return s;
363373}
364374
375+ static size_t MemUsage (const std::string& str)
376+ {
377+ return str.size () + memusage::MallocUsage (sizeof (memusage::list_node<std::string>));
378+ }
379+
365380void 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)
366381{
367382 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+ {
368388 std::string str_prefixed = LogEscapeMessage (str);
369389
370390 if (m_started_new_line) {
@@ -387,6 +407,17 @@ void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& loggi
387407 if (m_buffering) {
388408 // buffer if we haven't started logging yet
389409 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+ }
390421 return ;
391422 }
392423
0 commit comments