dynamic-graph  4.1.0-8-gdab7-dirty
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 void write(const char *c) { os_ << c; }
36 
37 private:
38  std::ostream &os_;
39 };
40 typedef boost::shared_ptr<LoggerStream> LoggerStreamPtr_t;
41 
42 class RealTimeLogger;
43 
49 class RTLoggerStream {
50 public:
51  inline RTLoggerStream(RealTimeLogger *logger, std::ostream &os)
52  : ok_(logger != NULL), logger_(logger), os_(os) {}
53  template <typename T> inline RTLoggerStream &operator<<(T t) {
54  if (ok_)
55  os_ << t;
56  return *this;
57  }
58  inline RTLoggerStream &operator<<(std::ostream &(*pf)(std::ostream &)) {
59  if (ok_)
60  os_ << pf;
61  return *this;
62  }
63 
64  inline ~RTLoggerStream();
65 
66  inline bool isNull() { return !ok_; }
67 
68 private:
69  const bool ok_;
70  RealTimeLogger *logger_;
71  std::ostream &os_;
72 };
74 
98 class DYNAMIC_GRAPH_DLLAPI RealTimeLogger {
99 public:
100  static RealTimeLogger &instance();
101 
102  static void destroy();
103 
106  RealTimeLogger(const std::size_t &bufferSize);
107 
108  inline void clearOutputStreams() { outputs_.clear(); }
109 
110  inline void addOutputStream(const LoggerStreamPtr_t &os) {
111  outputs_.push_back(os);
112  }
113 
117  bool spinOnce();
118 
121  RTLoggerStream front();
122 
124  RTLoggerStream emptyStream() { return RTLoggerStream(NULL, oss_); }
125 
126  inline void frontReady() {
127  backIdx_ = (backIdx_ + 1) % buffer_.size();
128  wmutex.unlock();
129  }
130 
131  inline bool empty() const { return frontIdx_ == backIdx_; }
132 
133  inline bool full() const {
134  return ((backIdx_ + 1) % buffer_.size()) == frontIdx_;
135  }
136 
137  inline std::size_t size() const {
138  if (frontIdx_ <= backIdx_)
139  return backIdx_ - frontIdx_;
140  else
141  return backIdx_ + buffer_.size() - frontIdx_;
142  }
143 
144  inline std::size_t getBufferSize() { return buffer_.size(); }
145 
146  ~RealTimeLogger();
147 
148 private:
149  struct Data {
150  std::stringbuf buf;
151  };
152 
153  std::vector<LoggerStreamPtr_t> outputs_;
154  std::vector<Data *> buffer_;
156  std::size_t frontIdx_;
159  std::size_t backIdx_;
160  std::ostream oss_;
161 
163  boost::mutex wmutex;
164  std::size_t nbDiscarded_;
165 
166  struct thread;
167 
168  static RealTimeLogger *instance_;
169  static thread *thread_;
170 };
171 
172 RTLoggerStream::~RTLoggerStream() {
173  if (ok_) {
174  os_ << std::ends;
175  logger_->frontReady();
176  }
177 }
178 
179 } // end of namespace dynamicgraph
180 
181 #endif
Main class of the real-time logger.
Stream for the real-time logger.
RTLoggerStream emptyStream()
Return an empty stream object.