Skip to content

Commit ffe1470

Browse files
committed
Logging: Use new logging macros
The Info log level is still used for all messages other than exceptions. Also remove now-unused log functions.
1 parent b1a297c commit ffe1470

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

include/mp/proxy-io.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class Logger
167167
std::ostringstream m_buffer;
168168
};
169169

170+
#define MP_LOGPLAIN(loop, ...) if (mp::Logger logger{(loop).m_log_opts, __VA_ARGS__}; logger) logger
171+
172+
#define MP_LOG(loop, ...) MP_LOGPLAIN(loop, __VA_ARGS__) << "{" << LongThreadName((loop).m_exe_name) << "} "
173+
170174
std::string LongThreadName(const char* exe_name);
171175

172176
//! Event loop implementation.
@@ -244,15 +248,6 @@ class EventLoop
244248
//! Check if loop should exit.
245249
bool done() const MP_REQUIRES(m_mutex);
246250

247-
Logger log()
248-
{
249-
Logger logger(m_log_opts, Log::Info);
250-
logger << "{" << LongThreadName(m_exe_name) << "} ";
251-
return logger;
252-
}
253-
Logger logPlain() { return {m_log_opts, Log::Info}; }
254-
Logger raise() { return {m_log_opts, Log::Raise}; }
255-
256251
//! Process name included in thread names so combined debug output from
257252
//! multiple processes is easier to understand.
258253
const char* m_exe_name;
@@ -676,7 +671,7 @@ std::unique_ptr<ProxyClient<InitInterface>> ConnectStream(EventLoop& loop, int f
676671
init_client = connection->m_rpc_system->bootstrap(ServerVatId().vat_id).castAs<InitInterface>();
677672
Connection* connection_ptr = connection.get();
678673
connection->onDisconnect([&loop, connection_ptr] {
679-
loop.log() << "IPC client: unexpected network disconnect.";
674+
MP_LOG(loop, Log::Info) << "IPC client: unexpected network disconnect.";
680675
delete connection_ptr;
681676
});
682677
});
@@ -699,7 +694,7 @@ void _Serve(EventLoop& loop, kj::Own<kj::AsyncIoStream>&& stream, InitImpl& init
699694
});
700695
auto it = loop.m_incoming_connections.begin();
701696
it->onDisconnect([&loop, it] {
702-
loop.log() << "IPC server: socket disconnected.";
697+
MP_LOG(loop, Log::Info) << "IPC server: socket disconnected.";
703698
loop.m_incoming_connections.erase(it);
704699
});
705700
}

include/mp/proxy-types.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ template <typename Client>
568568
void clientDestroy(Client& client)
569569
{
570570
if (client.m_context.connection) {
571-
client.m_context.loop->log() << "IPC client destroy " << typeid(client).name();
571+
MP_LOG(*client.m_context.loop, Log::Info) << "IPC client destroy " << typeid(client).name();
572572
} else {
573573
KJ_LOG(INFO, "IPC interrupted client destroy", typeid(client).name());
574574
}
@@ -577,7 +577,7 @@ void clientDestroy(Client& client)
577577
template <typename Server>
578578
void serverDestroy(Server& server)
579579
{
580-
server.m_context.loop->log() << "IPC server destroy " << typeid(server).name();
580+
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server destroy " << typeid(server).name();
581581
}
582582

583583
//! Entry point called by generated client code that looks like:
@@ -605,7 +605,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
605605
// declaration so the server method runs in a dedicated thread.
606606
assert(!g_thread_context.loop_thread);
607607
g_thread_context.waiter = std::make_unique<Waiter>();
608-
proxy_client.m_context.loop->logPlain()
608+
MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Info)
609609
<< "{" << g_thread_context.thread_name
610610
<< "} IPC client first request from current thread, constructing waiter";
611611
}
@@ -629,13 +629,13 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
629629
using FieldList = typename ProxyClientMethodTraits<typename Request::Params>::Fields;
630630
invoke_context.emplace(*proxy_client.m_context.connection, thread_context);
631631
IterateFields().handleChain(*invoke_context, request, FieldList(), typename FieldObjs::BuildParams{&fields}...);
632-
proxy_client.m_context.loop->logPlain()
632+
MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Info)
633633
<< "{" << thread_context.thread_name << "} IPC client send "
634634
<< TypeName<typename Request::Params>() << " " << LogEscape(request.toString(), proxy_client.m_context.loop->m_log_opts.max_chars);
635635

636636
proxy_client.m_context.loop->m_task_set->add(request.send().then(
637637
[&](::capnp::Response<typename Request::Results>&& response) {
638-
proxy_client.m_context.loop->logPlain()
638+
MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Info)
639639
<< "{" << thread_context.thread_name << "} IPC client recv "
640640
<< TypeName<typename Request::Results>() << " " << LogEscape(response.toString(), proxy_client.m_context.loop->m_log_opts.max_chars);
641641
try {
@@ -653,7 +653,7 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
653653
disconnected = "IPC client method call interrupted by disconnect.";
654654
} else {
655655
kj_exception = kj::str("kj::Exception: ", e).cStr();
656-
proxy_client.m_context.loop->logPlain()
656+
MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Info)
657657
<< "{" << thread_context.thread_name << "} IPC client exception " << kj_exception;
658658
}
659659
const Lock lock(thread_context.waiter->m_mutex);
@@ -665,8 +665,8 @@ void clientInvoke(ProxyClient& proxy_client, const GetRequest& get_request, Fiel
665665
Lock lock(thread_context.waiter->m_mutex);
666666
thread_context.waiter->wait(lock, [&done]() { return done; });
667667
if (exception) std::rethrow_exception(exception);
668-
if (!kj_exception.empty()) proxy_client.m_context.loop->raise() << kj_exception;
669-
if (disconnected) proxy_client.m_context.loop->raise() << disconnected;
668+
if (!kj_exception.empty()) MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Raise) << kj_exception;
669+
if (disconnected) MP_LOGPLAIN(*proxy_client.m_context.loop, Log::Raise) << disconnected;
670670
}
671671

672672
//! Invoke callable `fn()` that may return void. If it does return void, replace
@@ -700,7 +700,7 @@ kj::Promise<void> serverInvoke(Server& server, CallContext& call_context, Fn fn)
700700
using Results = typename decltype(call_context.getResults())::Builds;
701701

702702
int req = ++server_reqs;
703-
server.m_context.loop->log() << "IPC server recv request #" << req << " "
703+
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server recv request #" << req << " "
704704
<< TypeName<typename Params::Reads>() << " " << LogEscape(params.toString(), server.m_context.loop->m_log_opts.max_chars);
705705

706706
try {
@@ -717,14 +717,14 @@ kj::Promise<void> serverInvoke(Server& server, CallContext& call_context, Fn fn)
717717
return ReplaceVoid([&]() { return fn.invoke(server_context, ArgList()); },
718718
[&]() { return kj::Promise<CallContext>(kj::mv(call_context)); })
719719
.then([&server, req](CallContext call_context) {
720-
server.m_context.loop->log() << "IPC server send response #" << req << " " << TypeName<Results>()
720+
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server send response #" << req << " " << TypeName<Results>()
721721
<< " " << LogEscape(call_context.getResults().toString(), server.m_context.loop->m_log_opts.max_chars);
722722
});
723723
} catch (const std::exception& e) {
724-
server.m_context.loop->log() << "IPC server unhandled exception: " << e.what();
724+
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server unhandled exception: " << e.what();
725725
throw;
726726
} catch (...) {
727-
server.m_context.loop->log() << "IPC server unhandled exception";
727+
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server unhandled exception";
728728
throw;
729729
}
730730
}

include/mp/type-context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
150150
// thread.
151151
KJ_IF_MAYBE (thread_server, perhaps) {
152152
const auto& thread = static_cast<ProxyServer<Thread>&>(*thread_server);
153-
server.m_context.loop->log()
153+
MP_LOG(*server.m_context.loop, Log::Info)
154154
<< "IPC server post request #" << req << " {" << thread.m_thread_context.thread_name << "}";
155155
thread.m_thread_context.waiter->post(std::move(invoke));
156156
} else {
157-
server.m_context.loop->log()
157+
MP_LOG(*server.m_context.loop, Log::Info)
158158
<< "IPC server error request #" << req << ", missing thread to execute request";
159159
throw std::runtime_error("invalid thread handle");
160160
}

src/mp/proxy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ thread_local ThreadContext g_thread_context;
4242
void LoggingErrorHandler::taskFailed(kj::Exception&& exception)
4343
{
4444
KJ_LOG(ERROR, "Uncaught exception in daemonized task.", exception);
45-
m_loop.log() << "Uncaught exception in daemonized task.";
45+
MP_LOG(m_loop, Log::Info) << "Uncaught exception in daemonized task.";
4646
}
4747

4848
EventLoopRef::EventLoopRef(EventLoop& loop, Lock* lock) : m_loop(&loop), m_lock(lock)
@@ -251,9 +251,9 @@ void EventLoop::loop()
251251
break;
252252
}
253253
}
254-
log() << "EventLoop::loop done, cancelling event listeners.";
254+
MP_LOG(*this, Log::Info) << "EventLoop::loop done, cancelling event listeners.";
255255
m_task_set.reset();
256-
log() << "EventLoop::loop bye.";
256+
MP_LOG(*this, Log::Info) << "EventLoop::loop bye.";
257257
wait_stream = nullptr;
258258
KJ_SYSCALL(::close(post_fd));
259259
const Lock lock(m_mutex);

0 commit comments

Comments
 (0)