MatLogger2  1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
matlogger2.h
Go to the documentation of this file.
1 #ifndef __XBOT_MATLOGGER2_H__
2 #define __XBOT_MATLOGGER2_H__
3 
4 #include <string>
5 #include <memory>
6 #include <unordered_map>
7 
8 #include <eigen3/Eigen/Dense>
9 
11 
12 
13 
14 namespace XBot
15 {
16  namespace matlogger2
17  {
18  class Backend;
19  }
20 
66  class MatLogger2
67  {
68 
69  public:
70 
71 
72  typedef std::weak_ptr<MatLogger2> WeakPtr;
73  typedef std::shared_ptr<MatLogger2> Ptr;
74 
75  struct Options
76  {
79 
80  Options();
81  };
82 
91  template <typename... Args>
92  static Ptr MakeLogger(Args... args);
93 
97  const std::string& get_filename() const;
98 
103  Options get_options() const;
104 
109  void set_on_data_available_callback(VariableBuffer::CallbackType callback);
110 
118  void set_buffer_mode(VariableBuffer::Mode buffer_mode);
119 
132  bool create(const std::string& var_name,
133  int rows, int cols = 1,
134  int buffer_size = -1);
135 
136 
142  template <typename Derived>
143  bool add(const std::string& var_name, const Eigen::MatrixBase<Derived>& data);
144 
145  template <typename Scalar>
146  bool add(const std::string& var_name, const std::vector<Scalar>& data);
147 
148  template <typename Iterator>
149  // this overload may allocate a temporary vector!
150  bool add(const std::string& var_name, Iterator begin, Iterator end);
151 
152  bool add(const std::string& var_name, double data);
153 
159  int flush_available_data();
160 
165  ~MatLogger2();
166 
167  private:
168 
176  MatLogger2(std::string file,
177  Options opt = Options());
178 
184  bool flush_to_queue_all();
185 
190  VariableBuffer * find_or_create(const std::string& var_name,
191  int rows, int cols
192  );
193 
194 
195  // option struct
196  Options _opt;
197 
198  // protect non-const access to _vars
199  // producer must hold it during create(), and
200  // set_on_data_available_callback()
201  // consumer must hold it during flush_available_data()
202  class MutexImpl;
203  std::unique_ptr<MutexImpl> _vars_mutex;
204 
205  // map of all defined variables
206  std::unordered_map<std::string, VariableBuffer> _vars;
207 
208  // buffer mode
209  VariableBuffer::Mode _buffer_mode;
210 
211  // callback that all variables shall use to notify that a new block is available
212  VariableBuffer::CallbackType _on_block_available;
213 
214  // path to mat-file
215  std::string _file_name;
216 
217  // handle to backend object
218  std::unique_ptr<matlogger2::Backend> _backend;
219 
220 
221  };
222 
223 
224 
225 }
226 
227 template <typename... Args>
229 {
230  return Ptr(new MatLogger2(args...));
231 }
232 
233 template <typename Derived>
234 inline bool XBot::MatLogger2::add(const std::string& var_name, const Eigen::MatrixBase< Derived >& data)
235 {
236  VariableBuffer * vbuf = find_or_create(var_name, data.rows(), data.cols());
237 
238  return vbuf && vbuf->add_elem(data);
239 
240 }
241 
242 template<typename Iterator>
243 inline bool XBot::MatLogger2::add(const std::string& var_name, Iterator begin, Iterator end)
244 {
245  static std::vector<double> tmp;
246  tmp.clear();
247  tmp.assign(begin, end);
248 
249  return add(var_name, tmp);
250 
251 }
252 
253 
254 template <typename Scalar>
255 inline bool XBot::MatLogger2::add(const std::string& var_name, const std::vector<Scalar>& data)
256 {
257  Eigen::Map<const Eigen::Matrix<Scalar,-1,1>> map(data.data(), data.size());
258  return add(var_name, map);
259 }
260 
261 
262 #endif
Mode
Enum for specifying the type of buffer.
Definition: var_buffer.h:40
bool add_elem(const Eigen::MatrixBase< Derived > &data)
Add an element to the buffer.
Definition: var_buffer.h:260
std::weak_ptr< MatLogger2 > WeakPtr
Definition: matlogger2.h:72
static Ptr MakeLogger(Args...args)
Factory method that must be used to construct a MatLogger2 instance.
The MatLogger2 class allows the user to save numeric variables (scalars, vectors, matrices) to HDF5 M...
Definition: matlogger2.h:66
std::shared_ptr< MatLogger2 > Ptr
Definition: matlogger2.h:73
The VariableBuffer class implements a memory buffer for a single logged variable. ...
Definition: var_buffer.h:32
bool add(const std::string &var_name, const Eigen::MatrixBase< Derived > &data)
Add an element to an existing variable.
Definition: matlogger2.h:234
std::function< void(BufferInfo)> CallbackType
Definition: var_buffer.h:63