Commit bcb3ca77 authored by Joseph Mirabel's avatar Joseph Mirabel Committed by olivier stasse
Browse files

Enhance interface of RealTimeLogger and RTLoggerStream

parent 133c8819
...@@ -7,12 +7,10 @@ ...@@ -7,12 +7,10 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <boost/circular_buffer.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#include <dynamic-graph/config.hh> #include <dynamic-graph/config.hh>
#include <dynamic-graph/debug.h>
namespace dynamicgraph { namespace dynamicgraph {
/// \ingroup debug /// \ingroup debug
...@@ -50,22 +48,30 @@ class RealTimeLogger; ...@@ -50,22 +48,30 @@ class RealTimeLogger;
/// This class is only used by RealTimeLogger. /// This class is only used by RealTimeLogger.
class RTLoggerStream { class RTLoggerStream {
public: public:
RTLoggerStream(RealTimeLogger *logger, std::ostream &os) inline RTLoggerStream(RealTimeLogger *logger, std::ostream &os)
: logger_(logger), os_(os) {} : ok_(logger!=NULL), logger_(logger), os_(os) {}
template <typename T> inline RTLoggerStream &operator<<(T t) { template <typename T> inline RTLoggerStream &operator<<(T t) {
if (logger_ != NULL) if (ok_) os_ << t;
os_ << t;
return *this; return *this;
} }
inline RTLoggerStream &operator<<(std::ostream &(*pf)(std::ostream &)) { inline RTLoggerStream &operator<<(std::ostream &(*pf)(std::ostream &)) {
if (logger_ != NULL) if (ok_) os_ << pf;
os_ << pf;
return *this; return *this;
} }
~RTLoggerStream(); inline ~RTLoggerStream() {
if (ok_) {
os_ << std::ends;
logger_->frontReady();
}
}
inline bool isNull() {
return !ok_;
}
private: private:
const bool ok_;
RealTimeLogger *logger_; RealTimeLogger *logger_;
std::ostream &os_; std::ostream &os_;
}; };
...@@ -119,6 +125,11 @@ public: ...@@ -119,6 +125,11 @@ public:
/// The message is considered finished when the object is destroyed. /// The message is considered finished when the object is destroyed.
RTLoggerStream front(); RTLoggerStream front();
/// Return an empty stream object.
RTLoggerStream emptyStream() {
return RTLoggerStream(NULL, oss_);
}
inline void frontReady() { inline void frontReady() {
backIdx_ = (backIdx_ + 1) % buffer_.size(); backIdx_ = (backIdx_ + 1) % buffer_.size();
wmutex.unlock(); wmutex.unlock();
......
...@@ -59,12 +59,6 @@ RTLoggerStream RealTimeLogger::front() { ...@@ -59,12 +59,6 @@ RTLoggerStream RealTimeLogger::front() {
return RTLoggerStream(this, oss_); return RTLoggerStream(this, oss_);
} }
RTLoggerStream::~RTLoggerStream() {
os_ << std::ends;
if (logger_ != NULL)
logger_->frontReady();
}
struct RealTimeLogger::thread { struct RealTimeLogger::thread {
bool requestShutdown_; bool requestShutdown_;
int threadPolicy_; int threadPolicy_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment