dynamic-graph  4.4.0
Dynamic graph library
real-time-logger-def.h
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 <sstream>
8 #include <vector>
9 
10 #include <boost/shared_ptr.hpp>
11 #include <boost/thread/mutex.hpp>
12 
13 #include <dynamic-graph/config.hh>
14 
15 namespace dynamicgraph {
23 class LoggerStream {
24 public:
25  virtual void write(const char *c) = 0;
26 };
27 
32 class LoggerIOStream : public LoggerStream {
33 public:
34  LoggerIOStream(std::ostream &os) : os_(os) {}
35  virtual ~LoggerIOStream() {}
36  virtual void write(const char *c) { os_ << c; }
37 
38 private:
39  std::ostream &os_;
40 };
41 typedef boost::shared_ptr<LoggerStream> LoggerStreamPtr_t;
42 
43 class RealTimeLogger;
44 
50 class RTLoggerStream {
51 public:
52  inline RTLoggerStream(RealTimeLogger *logger, std::ostream &os)
53  : ok_(logger != NULL), logger_(logger), os_(os) {}
54  template <typename T> inline RTLoggerStream &operator<<(T t) {
55  if (ok_)
56  os_ << t;
57  return *this;
58  }
59  inline RTLoggerStream &operator<<(std::ostream &(*pf)(std::ostream &)) {
60  if (ok_)
61  os_ << pf;
62  return *this;
63  }
64 
65  inline ~RTLoggerStream();
66 
67  inline bool isNull() { return !ok_; }
68 
69 private:
70  const bool ok_;
71  RealTimeLogger *logger_;
72  std::ostream &os_;
73 };
75 
99 class DYNAMIC_GRAPH_DLLAPI RealTimeLogger {
100 public:
101  static RealTimeLogger &instance();
102 
103  static void destroy();
104 
107  RealTimeLogger(const std::size_t &bufferSize);
108 
109  inline void clearOutputStreams() { outputs_.clear(); }
110 
111  inline void addOutputStream(const LoggerStreamPtr_t &os) {
112  outputs_.push_back(os);
113  }
114 
118  bool spinOnce();
119 
122  RTLoggerStream front();
123 
125  RTLoggerStream emptyStream() { return RTLoggerStream(NULL, oss_); }
126 
127  inline void frontReady() {
128  backIdx_ = (backIdx_ + 1) % buffer_.size();
129  wmutex.unlock();
130  }
131 
132  inline bool empty() const { return frontIdx_ == backIdx_; }
133 
134  inline bool full() const {
135  return ((backIdx_ + 1) % buffer_.size()) == frontIdx_;
136  }
137 
138  inline std::size_t size() const {
139  if (frontIdx_ <= backIdx_)
140  return backIdx_ - frontIdx_;
141  else
142  return backIdx_ + buffer_.size() - frontIdx_;
143  }
144 
145  inline std::size_t getBufferSize() { return buffer_.size(); }
146 
147  ~RealTimeLogger();
148 
149 private:
150  struct Data {
151  std::stringbuf buf;
152  };
153 
154  std::vector<LoggerStreamPtr_t> outputs_;
155  std::vector<Data *> buffer_;
157  std::size_t frontIdx_;
160  std::size_t backIdx_;
161  std::ostream oss_;
162 
164  boost::mutex wmutex;
165  std::size_t nbDiscarded_;
166 
167  struct thread;
168 
169  static RealTimeLogger *instance_;
170  static thread *thread_;
171 };
172 
173 RTLoggerStream::~RTLoggerStream() {
174  if (ok_) {
175  os_ << std::ends;
176  logger_->frontReady();
177  }
178 }
179 
180 } // end of namespace dynamicgraph
181 
182 #endif
Main class of the real-time logger.
Stream for the real-time logger.
RTLoggerStream emptyStream()
Return an empty stream object.