Skip to content

Commit 6f6c64e

Browse files
committed
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).
1 parent 0876f08 commit 6f6c64e

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

Diff for: doc/release/yarp_3_5/LogForwarder_static_Bottle.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
LogForwarder_static_Bottle {#yarp-3.5}
2+
--------------------------
3+
4+
### Libraries
5+
6+
#### `os`
7+
8+
* The `LogForwarder` no longer skips messages when messages are sent too
9+
quickly (#2643).

Diff for: src/libYARP_os/src/yarp/os/impl/LogForwarder.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ yarp::os::impl::LogForwarder::LogForwarder()
3737
if (!outputPort.open(logPortName)) {
3838
printf("LogForwarder error while opening port %s\n", logPortName.c_str());
3939
}
40-
outputPort.enableBackgroundWrite(true);
4140
outputPort.addOutput("/yarplogger", "fast_tcp");
4241

4342
started = true;
@@ -46,12 +45,12 @@ yarp::os::impl::LogForwarder::LogForwarder()
4645
void yarp::os::impl::LogForwarder::forward(const std::string& message)
4746
{
4847
mutex.lock();
49-
static Bottle b;
48+
Bottle& b = outputPort.prepare();
5049
b.clear();
5150
std::string port = "[" + outputPort.getName() + "]";
5251
b.addString(port);
5352
b.addString(message);
54-
outputPort.write(b);
53+
outputPort.writeStrict();
5554
mutex.unlock();
5655
}
5756

@@ -68,9 +67,7 @@ void yarp::os::impl::LogForwarder::shutdown()
6867

6968
yarp::os::impl::LogForwarder& fw = getInstance();
7069
fw.forward(ost.str());
71-
while (fw.outputPort.isWriting()) {
72-
yarp::os::SystemClock::delaySystem(0.2);
73-
}
70+
fw.outputPort.waitForWrite();
7471
fw.outputPort.interrupt();
7572
fw.outputPort.close();
7673
}

Diff for: src/libYARP_os/src/yarp/os/impl/LogForwarder.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#include <yarp/os/api.h>
1010

11-
#include <yarp/os/Port.h>
11+
#include <yarp/os/Bottle.h>
12+
#include <yarp/os/BufferedPort.h>
1213

1314
#include <mutex>
1415
#include <string>
@@ -32,7 +33,7 @@ class YARP_os_impl_API LogForwarder
3233
LogForwarder& operator=(LogForwarder const&) = delete;
3334

3435
std::mutex mutex;
35-
yarp::os::Port outputPort;
36+
yarp::os::BufferedPort<yarp::os::Bottle> outputPort;
3637
static bool started;
3738
};
3839

0 commit comments

Comments
 (0)