MatLogger2
1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
|
The MatLogger2 class allows the user to save numeric variables (scalars, vectors, matrices) to HDF5 MAT-files. More...
#include <matlogger2.h>
Classes | |
class | MutexImpl |
struct | Options |
Public Types | |
typedef std::weak_ptr< MatLogger2 > | WeakPtr |
typedef std::shared_ptr< MatLogger2 > | Ptr |
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... | |
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.
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.
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.
|
inline |
Add an element to an existing variable.
Definition at line 234 of file matlogger2.h.
|
inline |
Definition at line 255 of file matlogger2.h.
|
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.
var_name | Variable name |
rows | Sample row size |
cols | Sample column size (defaults to 1) |
buffer_size | Buffer size in terms of number of elements (defaults to get_options().default_buffer_size) |
Definition at line 154 of file matlogger2.cpp.
int MatLogger2::flush_available_data | ( | ) |
Flush available data 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.
|
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.
|
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.