Class Writer

Class Documentation

class Writer

The Writer allows writing messages to a new bag. For every topic, information about its type needs to be added before writing the first message.

Public Functions

explicit Writer(std::unique_ptr<rosbag2_cpp::writer_interfaces::BaseWriterInterface> writer_impl = std::make_unique<writers::SequentialWriter>())
~Writer()
void open(const std::string &uri)

Opens a new bagfile and prepare it for writing messages. The bagfile must not exist. This must be called before any other function is used.

See also

rmw_get_serialization_format. For specifications, please see

See also

open, which let’s you specify more storage and converter options.

Note

This will open URI with the default storage options

  • using default storage backend

  • using no converter options, storing messages with the incoming serialization format

Parameters:

storage_uri – URI of the storage to open.

void open(const rosbag2_storage::StorageOptions &storage_options, const ConverterOptions &converter_options = ConverterOptions())

Opens a new bagfile and prepare it for writing messages. The bagfile must not exist. This must be called before any other function is used.

Parameters:
  • storage_options – Options to configure the storage

  • converter_options – options to define in which format incoming messages are stored

void create_topic(const rosbag2_storage::TopicMetadata &topic_with_type)

Create a new topic in the underlying storage. Needs to be called for every topic used within a message which is passed to write(…).

Parameters:

topic_with_type – name and type identifier of topic to be created

Throws:

runtime_error – if the Writer is not open.

bool take_snapshot()

Trigger a snapshot when snapshot mode is enabled.

Returns:

true if snapshot is successful, false if snapshot fails or is not supported

void remove_topic(const rosbag2_storage::TopicMetadata &topic_with_type)

Remove a new topic in the underlying storage. If creation of subscription fails remove the topic from the db (more of cleanup)

Parameters:

topic_with_type – name and type identifier of topic to be created

Throws:

runtime_error – if the Writer is not open.

void write(std::shared_ptr<rosbag2_storage::SerializedBagMessage> message)

Write a message to a bagfile. The topic needs to have been created before writing is possible.

Parameters:

message – to be written to the bagfile

Throws:

runtime_error – if the Writer is not open.

void write(std::shared_ptr<rosbag2_storage::SerializedBagMessage> message, const std::string &topic_name, const std::string &type_name, const std::string &serialization_format = "cdr")

Write a message to a bagfile. The topic will be created if it has not been created already.

Parameters:
  • message – to be written to the bagfile

  • topic_name – the string of the topic this messages belongs to

  • type_name – the string of the type associated with this message

  • serialization_format – the format in which this message is serialized

Throws:

runtime_error – if the Writer is not open.

void write(const rclcpp::SerializedMessage &message, const std::string &topic_name, const std::string &type_name, const rclcpp::Time &time)

Write a serialized message to a bagfile. The topic will be created if it has not been created already.

Parameters:
  • message – rclcpp::SerializedMessage The serialized message to be written to the bagfile

  • topic_name – the string of the topic this messages belongs to

  • type_name – the string of the type associated with this message

  • time – The time stamp of the message

Throws:

runtime_error – if the Writer is not open or duplicating message is failed.

void write(std::shared_ptr<rclcpp::SerializedMessage> message, const std::string &topic_name, const std::string &type_name, const rclcpp::Time &time)

Write a serialized message to a bagfile. The topic will be created if it has not been created already.

Warning

after calling this function, the serialized data will no longer be managed by message.

Parameters:
  • message – rclcpp::SerializedMessage The serialized message to be written to the bagfile

  • topic_name – the string of the topic this messages belongs to

  • type_name – the string of the type associated with this message

  • time – The time stamp of the message

Throws:

runtime_error – if the Writer is not open.

template<class MessageT>
inline void write(const MessageT &message, const std::string &topic_name, const rclcpp::Time &time)

Write a non-serialized message to a bagfile. The topic will be created if it has not been created already.

Parameters:
  • message – MessageT The serialized message to be written to the bagfile

  • topic_name – the string of the topic this messages belongs to

  • type_name – the string of the type associated with this message

  • time – The time stamp of the message

Throws:

runtime_error – if the Writer is not open.

inline writer_interfaces::BaseWriterInterface &get_implementation_handle() const
void add_event_callbacks(bag_events::WriterEventCallbacks &callbacks)
void close()

Close the current bag file and write metadata.yaml file.