Drake
Drake C++ Documentation
DrakeLcmLog Class Reference

Detailed Description

A LCM interface for logging LCM messages to a file or playing back from a existing log.

Note the user is responsible for offsetting the clock used to generate the log and the clock used for playback. For example, if the log is generated by some external logger (the lcm-logger binary), which uses the unix epoch time clock to record message arrival time, the user needs to offset those timestamps properly to match and the clock used for playback.

#include <drake/lcm/drake_lcm_log.h>

Public Member Functions

 DrakeLcmLog (const std::string &file_name, bool is_write, bool overwrite_publish_time_with_system_clock=false)
 Constructs a DrakeLcmLog. More...
 
 ~DrakeLcmLog () override
 
void Publish (const std::string &channel, const void *data, int data_size, std::optional< double > time_sec) override
 Writes an entry occurred at timestamp with content data to the log file. More...
 
std::shared_ptr< DrakeSubscriptionInterfaceSubscribe (const std::string &channel, HandlerFunction handler) override
 Subscribes handler to channel. More...
 
std::shared_ptr< DrakeSubscriptionInterfaceSubscribeMultichannel (std::string_view regex, MultichannelHandlerFunction) override
 This function is not yet supported for LCM logs, and will always throw. More...
 
std::shared_ptr< DrakeSubscriptionInterfaceSubscribeAllChannels (MultichannelHandlerFunction) override
 Subscribe to all channels; this is useful for logging and redirecting LCM traffic without regard to its content. More...
 
int HandleSubscriptions (int) override
 This is a no-op for Read mode, and an exception in Write mode. More...
 
double GetNextMessageTime () const
 Returns the time in seconds for the next logged message's occurrence time or infinity if there are no more messages in the current log. More...
 
void DispatchMessageAndAdvanceLog (double current_time)
 Let MSG be the next message event in the log, if current_time matches MSG's timestamp, for every DrakeLcmMessageHandlerInterface sub that's subscribed to MSG's channel, invoke sub's HandleMessage method. More...
 
bool is_write () const
 Returns true if this instance is constructed in write-only mode. More...
 
double timestamp_to_second (uint64_t timestamp) const
 Converts timestamp (in microseconds) to time (in seconds) relative to the starting time passed to the constructor. More...
 
uint64_t second_to_timestamp (double sec) const
 Converts time (in seconds) relative to the starting time passed to the constructor to a timestamp in microseconds. More...
 
std::string get_lcm_url () const override
 Returns a URL describing the transport of this LCM interface. More...
 
Does not allow copy, move, or assignment
 DrakeLcmLog (const DrakeLcmLog &)=delete
 
DrakeLcmLogoperator= (const DrakeLcmLog &)=delete
 
 DrakeLcmLog (DrakeLcmLog &&)=delete
 
DrakeLcmLogoperator= (DrakeLcmLog &&)=delete
 
- Public Member Functions inherited from DrakeLcmInterface
virtual ~DrakeLcmInterface ()
 
 DrakeLcmInterface (const DrakeLcmInterface &)=delete
 
DrakeLcmInterfaceoperator= (const DrakeLcmInterface &)=delete
 
 DrakeLcmInterface (DrakeLcmInterface &&)=delete
 
DrakeLcmInterfaceoperator= (DrakeLcmInterface &&)=delete
 

Additional Inherited Members

- Public Types inherited from DrakeLcmInterface
using HandlerFunction = std::function< void(const void *, int)>
 A callback used by DrakeLcmInterface::Subscribe(), with arguments: More...
 
using MultichannelHandlerFunction = std::function< void(std::string_view, const void *, int)>
 A callback used by DrakeLcmInterface::SubscribeMultipleChannels (which therefore needs the receiving channel passed in). More...
 
- Protected Member Functions inherited from DrakeLcmInterface
 DrakeLcmInterface ()
 

Constructor & Destructor Documentation

◆ DrakeLcmLog() [1/3]

DrakeLcmLog ( const DrakeLcmLog )
delete

◆ DrakeLcmLog() [2/3]

DrakeLcmLog ( DrakeLcmLog &&  )
delete

◆ DrakeLcmLog() [3/3]

DrakeLcmLog ( const std::string &  file_name,
bool  is_write,
bool  overwrite_publish_time_with_system_clock = false 
)

Constructs a DrakeLcmLog.

Parameters
file_nameLog's file name for reading or writing.
is_writeIf false, this instance reads from the Lcm log identified by file_name. If true, this instance writes to the Lcm log whose name is given by file_name.
overwrite_publish_time_with_system_clockThis parameter only affects the Publish method in write-only mode. If true, override the second parameter passed to Publish method, and use host system's clock to generate the timestamp for the logged message. This is used to mimic lcm-logger's behavior. It also implicitly records how fast the messages are generated in real time.
Exceptions
std::exceptionif unable to open file.

◆ ~DrakeLcmLog()

~DrakeLcmLog ( )
override

Member Function Documentation

◆ DispatchMessageAndAdvanceLog()

void DispatchMessageAndAdvanceLog ( double  current_time)

Let MSG be the next message event in the log, if current_time matches MSG's timestamp, for every DrakeLcmMessageHandlerInterface sub that's subscribed to MSG's channel, invoke sub's HandleMessage method.

Then, this function advances the log by exactly one message. This function does nothing if MSG is null (end of log) or current_time does not match MSG's timestamp.

Exceptions
std::exceptionif this instance is not constructed in read-only mode.

◆ get_lcm_url()

std::string get_lcm_url ( ) const
overridevirtual

Returns a URL describing the transport of this LCM interface.

When the URL refers to a transport offered by LCM itself (e.g., memq or udpm), then this function must return the conventional URL spelling. If the implementation of DrakeLcmInterface is using a non-standard back end, the result implementation-defined.

In either case, it is always formatted using URI syntax rules per the RFC(s).

Implements DrakeLcmInterface.

◆ GetNextMessageTime()

double GetNextMessageTime ( ) const

Returns the time in seconds for the next logged message's occurrence time or infinity if there are no more messages in the current log.

Exceptions
std::exceptionif this instance is not constructed in read-only mode.

◆ HandleSubscriptions()

int HandleSubscriptions ( int  )
overridevirtual

This is a no-op for Read mode, and an exception in Write mode.

Implements DrakeLcmInterface.

◆ is_write()

bool is_write ( ) const

Returns true if this instance is constructed in write-only mode.

◆ operator=() [1/2]

DrakeLcmLog& operator= ( DrakeLcmLog &&  )
delete

◆ operator=() [2/2]

DrakeLcmLog& operator= ( const DrakeLcmLog )
delete

◆ Publish()

void Publish ( const std::string &  channel,
const void *  data,
int  data_size,
std::optional< double time_sec 
)
overridevirtual

Writes an entry occurred at timestamp with content data to the log file.

The current implementation blocks until writing is done.

Parameters
channelChannel name.
dataPointer to raw bytes.
data_sizeNumber of bytes in data.
time_secTime in seconds when the message is published. Since messages are save to the log file in the order of Publish calls, this function should only be called with non-decreasing second. Note that this parameter can be overwritten by the host system's clock if overwrite_publish_time_with_system_clock is true at construction time.
Exceptions
std::exceptionif this instance is not constructed in write-only mode.

Implements DrakeLcmInterface.

◆ second_to_timestamp()

uint64_t second_to_timestamp ( double  sec) const

Converts time (in seconds) relative to the starting time passed to the constructor to a timestamp in microseconds.

◆ Subscribe()

std::shared_ptr<DrakeSubscriptionInterface> Subscribe ( const std::string &  channel,
HandlerFunction  handler 
)
overridevirtual

Subscribes handler to channel.

Multiple handlers can subscribe to the same channel.

Exceptions
std::exceptionif this instance is not constructed in read-only mode.
Returns
nullptr because this implementation does not support unsubscribe.

Implements DrakeLcmInterface.

◆ SubscribeAllChannels()

std::shared_ptr<DrakeSubscriptionInterface> SubscribeAllChannels ( MultichannelHandlerFunction  )
overridevirtual

Subscribe to all channels; this is useful for logging and redirecting LCM traffic without regard to its content.

Exceptions
std::exceptionif this instance is not constructed in read-only mode.
Returns
nullptr because this implementation does not support unsubscribe.

Implements DrakeLcmInterface.

◆ SubscribeMultichannel()

std::shared_ptr<DrakeSubscriptionInterface> SubscribeMultichannel ( std::string_view  regex,
MultichannelHandlerFunction   
)
overridevirtual

This function is not yet supported for LCM logs, and will always throw.

Implements DrakeLcmInterface.

◆ timestamp_to_second()

double timestamp_to_second ( uint64_t  timestamp) const

Converts timestamp (in microseconds) to time (in seconds) relative to the starting time passed to the constructor.


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