Skip to content

Commit 9e2147b

Browse files
committed
test: Use KJ_LOG insted of std::cout for logging
This avoids thread sanitizer errors like: Read of size 8 at 0x7ffff7c81cb8 by thread T8: #0 std::__1::ios_base::width[abi:ne210101]() const /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/ios:497 (mptest+0x13e737) bitcoin-core#1 std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output[abi:ne210101]<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/__locale_dir/pad_and_output.h:54 (mptest+0x13e737) bitcoin-core#2 std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence[abi:ne210101]<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/__ostream/put_character_sequence.h:37 (mptest+0x13e5e0) bitcoin-core#3 std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<[abi:ne210101]<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/__ostream/basic_ostream.h:438 (mptest+0x13e0ee) Previous write of size 8 at 0x7ffff7c81cb8 by main thread (mutexes: write M0): #0 std::__1::ios_base::width[abi:ne210101](long) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/ios:501 (mptest+0x13e8bc) bitcoin-core#1 std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output[abi:ne210101]<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/__locale_dir/pad_and_output.h:80 (mptest+0x13e8bc) bitcoin-core#2 std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence[abi:ne210101]<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/__ostream/put_character_sequence.h:37 (mptest+0x13e5e0) bitcoin-core#3 std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<[abi:ne210101]<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) /nix/store/71bq4557fksv43c9lf9j5ywrj6zk17hl-libcxx-21.1.1-dev/include/c++/v1/__ostream/basic_ostream.h:438 (mptest+0x13e0ee) bitcoin-core#4 mp::test::TestSetup::TestSetup(bool)::{lambda()bitcoin-core#1}::operator()() const::{lambda(mp::LogMessage)bitcoin-core#1}::operator()(mp::LogMessage) const test/mp/test/test.cpp:71 (mptest+0x13e0ee)
1 parent 7eb1da1 commit 9e2147b

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

include/mp/proxy-io.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class LoggingErrorHandler : public kj::TaskSet::ErrorHandler
9898
EventLoop& m_loop;
9999
};
100100

101+
//! Log flags. Update stringify function if changed!
101102
enum class Log {
102103
Trace = 0,
103104
Debug,
@@ -107,6 +108,8 @@ enum class Log {
107108
Raise,
108109
};
109110

111+
kj::StringPtr KJ_STRINGIFY(Log flags);
112+
110113
struct LogMessage {
111114

112115
//! Message to be logged

src/mp/proxy.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,16 @@ std::string LongThreadName(const char* exe_name)
430430
return g_thread_context.thread_name.empty() ? ThreadName(exe_name) : g_thread_context.thread_name;
431431
}
432432

433+
kj::StringPtr KJ_STRINGIFY(Log v)
434+
{
435+
switch (v) {
436+
case Log::Trace: return "Trace";
437+
case Log::Debug: return "Debug";
438+
case Log::Info: return "Info";
439+
case Log::Warning: return "Warning";
440+
case Log::Error: return "Error";
441+
case Log::Raise: return "Raise";
442+
}
443+
return "<Log?>";
444+
}
433445
} // namespace mp

test/mp/test/test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ class TestSetup
6767

6868
TestSetup(bool client_owns_connection = true)
6969
: thread{[&] {
70-
EventLoop loop("mptest", [](mp::LogMessage log_data) {
71-
std::cout << "LOG" << (int)log_data.level << ": " << log_data.message << "\n";
72-
if (log_data.level == mp::Log::Raise) throw std::runtime_error(log_data.message);
70+
EventLoop loop("mptest", [](mp::LogMessage log) {
71+
// Info logs are not printed by default, but will be shown with `mptest --verbose`
72+
KJ_LOG(INFO, log.level, log.message);
73+
if (log.level == mp::Log::Raise) throw std::runtime_error(log.message);
7374
});
7475
auto pipe = loop.m_io_context.provider->newTwoWayPipe();
7576

0 commit comments

Comments
 (0)