From 6f6c64e8fe113337b836e4e0a546ef47a5a71db4 Mon Sep 17 00:00:00 2001 From: "Daniele E. Domenichelli" Date: Tue, 27 Jul 2021 12:48:49 +0200 Subject: [PATCH] os/impl/LogForwarder: Do not use a static Bottle When the `forward` method is called twice, the `Bottle::clear()` method could be called before the message is sent, hence deleting the bottle being sent. This fixes the `LogForwarder` skipping messages when messages are sent too quickly (Fixes #2643). --- doc/release/yarp_3_5/LogForwarder_static_Bottle.md | 9 +++++++++ src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp | 9 +++------ src/libYARP_os/src/yarp/os/impl/LogForwarder.h | 5 +++-- 3 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 doc/release/yarp_3_5/LogForwarder_static_Bottle.md diff --git a/doc/release/yarp_3_5/LogForwarder_static_Bottle.md b/doc/release/yarp_3_5/LogForwarder_static_Bottle.md new file mode 100644 index 00000000000..f4ab56727eb --- /dev/null +++ b/doc/release/yarp_3_5/LogForwarder_static_Bottle.md @@ -0,0 +1,9 @@ +LogForwarder_static_Bottle {#yarp-3.5} +-------------------------- + +### Libraries + +#### `os` + +* The `LogForwarder` no longer skips messages when messages are sent too + quickly (#2643). diff --git a/src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp b/src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp index b1b11f9d9f8..beaf80e7582 100644 --- a/src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp +++ b/src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp @@ -37,7 +37,6 @@ yarp::os::impl::LogForwarder::LogForwarder() if (!outputPort.open(logPortName)) { printf("LogForwarder error while opening port %s\n", logPortName.c_str()); } - outputPort.enableBackgroundWrite(true); outputPort.addOutput("/yarplogger", "fast_tcp"); started = true; @@ -46,12 +45,12 @@ yarp::os::impl::LogForwarder::LogForwarder() void yarp::os::impl::LogForwarder::forward(const std::string& message) { mutex.lock(); - static Bottle b; + Bottle& b = outputPort.prepare(); b.clear(); std::string port = "[" + outputPort.getName() + "]"; b.addString(port); b.addString(message); - outputPort.write(b); + outputPort.writeStrict(); mutex.unlock(); } @@ -68,9 +67,7 @@ void yarp::os::impl::LogForwarder::shutdown() yarp::os::impl::LogForwarder& fw = getInstance(); fw.forward(ost.str()); - while (fw.outputPort.isWriting()) { - yarp::os::SystemClock::delaySystem(0.2); - } + fw.outputPort.waitForWrite(); fw.outputPort.interrupt(); fw.outputPort.close(); } diff --git a/src/libYARP_os/src/yarp/os/impl/LogForwarder.h b/src/libYARP_os/src/yarp/os/impl/LogForwarder.h index 28188bd7e8c..52c7758b9e7 100644 --- a/src/libYARP_os/src/yarp/os/impl/LogForwarder.h +++ b/src/libYARP_os/src/yarp/os/impl/LogForwarder.h @@ -8,7 +8,8 @@ #include -#include +#include +#include #include #include @@ -32,7 +33,7 @@ class YARP_os_impl_API LogForwarder LogForwarder& operator=(LogForwarder const&) = delete; std::mutex mutex; - yarp::os::Port outputPort; + yarp::os::BufferedPort outputPort; static bool started; };