Skip to content

Commit e50802f

Browse files
committed
Add a verbose logging option and put expensive debug output behind it
Logging every send/receive/request/response is very expensive for structs due to the way they're stringified by capnproto. Introduce the concept of verbose logging, and expose it to clients by adding a new constructor for EventLoop which accepts LogOptions. For backwards compatibility, the existing constructor is unchanged. Additionally, for back compat, a new callback function is added rather than adding a parameter to the existing one.
1 parent 47d79db commit e50802f

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

include/mp/proxy-io.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,15 @@ struct LogOptions {
133133
//! External logging callback.
134134
LogFn log_fn;
135135

136+
//! External verbose logging callback.
137+
LogFn verbose_log_fn{nullptr};
138+
136139
//! Maximum number of characters to use when representing
137140
//! request and response structs as strings.
138141
size_t max_chars{200};
142+
143+
//! Enable verbose logging which may affect performance
144+
bool verbose{false};
139145
};
140146

141147
std::string LongThreadName(const char* exe_name);
@@ -170,6 +176,10 @@ class EventLoop
170176
public:
171177
//! Construct event loop object.
172178
EventLoop(const char* exe_name, LogFn log_fn, void* context = nullptr);
179+
180+
//! Construct event loop object with logging options.
181+
EventLoop(const char* exe_name, LogOptions log_opts, void* context = nullptr);
182+
173183
~EventLoop();
174184

175185
//! Run event loop. Does not return until shutdown. This should only be
@@ -210,13 +220,13 @@ class EventLoop
210220
//! Check if loop should exit.
211221
bool done() const MP_REQUIRES(m_mutex);
212222

213-
Logger log()
223+
Logger log(bool verbose = false)
214224
{
215-
Logger logger(false, m_log_opts.log_fn);
225+
Logger logger(false, verbose ? m_log_opts.verbose_log_fn : m_log_opts.log_fn);
216226
logger << "{" << LongThreadName(m_exe_name) << "} ";
217227
return logger;
218228
}
219-
Logger logPlain() { return {false, m_log_opts.log_fn}; }
229+
Logger logPlain(bool verbose = false) { return {false, verbose ? m_log_opts.verbose_log_fn : m_log_opts.log_fn}; }
220230
Logger raise() { return {true, m_log_opts.log_fn}; }
221231

222232
//! Process name included in thread names so combined debug output from

include/mp/proxy-types.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,19 @@ 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()
633-
<< "{" << thread_context.thread_name << "} IPC client send "
634-
<< TypeName<typename Request::Params>() << " " << LogEscape(request.toString(), proxy_client.m_context.loop->m_log_opts.max_chars);
632+
if (proxy_client.m_context.loop->m_log_opts.verbose) {
633+
proxy_client.m_context.loop->logPlain(true)
634+
<< "{" << thread_context.thread_name << "} IPC client send "
635+
<< TypeName<typename Request::Params>() << " " << LogEscape(request.toString(), proxy_client.m_context.loop->m_log_opts.max_chars);
636+
}
635637

636638
proxy_client.m_context.loop->m_task_set->add(request.send().then(
637639
[&](::capnp::Response<typename Request::Results>&& response) {
638-
proxy_client.m_context.loop->logPlain()
639-
<< "{" << thread_context.thread_name << "} IPC client recv "
640-
<< TypeName<typename Request::Results>() << " " << LogEscape(response.toString(), proxy_client.m_context.loop->m_log_opts.max_chars);
640+
if (proxy_client.m_context.loop->m_log_opts.verbose) {
641+
proxy_client.m_context.loop->logPlain(true)
642+
<< "{" << thread_context.thread_name << "} IPC client recv "
643+
<< TypeName<typename Request::Results>() << " " << LogEscape(response.toString(), proxy_client.m_context.loop->m_log_opts.max_chars);
644+
}
641645
try {
642646
IterateFields().handleChain(
643647
*invoke_context, response, FieldList(), typename FieldObjs::ReadResults{&fields}...);
@@ -700,8 +704,10 @@ kj::Promise<void> serverInvoke(Server& server, CallContext& call_context, Fn fn)
700704
using Results = typename decltype(call_context.getResults())::Builds;
701705

702706
int req = ++server_reqs;
703-
server.m_context.loop->log() << "IPC server recv request #" << req << " "
704-
<< TypeName<typename Params::Reads>() << " " << LogEscape(params.toString(), server.m_context.loop->m_log_opts.max_chars);
707+
if (server.m_context.loop->m_log_opts.verbose) {
708+
server.m_context.loop->log(true) << "IPC server recv request #" << req << " "
709+
<< TypeName<typename Params::Reads>() << " " << LogEscape(params.toString(), server.m_context.loop->m_log_opts.max_chars);
710+
}
705711

706712
try {
707713
using ServerContext = ServerInvokeContext<Server, CallContext>;
@@ -717,8 +723,10 @@ kj::Promise<void> serverInvoke(Server& server, CallContext& call_context, Fn fn)
717723
return ReplaceVoid([&]() { return fn.invoke(server_context, ArgList()); },
718724
[&]() { return kj::Promise<CallContext>(kj::mv(call_context)); })
719725
.then([&server, req](CallContext call_context) {
720-
server.m_context.loop->log() << "IPC server send response #" << req << " " << TypeName<Results>()
721-
<< " " << LogEscape(call_context.getResults().toString(), server.m_context.loop->m_log_opts.max_chars);
726+
if (server.m_context.loop->m_log_opts.verbose) {
727+
server.m_context.loop->log(true) << "IPC server send response #" << req << " " << TypeName<Results>()
728+
<< " " << LogEscape(call_context.getResults().toString(), server.m_context.loop->m_log_opts.max_chars);
729+
}
722730
});
723731
} catch (const std::exception& e) {
724732
server.m_context.loop->log() << "IPC server unhandled exception: " << e.what();

0 commit comments

Comments
 (0)