real-time-logger-def.h
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 // Copyright 2018, Joseph Mirabel LAAS-CNRS
3 //
4 
5 #ifndef DYNAMIC_GRAPH_LOGGER_REAL_TIME_DEF_H
6 #define DYNAMIC_GRAPH_LOGGER_REAL_TIME_DEF_H
7 #include <boost/shared_ptr.hpp>
8 #include <boost/thread/mutex.hpp>
9 #include <dynamic-graph/config.hh>
10 #include <sstream>
11 #include <vector>
12 
13 namespace dynamicgraph {
21 class LoggerStream {
22  public:
23  virtual void write(const char *c) = 0;
24 };
25 
30 class LoggerIOStream : public LoggerStream {
31  public:
32  LoggerIOStream(std::ostream &os) : os_(os) {}
33  virtual ~LoggerIOStream() {}
34  virtual void write(const char *c) { os_ << c; }
35 
36  private:
37  std::ostream &os_;
38 };
39 typedef boost::shared_ptr<LoggerStream> LoggerStreamPtr_t;
40 
41 class RealTimeLogger;
42 
48 class RTLoggerStream {
49  public:
50  inline RTLoggerStream(RealTimeLogger *logger, std::ostream &os)
51  : ok_(logger != NULL), logger_(logger), os_(os) {}
52  template <typename T>
53  inline RTLoggerStream &operator<<(T t) {
54  if (ok_) os_ << t;
55  return *this;
56  }
57  inline RTLoggerStream &operator<<(std::ostream &(*pf)(std::ostream &)) {
58  if (ok_) os_ << pf;
59  return *this;
60  }
61 
62  inline ~RTLoggerStream();
63 
64  inline bool isNull() { return !ok_; }
65 
66  private:
67  const bool ok_;
68  RealTimeLogger *logger_;
69  std::ostream &os_;
70 };
72 
96 class DYNAMIC_GRAPH_DLLAPI RealTimeLogger {
97  public:
98  static RealTimeLogger &instance();
99 
100  static void destroy();
101 
104  RealTimeLogger(const std::size_t &bufferSize);
105 
106  inline void clearOutputStreams() { outputs_.clear(); }
107 
108  inline void addOutputStream(const LoggerStreamPtr_t &os) {
109  outputs_.push_back(os);
110  }
111 
115  bool spinOnce();
116 
119  RTLoggerStream front();
120 
122  RTLoggerStream emptyStream() { return RTLoggerStream(NULL, oss_); }
123 
124  inline void frontReady() {
125  backIdx_ = (backIdx_ + 1) % buffer_.size();
126  wmutex.unlock();
127  }
128 
129  inline bool empty() const { return frontIdx_ == backIdx_; }
130 
131  inline bool full() const {
132  return ((backIdx_ + 1) % buffer_.size()) == frontIdx_;
133  }
134 
135  inline std::size_t size() const {
136  if (frontIdx_ <= backIdx_)
137  return backIdx_ - frontIdx_;
138  else
139  return backIdx_ + buffer_.size() - frontIdx_;
140  }
141 
142  inline std::size_t getBufferSize() { return buffer_.size(); }
143 
144  ~RealTimeLogger();
145 
146  private:
147  struct Data {
148  std::stringbuf buf;
149  };
150 
151  std::vector<LoggerStreamPtr_t> outputs_;
152  std::vector<Data *> buffer_;
154  std::size_t frontIdx_;
157  std::size_t backIdx_;
158  std::ostream oss_;
159 
161  boost::mutex wmutex;
162  std::size_t nbDiscarded_;
163 
164  struct thread;
165 
166  static RealTimeLogger *instance_;
167  static thread *thread_;
168 };
169 
170 RTLoggerStream::~RTLoggerStream() {
171  if (ok_) {
172  os_ << std::ends;
173  logger_->frontReady();
174  }
175 }
176 
177 } // end of namespace dynamicgraph
178 
179 #endif
dynamicgraph::operator<<
DYNAMIC_GRAPH_DLLAPI std::ostream & operator<<(std::ostream &os, const dynamicgraph::Entity &ent)
Definition: src/dgraph/entity.cpp:174
dynamicgraph::RealTimeLogger::nbDiscarded_
std::size_t nbDiscarded_
Definition: real-time-logger-def.h:162
dynamicgraph::RealTimeLogger::emptyStream
RTLoggerStream emptyStream()
Return an empty stream object.
Definition: real-time-logger-def.h:122
dynamicgraph::RealTimeLogger::frontIdx_
std::size_t frontIdx_
Index of the next value to be read.
Definition: real-time-logger-def.h:154
dynamicgraph::RealTimeLogger::buffer_
std::vector< Data * > buffer_
Definition: real-time-logger-def.h:152
dynamicgraph::RealTimeLogger::Data::buf
std::stringbuf buf
Definition: real-time-logger-def.h:148
dynamicgraph
dynamicgraph::LoggerStream
Stream for the real-time logger.
Definition: real-time-logger-def.h:21
dynamicgraph::RealTimeLogger::size
std::size_t size() const
Definition: real-time-logger-def.h:135
dynamicgraph::RealTimeLogger::outputs_
std::vector< LoggerStreamPtr_t > outputs_
Definition: real-time-logger-def.h:151
dynamicgraph::RealTimeLogger
Main class of the real-time logger.
Definition: real-time-logger-def.h:96
dynamicgraph::RealTimeLogger::frontReady
void frontReady()
Definition: real-time-logger-def.h:124
dynamicgraph::RealTimeLogger::getBufferSize
std::size_t getBufferSize()
Definition: real-time-logger-def.h:142
dynamicgraph::LoggerIOStream::write
virtual void write(const char *c)
Definition: real-time-logger-def.h:34
dynamicgraph::RealTimeLogger::oss_
std::ostream oss_
Definition: real-time-logger-def.h:158
dynamicgraph::LoggerIOStream
Definition: real-time-logger-def.h:30
dynamicgraph::RealTimeLogger::Data
Definition: real-time-logger-def.h:147
dynamicgraph::RealTimeLogger::thread_
static thread * thread_
Definition: real-time-logger-def.h:167
dynamicgraph::LoggerStream::write
virtual void write(const char *c)=0
dynamicgraph::RealTimeLogger::empty
bool empty() const
Definition: real-time-logger-def.h:129
dynamicgraph::LoggerStreamPtr_t
boost::shared_ptr< LoggerStream > LoggerStreamPtr_t
Definition: real-time-logger-def.h:39
dynamicgraph::RealTimeLogger::addOutputStream
void addOutputStream(const LoggerStreamPtr_t &os)
Definition: real-time-logger-def.h:108
dynamicgraph::RealTimeLogger::wmutex
boost::mutex wmutex
The writer mutex.
Definition: real-time-logger-def.h:161
dynamicgraph::RealTimeLogger::instance_
static RealTimeLogger * instance_
Definition: real-time-logger-def.h:164
dynamicgraph::LoggerIOStream::~LoggerIOStream
virtual ~LoggerIOStream()
Definition: real-time-logger-def.h:33
dynamicgraph::LoggerIOStream::os_
std::ostream & os_
Definition: real-time-logger-def.h:37
dynamicgraph::RealTimeLogger::clearOutputStreams
void clearOutputStreams()
Definition: real-time-logger-def.h:106
dynamicgraph::RealTimeLogger::thread
Definition: src/debug/real-time-logger.cpp:62
dynamicgraph::RealTimeLogger::full
bool full() const
Definition: real-time-logger-def.h:131
dynamicgraph::RealTimeLogger::backIdx_
std::size_t backIdx_
Definition: real-time-logger-def.h:157
dynamicgraph::LoggerIOStream::LoggerIOStream
LoggerIOStream(std::ostream &os)
Definition: real-time-logger-def.h:32


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38