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 among create_topic and remove_topic.

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 among create_topic and remove_topic.

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(…).

Note

If writer is not open, this will just store the topic information locally and topics will be created on storage open.

Parameters:

topic_with_type – name and type identifier of topic to be created

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(…).

Note

If writer is not open, this will just store the topic information locally and topics will be created on storage open.

Parameters:
  • topic_with_type – name and type identifier of topic to be created

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

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)

Removes a new topic in the underlying storage.

Expected to be used if creation of subscription fails and cleanup is needed.

Note

If writer is not open, this will just remove the topic information locally.

Parameters:

topic_with_type – name and type identifier of topic to be created

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(std::shared_ptr<const rclcpp::SerializedMessage> message, const std::string &topic_name, const std::string &type_name, const rclcpp::Time &time, uint32_t sequence_number = 0)

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

  • sequence_number – An optional sequence number of the message. If non-zero, sequence numbers should be unique per channel (per topic and per publisher) and increasing over time.

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, uint32_t sequence_number = 0)

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

  • sequence_number – An optional sequence number of the message. If non-zero, sequence numbers should be unique per channel (per topic and per publisher) and increasing over time.

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, uint32_t sequence_number = 0)

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

  • sequence_number – An optional sequence number of the message. If non-zero, sequence numbers should be unique per channel (per topic and per publisher) and increasing over time.

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)

Add callbacks for events that may occur during bag writing.

Parameters:

callbacks – the structure containing the callback to add for each event.

Throws:

std::runtime_error – if none of the write_split_callback and messages_lost_callback callbacks are set.

bool has_callback_for_event(bag_events::BagEvent event) const

Check if a callback is registered for the given event.

Returns:

True if there is any callback registered for the event, false otherwise.