|
30 | 30 | namespace ipc { |
31 | 31 | namespace capnp { |
32 | 32 | namespace { |
33 | | -void IpcLogFn(bool raise, std::string message) |
| 33 | + |
| 34 | +BCLog::Level ConvertIPCLogLevel(mp::Log level) |
| 35 | +{ |
| 36 | + switch (level) { |
| 37 | + case mp::Log::Trace: return BCLog::Level::Trace; |
| 38 | + case mp::Log::Debug: return BCLog::Level::Debug; |
| 39 | + case mp::Log::Info: return BCLog::Level::Info; |
| 40 | + case mp::Log::Warning: return BCLog::Level::Warning; |
| 41 | + case mp::Log::Error: return BCLog::Level::Error; |
| 42 | + case mp::Log::Raise: return BCLog::Level::Error; |
| 43 | + } // no default case, so the compiler can warn about missing cases |
| 44 | + |
| 45 | + // Be conservative and assume that if MP ever adds a new log level, it |
| 46 | + // should only be shown at our most verbose level. |
| 47 | + return BCLog::Level::Trace; |
| 48 | +} |
| 49 | + |
| 50 | +mp::Log GetRequestedIPCLogLevel() |
| 51 | +{ |
| 52 | + if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Trace)) return mp::Log::Trace; |
| 53 | + if (LogAcceptCategory(BCLog::IPC, BCLog::Level::Debug)) return mp::Log::Debug; |
| 54 | + |
| 55 | + // Info, Warning, and Error are logged unconditionally |
| 56 | + return mp::Log::Info; |
| 57 | +} |
| 58 | + |
| 59 | +void IpcLogFn(mp::LogMessage message) |
34 | 60 | { |
35 | | - LogDebug(BCLog::IPC, "%s\n", message); |
36 | | - if (raise) throw Exception(message); |
| 61 | + LogPrintLevel(BCLog::IPC, ConvertIPCLogLevel(message.level), "%s\n", message.message); |
| 62 | + if (message.level == mp::Log::Raise) throw Exception(message.message); |
37 | 63 | } |
38 | 64 |
|
39 | 65 | class CapnpProtocol : public Protocol |
@@ -62,7 +88,11 @@ class CapnpProtocol : public Protocol |
62 | 88 | { |
63 | 89 | assert(!m_loop); |
64 | 90 | mp::g_thread_context.thread_name = mp::ThreadName(exe_name); |
65 | | - m_loop.emplace(exe_name, &IpcLogFn, &m_context); |
| 91 | + mp::LogOptions opts = { |
| 92 | + .log_fn = IpcLogFn, |
| 93 | + .log_level = GetRequestedIPCLogLevel() |
| 94 | + }; |
| 95 | + m_loop.emplace(exe_name, std::move(opts), &m_context); |
66 | 96 | if (ready_fn) ready_fn(); |
67 | 97 | mp::ServeStream<messages::Init>(*m_loop, fd, init); |
68 | 98 | m_parent_connection = &m_loop->m_incoming_connections.back(); |
@@ -90,7 +120,11 @@ class CapnpProtocol : public Protocol |
90 | 120 | std::promise<void> promise; |
91 | 121 | m_loop_thread = std::thread([&] { |
92 | 122 | util::ThreadRename("capnp-loop"); |
93 | | - m_loop.emplace(exe_name, &IpcLogFn, &m_context); |
| 123 | + mp::LogOptions opts = { |
| 124 | + .log_fn = IpcLogFn, |
| 125 | + .log_level = GetRequestedIPCLogLevel() |
| 126 | + }; |
| 127 | + m_loop.emplace(exe_name, std::move(opts), &m_context); |
94 | 128 | m_loop_ref.emplace(*m_loop); |
95 | 129 | promise.set_value(); |
96 | 130 | m_loop->loop(); |
|
0 commit comments