File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,9 @@ bool BCLog::Logger::StartLogging()
67
67
68
68
if (m_print_to_file) FileWriteStr (s, m_fileout);
69
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
+ }
70
73
71
74
m_msgs_before_open.pop_front ();
72
75
}
@@ -81,6 +84,7 @@ void BCLog::Logger::DisconnectTestLogger()
81
84
m_buffering = true ;
82
85
if (m_fileout != nullptr ) fclose (m_fileout);
83
86
m_fileout = nullptr ;
87
+ m_print_callbacks.clear ();
84
88
}
85
89
86
90
void BCLog::Logger::EnableCategory (BCLog::LogFlags flag)
@@ -270,6 +274,9 @@ void BCLog::Logger::LogPrintStr(const std::string& str)
270
274
fwrite (str_prefixed.data (), 1 , str_prefixed.size (), stdout);
271
275
fflush (stdout);
272
276
}
277
+ for (const auto & cb : m_print_callbacks) {
278
+ cb (str_prefixed);
279
+ }
273
280
if (m_print_to_file) {
274
281
assert (m_fileout != nullptr );
275
282
Original file line number Diff line number Diff line change @@ -77,6 +77,9 @@ namespace BCLog {
77
77
78
78
std::string LogTimestampStr (const std::string& str);
79
79
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
+
80
83
public:
81
84
bool m_print_to_console = false ;
82
85
bool m_print_to_file = false ;
@@ -95,7 +98,22 @@ namespace BCLog {
95
98
bool Enabled () const
96
99
{
97
100
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);
99
117
}
100
118
101
119
/* * Start logging (and flush all buffered messages) */
You can’t perform that action at this time.
0 commit comments