Skip to content

Commit ca3c05d

Browse files
committed
test: Use KJ_LOG instead 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 ca3c05d

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <kj/debug.h>
2525
#include <kj/function.h>
2626
#include <kj/memory.h>
27+
#include <kj/string.h>
2728
#include <map>
2829
#include <memory>
2930
#include <optional>
@@ -430,4 +431,16 @@ std::string LongThreadName(const char* exe_name)
430431
return g_thread_context.thread_name.empty() ? ThreadName(exe_name) : g_thread_context.thread_name;
431432
}
432433

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

test/mp/test/test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <exception>
1414
#include <functional>
1515
#include <future>
16-
#include <iostream>
1716
#include <kj/async.h>
1817
#include <kj/async-io.h>
1918
#include <kj/common.h>
@@ -67,9 +66,10 @@ class TestSetup
6766

6867
TestSetup(bool client_owns_connection = true)
6968
: 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);
69+
EventLoop loop("mptest", [](mp::LogMessage log) {
70+
// Info logs are not printed by default, but will be shown with `mptest --verbose`
71+
KJ_LOG(INFO, log.level, log.message);
72+
if (log.level == mp::Log::Raise) throw std::runtime_error(log.message);
7373
});
7474
auto pipe = loop.m_io_context.provider->newTwoWayPipe();
7575

0 commit comments

Comments
 (0)