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
RTLoggerStream emptyStream()
Return an empty stream object.
virtual void write(const char *c)=0
std::size_t frontIdx_
Index of the next value to be read.
boost::shared_ptr< LoggerStream > LoggerStreamPtr_t
std::vector< LoggerStreamPtr_t > outputs_
Main class of the real-time logger.
Stream for the real-time logger.
DYNAMIC_GRAPH_DLLAPI std::ostream & operator<<(std::ostream &os, const dynamicgraph::Entity &ent)
virtual void write(const char *c)
void addOutputStream(const LoggerStreamPtr_t &os)
boost::mutex wmutex
The writer mutex.
static RealTimeLogger * instance_


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03