mc_rtc::Logger Struct Reference

Logs controller data to disk. More...

#include <mc_rtc/log/Logger.h>

Classes

struct  GUIEvent
 GUI callback event. More...
 
struct  KeyAddedEvent
 Data for a key added event. More...
 
struct  KeyRemovedEvent
 Data for a key removed event. More...
 
struct  Meta
 Log meta data written in the first call to log after a call to \start. More...
 
struct  StartEvent
 Start event. More...
 

Public Types

enum  Policy { Policy::NON_THREADED = 0, Policy::THREADED = 1 }
 Defines available policies for the logger. More...
 
typedef std::function< void(mc_rtc::MessagePackBuilder &)> serialize_fn
 
using LogEvent = std::variant< KeyAddedEvent, KeyRemovedEvent, GUIEvent, StartEvent >
 

Public Member Functions

 Logger (const Policy &policy, const std::string &directory, const std::string &tmpl)
 Constructor. More...
 
 ~Logger ()
 Destructor. More...
 
void setup (const Policy &policy, const std::string &directory, const std::string &tmpl)
 Setup the constructor configuration. More...
 
Metameta () noexcept
 Access the log's metadata. More...
 
const Metameta () const noexcept
 Access the log's metadata (const) More...
 
void start (const std::string &ctl_name, double timestep, bool resume=false, double start_t=0.0)
 Start logging. More...
 
void open (const std::string &file, double timestep, double start_t=0.0)
 Open a new file for logging. More...
 
void log ()
 Log controller's data. More...
 
template<typename CallbackT , typename SourceT = void, typename std::enable_if< mc_rtc::log::callback_is_serializable< CallbackT >::value, int >::type = 0>
void addLogEntry (const std::string &name, const SourceT *source, CallbackT &&get_fn, bool overwrite=false)
 
template<typename MemberPtrT , MemberPtrT member, typename SourceT , typename std::enable_if< mc_rtc::log::is_serializable_member< MemberPtrT >::value, int >::type = 0>
void addLogEntry (const std::string &name, const SourceT *source, bool overwrite=false)
 
template<typename MethodPtrT , MethodPtrT method, typename SourceT , typename std::enable_if< mc_rtc::log::is_serializable_getter< MethodPtrT >::value, int >::type = 0>
void addLogEntry (const std::string &name, const SourceT *source, bool overwrite=false)
 
template<typename T , typename std::enable_if< mc_rtc::log::callback_is_serializable< T >::value, int >::type = 0>
void addLogEntry (const std::string &name, T &&get_fn, bool overwrite=false)
 
template<typename SourceT , typename CallbackT , typename... Args>
void addLogEntries (const SourceT *source, const std::string &name, CallbackT &&get_fn, Args &&... args)
 
void addGUIEvent (GUIEvent &&event)
 
void removeLogEntry (const std::string &name)
 
void removeLogEntries (const void *source)
 
double t () const
 
const std::string & path () const
 
void flush ()
 
size_t size () const
 
void clear (bool record=true)
 

Static Public Attributes

static const uint8_t magic [4]
 
static const uint8_t version
 

Detailed Description

Logs controller data to disk.

The user can select a desired logging policy that will impact the behaviour of this class.

See mc_rtc::Logger::Policy documentation for details on available policies

Member Typedef Documentation

◆ LogEvent

◆ serialize_fn

A function that fills LogData vectors

Member Enumeration Documentation

◆ Policy

Defines available policies for the logger.

Enumerator
NON_THREADED 

Non-threaded policy.

Using this policy, the logging disk operations are done in the same thread as the global controller running thread, this is well suited for simulations and environments where you can guarantee a very fast access to the disk (e.g. logging to a ramdisk)

THREADED 

Threaded policy.

Using this policy, the logging disk operations are done in a separate thread from the global controller running thread. As a result, some buffering occurs and you might lose some data if the controller crashes. This is intended for real-time environments.

Constructor & Destructor Documentation

◆ Logger()

mc_rtc::Logger::Logger ( const Policy policy,
const std::string &  directory,
const std::string &  tmpl 
)

Constructor.

Parameters
policyThe chosen logging policy
directoryPath to the directory where log files will be stored
tmplLog file template

◆ ~Logger()

mc_rtc::Logger::~Logger ( )

Destructor.

Member Function Documentation

◆ addGUIEvent()

void mc_rtc::Logger::addGUIEvent ( GUIEvent &&  event)
inline

Add a GUI event to the log

Parameters
eventEvent being added to the log

◆ addLogEntries()

template<typename SourceT , typename CallbackT , typename... Args>
void mc_rtc::Logger::addLogEntries ( const SourceT *  source,
const std::string &  name,
CallbackT &&  get_fn,
Args &&...  args 
)
inline

Add multiple entries at once with the same entry

See also
addLogEntry for requirements on the callbacks
Parameters
sourceSource of the log entry
nameName of the log entry
get_fnA function that provides data that should be logged
argsA serie of name, get_fn values

◆ addLogEntry() [1/4]

template<typename MemberPtrT , MemberPtrT member, typename SourceT , typename std::enable_if< mc_rtc::log::is_serializable_member< MemberPtrT >::value, int >::type = 0>
void mc_rtc::Logger::addLogEntry ( const std::string &  name,
const SourceT *  source,
bool  overwrite = false 
)
inline

Add a log entry from a source and a compile-time pointer to member

This is slightly more efficient than the source + pointer to member version at the cost of annoying syntax, typically:

logger.addLogEntry<decltype(&Source::member), &Source::member>("name", &source);
Parameters
nameName of the log entry
sourceSource of the log entry
overwriteIf true, overwrite the previous entry of the same name (if any), otherwise log and return

◆ addLogEntry() [2/4]

template<typename MethodPtrT , MethodPtrT method, typename SourceT , typename std::enable_if< mc_rtc::log::is_serializable_getter< MethodPtrT >::value, int >::type = 0>
void mc_rtc::Logger::addLogEntry ( const std::string &  name,
const SourceT *  source,
bool  overwrite = false 
)
inline

Add a log entry from a source and a compile-time pointer to method

This is slightly more efficient than the source + pointer to method version at the cost of annoying syntax, typically:

logger.addLogEntry<decltype(&Source::method), &Source::method>("name", &source);
Parameters
nameName of the log entry
sourceSource of the log entry
overwriteIf true, overwrite the previous entry of the same name (if any), otherwise log and return

◆ addLogEntry() [3/4]

template<typename CallbackT , typename SourceT = void, typename std::enable_if< mc_rtc::log::callback_is_serializable< CallbackT >::value, int >::type = 0>
void mc_rtc::Logger::addLogEntry ( const std::string &  name,
const SourceT *  source,
CallbackT &&  get_fn,
bool  overwrite = false 
)
inline

Add a log entry into the log with the provided source

This function only accepts callable objects that returns a l/rvalue to a serializable object.

The log entry can be removed by calling:

The later remove all other log entries from the same source

Parameters
nameName of the log entry, this should be unique at any given time but the same key can be re-used during the logger's life
sourceSource of the log entry
get_fnA function that provides data that should be logged
overwriteIf true, overwrite the previous entry of the same name (if any), otherwise log and return

◆ addLogEntry() [4/4]

template<typename T , typename std::enable_if< mc_rtc::log::callback_is_serializable< T >::value, int >::type = 0>
void mc_rtc::Logger::addLogEntry ( const std::string &  name,
T &&  get_fn,
bool  overwrite = false 
)
inline

Add a log entry into the log with no source

This function only accepts callable objects that returns a l/rvalue to a serializable object.

The log entry can only be removed by calling removeLogEntry with the same name

Parameters
nameName of the log entry, this should be unique at any given time but the same key can be re-used during the logger's life
get_fnA function that provides data that should be logged
overwriteIf true, overwrite the previous entry of the same name (if any), otherwise log and return

◆ clear()

void mc_rtc::Logger::clear ( bool  record = true)

Remove all entries (except t) from the log

Parameters
recordIf true record the removal events in the log

Note: if the record parameter is false and the log is written again it might be invalid

◆ flush()

void mc_rtc::Logger::flush ( )

Flush the log data to disk (only implemented in the synchronous method)

◆ log()

void mc_rtc::Logger::log ( )

Log controller's data.

Print controller data to the log.

◆ meta() [1/2]

const Meta& mc_rtc::Logger::meta ( ) const
inlinenoexcept

Access the log's metadata (const)

◆ meta() [2/2]

Meta& mc_rtc::Logger::meta ( )
inlinenoexcept

Access the log's metadata.

◆ open()

void mc_rtc::Logger::open ( const std::string &  file,
double  timestep,
double  start_t = 0.0 
)

Open a new file for logging.

This is similar to start but disregard the log directory and template. Instead the logging happens in the provided file: file.

Parameters
fileFile where data is going to be written
timestepTime increment for the log time entry
start_tStart the log at the specified time

◆ path()

const std::string& mc_rtc::Logger::path ( ) const

Access the file opened by this Logger

Note
This is empty before start or open has been called

◆ removeLogEntries()

void mc_rtc::Logger::removeLogEntries ( const void *  source)

Remove all log entries from a given source

This has no effect if no log entry is associated to the given source.

Parameters
sourceSource whose entries should be removed

◆ removeLogEntry()

void mc_rtc::Logger::removeLogEntry ( const std::string &  name)

Remove a log entry from the log

This has no effect if the log entry does not exist.

Parameters
nameName of the entry

◆ setup()

void mc_rtc::Logger::setup ( const Policy policy,
const std::string &  directory,
const std::string &  tmpl 
)

Setup the constructor configuration.

Parameters
policyThe chosen logging policy
directoryPath to the directory where log files will be stored
tmplLog file template

◆ size()

size_t mc_rtc::Logger::size ( ) const
inline

Returns the number of entries currently in the log

◆ start()

void mc_rtc::Logger::start ( const std::string &  ctl_name,
double  timestep,
bool  resume = false,
double  start_t = 0.0 
)

Start logging.

Print the file header to the log. This should be called at initialization or when a controller switch occurs

Parameters
ctl_nameName of the running controller
timestepTime increment for the log time entry
resumeIf true, start the time entry at the current value, otherwise, start at 0
start_tStart at the specified time, only used if resume is false

◆ t()

double mc_rtc::Logger::t ( ) const

Return the time elapsed since the controller start

Member Data Documentation

◆ magic

const uint8_t mc_rtc::Logger::magic[4]
static

Magic number used to identify binary logs

◆ version

const uint8_t mc_rtc::Logger::version
static

Version of the log format

This is stored in the binary file as data[3] - magic[3]


The documentation for this struct was generated from the following file: