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 close()

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

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.

void create_topic(const rosbag2_storage::TopicMetadata &topic_with_type, const rosbag2_storage::MessageDefinition &message_definition)

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

  • message_definition – message definition content for this topic’s type

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 split_bagfile()

Close the current bagfile and opens the next bagfile.

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<const 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<const 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<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.

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.

void write(std::shared_ptr<const rclcpp::SerializedMessage> message, const std::string &topic_name, const std::string &type_name, const rcutils_time_point_value_t &recv_timestamp, const rcutils_time_point_value_t &send_timestamp)

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

  • recv_time – The time stamp when the message was received

  • send_time – The time stamp when the message was send

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)