Skip to content

Commit

Permalink
os/impl/LogForwarder: Do not use a static Bottle
Browse files Browse the repository at this point in the history
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 robotology#2643).
  • Loading branch information
drdanz committed Aug 2, 2021
1 parent fb23c99 commit 63a1e8e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
9 changes: 9 additions & 0 deletions doc/release/yarp_3_5/LogForwarder_static_Bottle.md
Original file line number Diff line number Diff line change
@@ -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).
9 changes: 3 additions & 6 deletions src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions src/libYARP_os/src/yarp/os/impl/LogForwarder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#include <yarp/os/api.h>

#include <yarp/os/Port.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/BufferedPort.h>

#include <mutex>
#include <string>
Expand All @@ -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<yarp::os::Bottle> outputPort;
static bool started;
};

Expand Down

0 comments on commit 63a1e8e

Please sign in to comment.