Skip to content

Commit fa1936f

Browse files
author
MarcoFalke
committed
logging: Add member for arbitrary print callbacks
1 parent fba574c commit fa1936f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/logging.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ bool BCLog::Logger::StartLogging()
6767

6868
if (m_print_to_file) FileWriteStr(s, m_fileout);
6969
if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
70+
for (const auto& cb : m_print_callbacks) {
71+
cb(s);
72+
}
7073

7174
m_msgs_before_open.pop_front();
7275
}
@@ -81,6 +84,7 @@ void BCLog::Logger::DisconnectTestLogger()
8184
m_buffering = true;
8285
if (m_fileout != nullptr) fclose(m_fileout);
8386
m_fileout = nullptr;
87+
m_print_callbacks.clear();
8488
}
8589

8690
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
@@ -270,6 +274,9 @@ void BCLog::Logger::LogPrintStr(const std::string& str)
270274
fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
271275
fflush(stdout);
272276
}
277+
for (const auto& cb : m_print_callbacks) {
278+
cb(str_prefixed);
279+
}
273280
if (m_print_to_file) {
274281
assert(m_fileout != nullptr);
275282

src/logging.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ namespace BCLog {
7777

7878
std::string LogTimestampStr(const std::string& str);
7979

80+
/** Slots that connect to the print signal */
81+
std::list<std::function<void(const std::string&)>> m_print_callbacks /* GUARDED_BY(m_cs) */ {};
82+
8083
public:
8184
bool m_print_to_console = false;
8285
bool m_print_to_file = false;
@@ -95,7 +98,22 @@ namespace BCLog {
9598
bool Enabled() const
9699
{
97100
std::lock_guard<std::mutex> scoped_lock(m_cs);
98-
return m_buffering || m_print_to_console || m_print_to_file;
101+
return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
102+
}
103+
104+
/** Connect a slot to the print signal and return the connection */
105+
std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun)
106+
{
107+
std::lock_guard<std::mutex> scoped_lock(m_cs);
108+
m_print_callbacks.push_back(std::move(fun));
109+
return --m_print_callbacks.end();
110+
}
111+
112+
/** Delete a connection */
113+
void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it)
114+
{
115+
std::lock_guard<std::mutex> scoped_lock(m_cs);
116+
m_print_callbacks.erase(it);
99117
}
100118

101119
/** Start logging (and flush all buffered messages) */

0 commit comments

Comments
 (0)