Skip to content

Commit 4aeb686

Browse files
committed
Logging: Pass LogData struct to logging callback
The previous logging callback signature is deprecated and support will be removed in a future release. Also adds a <functional> include to calculator example to make IWYU happy.
1 parent 53d74d2 commit 4aeb686

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

example/calculator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <charconv>
1010
#include <cstring>
1111
#include <fstream>
12+
#include <functional>
1213
#include <iostream>
1314
#include <kj/async.h>
1415
#include <kj/common.h>
@@ -37,6 +38,7 @@ class InitImpl : public Init
3738
}
3839
};
3940

41+
// Exercises deprecated log callback signature
4042
static void LogPrint(bool raise, const std::string& message)
4143
{
4244
if (raise) throw std::runtime_error(message);

example/example.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ static auto Spawn(mp::EventLoop& loop, const std::string& process_argv0, const s
3535
return std::make_tuple(mp::ConnectStream<InitInterface>(loop, fd), pid);
3636
}
3737

38-
static void LogPrint(bool raise, const std::string& message)
38+
static void LogPrint(mp::LogMessage log_data)
3939
{
40-
if (raise) throw std::runtime_error(message);
41-
std::ofstream("debug.log", std::ios_base::app) << message << std::endl;
40+
if (log_data.level == mp::Log::Raise) throw std::runtime_error(log_data.message);
41+
std::ofstream("debug.log", std::ios_base::app) << log_data.message << std::endl;
4242
}
4343

4444
int main(int argc, char** argv)

example/printer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class InitImpl : public Init
3232
std::unique_ptr<Printer> makePrinter() override { return std::make_unique<PrinterImpl>(); }
3333
};
3434

35-
static void LogPrint(bool raise, const std::string& message)
35+
static void LogPrint(mp::LogMessage log_data)
3636
{
37-
if (raise) throw std::runtime_error(message);
38-
std::ofstream("debug.log", std::ios_base::app) << message << std::endl;
37+
if (log_data.level == mp::Log::Raise) throw std::runtime_error(log_data.message);
38+
std::ofstream("debug.log", std::ios_base::app) << log_data.message << std::endl;
3939
}
4040

4141
int main(int argc, char** argv)

include/mp/proxy-io.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,16 @@ enum class Log {
107107
Raise,
108108
};
109109

110-
using LogFn = std::function<void(bool raise, std::string message)>;
110+
struct LogMessage {
111+
112+
//! Message to be logged
113+
std::string message;
114+
115+
//! The severity level of this message
116+
Log level;
117+
};
118+
119+
using LogFn = std::function<void(LogMessage)>;
111120

112121
struct LogOptions {
113122

@@ -135,7 +144,7 @@ class Logger
135144

136145
~Logger() noexcept(false)
137146
{
138-
if (enabled()) m_options.log_fn(m_log_level == Log::Raise, m_buffer.str());
147+
if (enabled()) m_options.log_fn({std::move(m_buffer).str(), m_log_level});
139148
}
140149

141150
template <typename T>
@@ -208,6 +217,12 @@ class EventLoop
208217
//! Construct event loop object with specified logging options.
209218
EventLoop(const char* exe_name, LogOptions log_opts, void* context = nullptr);
210219

220+
//! Backwards-compatible constructor for previous (deprecated) logging callback signature
221+
EventLoop(const char* exe_name, std::function<void(bool, std::string)> old_callback, void* context = nullptr)
222+
: EventLoop(exe_name,
223+
LogFn{[&](LogMessage log_data) {old_callback(log_data.level == Log::Raise, std::move(log_data.message));}},
224+
context){}
225+
211226
~EventLoop();
212227

213228
//! Run event loop. Does not return until shutdown. This should only be

test/mp/test/test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class TestSetup
6060

6161
TestSetup(bool client_owns_connection = true)
6262
: thread{[&] {
63-
EventLoop loop("mptest", [](bool raise, const std::string& log) {
64-
std::cout << "LOG" << raise << ": " << log << "\n";
65-
if (raise) throw std::runtime_error(log);
63+
EventLoop loop("mptest", [](mp::LogMessage log_data) {
64+
std::cout << "LOG" << (int)log_data.level << ": " << log_data.message << "\n";
65+
if (log_data.level == mp::Log::Raise) throw std::runtime_error(log_data.message);
6666
});
6767
auto pipe = loop.m_io_context.provider->newTwoWayPipe();
6868

0 commit comments

Comments
 (0)