MatLogger2  1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
XBot::MatLogger2 Class Reference

The MatLogger2 class allows the user to save numeric variables (scalars, vectors, matrices) to HDF5 MAT-files. More...

#include <matlogger2.h>

+ Collaboration diagram for XBot::MatLogger2:

Classes

class  MutexImpl
 
struct  Options
 

Public Types

typedef std::weak_ptr< MatLogger2WeakPtr
 
typedef std::shared_ptr< MatLogger2Ptr
 

Public Member Functions

const std::string & get_filename () const
 Returns the full path associated with this logger object. More...
 
Options get_options () const
 Returns the MatLogger2::Options struct associated with this object. More...
 
void set_on_data_available_callback (VariableBuffer::CallbackType callback)
 Set the callback that is invoked whenever new data is available for writing to disk. More...
 
void set_buffer_mode (VariableBuffer::Mode buffer_mode)
 Set whether this buffer should be treated as a (possibly dual threaded) producer-consumer queue, or as a single-threaded circular buffer. More...
 
bool create (const std::string &var_name, int rows, int cols=1, int buffer_size=-1)
 Create a logged variable from its name as it will appear inside the MAT-file, dimensions, and buffer size. More...
 
template<typename Derived >
bool add (const std::string &var_name, const Eigen::MatrixBase< Derived > &data)
 Add an element to an existing variable. More...
 
template<typename Scalar >
bool add (const std::string &var_name, const std::vector< Scalar > &data)
 
template<typename Iterator >
bool add (const std::string &var_name, Iterator begin, Iterator end)
 
bool add (const std::string &var_name, double data)
 
int flush_available_data ()
 Flush available data to disk. More...
 
 ~MatLogger2 ()
 Destructor flushes all buffers to disk, then releases any resource connected with the underlying MAT-file. More...
 
template<typename... Args>
XBot::MatLogger2::Ptr MakeLogger (Args...args)
 

Static Public Member Functions

template<typename... Args>
static Ptr MakeLogger (Args...args)
 Factory method that must be used to construct a MatLogger2 instance. More...
 

Detailed Description

The MatLogger2 class allows the user to save numeric variables (scalars, vectors, matrices) to HDF5 MAT-files.

Output formatting:

Construction: via the MakeLogger(..) factory method (see the defined private constructors for the available signatures, e.g. MatLogger2(std::string filename).

Usage: variables are created via the create() method. Elements are added to a variable with the add() method. Different overloads are provided for Eigen3 types, std::vector, and scalars. Such elements are stored inside an internal buffer, which can be flushed to disk by calling flush_available_data(). It is possible to be notified whenever new data is available for flushing, by registering a callback through the set_on_data_available_callback() method.

Different functioning mode are available, see also set_buffer_mode()

Producer-consumer multi-threaded usage: Logged samples are pushed into a queue. The MatLogger2 class expects the user to periodically free such a queue by consuming its elements (flush_available_data()). If the buffer becomes full, some data can go lost. In producer-consumer mode, the MatLogger2 class can be used inside a multi-threaded environment, provided that the following constraints are satisfied: 1) only one "producer" thread shall call create() and add() 2) only one "consumer" thread shall call flush_available_data() The MatAppender class (see matlogger2/utils/mat_appender.h) provides a ready-to-use consumer thread that periodically writes available data to disk.

Circular-buffer single-threaded usage: Logged samples are pushed into a circular buffer. In circular-buffer mode, the user CAN NOT call flush_available_data(). If the buffer becomes full, older samples are overwritten. In circular-buffer mode, the MatLogger2 class can NOT be used inside a multi-threaded environment (i.e. additional synchronization must be provided by the user)

Definition at line 66 of file matlogger2.h.

Member Typedef Documentation

typedef std::shared_ptr<MatLogger2> XBot::MatLogger2::Ptr

Definition at line 73 of file matlogger2.h.

typedef std::weak_ptr<MatLogger2> XBot::MatLogger2::WeakPtr

Definition at line 72 of file matlogger2.h.

Constructor & Destructor Documentation

MatLogger2::~MatLogger2 ( )

Destructor flushes all buffers to disk, then releases any resource connected with the underlying MAT-file.

Definition at line 298 of file matlogger2.cpp.

Member Function Documentation

template<typename Derived >
bool XBot::MatLogger2::add ( const std::string &  var_name,
const Eigen::MatrixBase< Derived > &  data 
)
inline

Add an element to an existing variable.

Returns
True on success (variable exists)

Definition at line 234 of file matlogger2.h.

template<typename Scalar >
bool XBot::MatLogger2::add ( const std::string &  var_name,
const std::vector< Scalar > &  data 
)
inline

Definition at line 255 of file matlogger2.h.

template<typename Iterator >
bool XBot::MatLogger2::add ( const std::string &  var_name,
Iterator  begin,
Iterator  end 
)
inline

Definition at line 243 of file matlogger2.h.

bool MatLogger2::add ( const std::string &  var_name,
double  data 
)

Definition at line 200 of file matlogger2.cpp.

bool MatLogger2::create ( const std::string &  var_name,
int  rows,
int  cols = 1,
int  buffer_size = -1 
)

Create a logged variable from its name as it will appear inside the MAT-file, dimensions, and buffer size.

Parameters
var_nameVariable name
rowsSample row size
colsSample column size (defaults to 1)
buffer_sizeBuffer size in terms of number of elements (defaults to get_options().default_buffer_size)
Returns
True on success (variable name is unique, dimensions and buffer_size are > 0)

Definition at line 154 of file matlogger2.cpp.

int MatLogger2::flush_available_data ( )

Flush available data to disk.

Returns
The number of bytes that were written to disk.

Definition at line 211 of file matlogger2.cpp.

const std::string & MatLogger2::get_filename ( ) const

Returns the full path associated with this logger object.

Definition at line 119 of file matlogger2.cpp.

MatLogger2::Options MatLogger2::get_options ( ) const

Returns the MatLogger2::Options struct associated with this object.

Definition at line 124 of file matlogger2.cpp.

template<typename... Args>
static Ptr XBot::MatLogger2::MakeLogger ( Args...  args)
static

Factory method that must be used to construct a MatLogger2 instance.

If no exception is thrown, it returns a shared pointer to the constructed instance.

The function internally calls one of the available constructor overload.

template<typename... Args>
XBot::MatLogger2::Ptr XBot::MatLogger2::MakeLogger ( Args...  args)
inline

Definition at line 228 of file matlogger2.h.

void XBot::MatLogger2::set_buffer_mode ( VariableBuffer::Mode  buffer_mode)

Set whether this buffer should be treated as a (possibly dual threaded) producer-consumer queue, or as a single-threaded circular buffer.

By default, the producer_consumer mode is used.

NOTE: only call this method before starting using the logger!

Definition at line 141 of file matlogger2.cpp.

void MatLogger2::set_on_data_available_callback ( VariableBuffer::CallbackType  callback)

Set the callback that is invoked whenever new data is available for writing to disk.

Definition at line 129 of file matlogger2.cpp.


The documentation for this class was generated from the following files: