mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-01 10:43:10 +02:00
logging: Add member for arbitrary print callbacks
This commit is contained in:
parent
fba574c908
commit
fa1936f57b
@ -67,6 +67,9 @@ bool BCLog::Logger::StartLogging()
|
||||
|
||||
if (m_print_to_file) FileWriteStr(s, m_fileout);
|
||||
if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
|
||||
for (const auto& cb : m_print_callbacks) {
|
||||
cb(s);
|
||||
}
|
||||
|
||||
m_msgs_before_open.pop_front();
|
||||
}
|
||||
@ -81,6 +84,7 @@ void BCLog::Logger::DisconnectTestLogger()
|
||||
m_buffering = true;
|
||||
if (m_fileout != nullptr) fclose(m_fileout);
|
||||
m_fileout = nullptr;
|
||||
m_print_callbacks.clear();
|
||||
}
|
||||
|
||||
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
|
||||
@ -270,6 +274,9 @@ void BCLog::Logger::LogPrintStr(const std::string& str)
|
||||
fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
for (const auto& cb : m_print_callbacks) {
|
||||
cb(str_prefixed);
|
||||
}
|
||||
if (m_print_to_file) {
|
||||
assert(m_fileout != nullptr);
|
||||
|
||||
|
@ -77,6 +77,9 @@ namespace BCLog {
|
||||
|
||||
std::string LogTimestampStr(const std::string& str);
|
||||
|
||||
/** Slots that connect to the print signal */
|
||||
std::list<std::function<void(const std::string&)>> m_print_callbacks /* GUARDED_BY(m_cs) */ {};
|
||||
|
||||
public:
|
||||
bool m_print_to_console = false;
|
||||
bool m_print_to_file = false;
|
||||
@ -95,7 +98,22 @@ namespace BCLog {
|
||||
bool Enabled() const
|
||||
{
|
||||
std::lock_guard<std::mutex> scoped_lock(m_cs);
|
||||
return m_buffering || m_print_to_console || m_print_to_file;
|
||||
return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
|
||||
}
|
||||
|
||||
/** Connect a slot to the print signal and return the connection */
|
||||
std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun)
|
||||
{
|
||||
std::lock_guard<std::mutex> scoped_lock(m_cs);
|
||||
m_print_callbacks.push_back(std::move(fun));
|
||||
return --m_print_callbacks.end();
|
||||
}
|
||||
|
||||
/** Delete a connection */
|
||||
void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it)
|
||||
{
|
||||
std::lock_guard<std::mutex> scoped_lock(m_cs);
|
||||
m_print_callbacks.erase(it);
|
||||
}
|
||||
|
||||
/** Start logging (and flush all buffered messages) */
|
||||
|
Loading…
x
Reference in New Issue
Block a user