Class SequentialWriter

Inheritance Relationships

Base Type

Class Documentation

class SequentialWriter : public rosbag2_cpp::writer_interfaces::BaseWriterInterface

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 SequentialWriter(std::unique_ptr<rosbag2_storage::StorageFactoryInterface> storage_factory = std::make_unique<rosbag2_storage::StorageFactory>(), std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory = std::make_shared<SerializationFormatConverterFactory>(), std::unique_ptr<rosbag2_storage::MetadataIo> metadata_io = std::make_unique<rosbag2_storage::MetadataIo>())
~SequentialWriter() override
virtual void open(const rosbag2_storage::StorageOptions &storage_options, const ConverterOptions &converter_options) override

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

virtual void close() override
virtual void create_topic(const rosbag2_storage::TopicMetadata &topic_with_type) override

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

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

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

virtual void remove_topic(const rosbag2_storage::TopicMetadata &topic_with_type) override

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

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

Write a message to a bagfile. The topic needs to have been created before writing is possible. Only writes message if within start_time_ns and end_time_ns (from storage_options).

Parameters:

message – to be written to the bagfile

Throws:

runtime_error – if the Writer is not open.

virtual bool take_snapshot() override

Take a snapshot by triggering a circular buffer flip, writing data to disk. *

Returns:

true if snapshot is successful

virtual void add_event_callbacks(const bag_events::WriterEventCallbacks &callbacks) override

Add callbacks for events that may occur during bag writing.

Parameters:

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

virtual void split_bagfile() override

Closes the current backed storage and opens the next bagfile.

virtual bool has_callback_for_event(bag_events::BagEvent event) const override

Check if a callback is registered for the given event.

Returns:

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

Protected Functions

std::string split_bagfile_local(bool execute_callbacks = true)
void execute_bag_split_callbacks(const std::string &closed_file, const std::string &opened_file)
void switch_to_next_storage()
std::string format_storage_uri(const std::string &base_folder, uint64_t storage_count)
bool should_split_bagfile(const std::chrono::time_point<std::chrono::high_resolution_clock> &current_time) const
bool message_within_accepted_time_range(const rcutils_time_point_value_t current_time) const
virtual void init_metadata()
void finalize_metadata()
virtual std::shared_ptr<const rosbag2_storage::SerializedBagMessage> get_writeable_message(std::shared_ptr<const rosbag2_storage::SerializedBagMessage> message)
void on_messages_lost(std::shared_ptr<std::vector<bag_events::MessagesLostInfo>> msgs_lost_info)

Helper method to process lost messages events.

Note

This method is expected to be called from a multiple threads.

Parameters:

msgs_lost_info – The information about lost messages to be processed.

Protected Attributes

std::string base_folder_
std::unique_ptr<rosbag2_storage::StorageFactoryInterface> storage_factory_
std::shared_ptr<SerializationFormatConverterFactoryInterface> converter_factory_
std::shared_ptr<rosbag2_storage::storage_interfaces::ReadWriteInterface> storage_
std::unique_ptr<rosbag2_storage::MetadataIo> metadata_io_
std::unique_ptr<Converter> converter_
bool use_cache_ = {false}
std::shared_ptr<rosbag2_cpp::cache::MessageCacheInterface> message_cache_
std::unique_ptr<rosbag2_cpp::cache::CacheConsumer> cache_consumer_
rosbag2_storage::StorageOptions storage_options_
std::unordered_map<std::string, rosbag2_storage::TopicInformation> topics_names_to_info_

Topic name to the TopicInformation map. Used to keep topic list and track message counts. If cache is present, the message counts updated by CacheConsumer.

See also

topics_info_mutex_ only when we are explicitly adding or deleting items (create_topic(..)/remove_topic(..)) and when we access it from CacheConsumer callback i.e., write_messages(..). In all other cases like in write(..) it is safe to access without topics_info_mutex_ losck as all external API calls are protected with

See also

writer_mutex_ on

See also

rosbag2_cpp::Writer level.

Note

The map is persisted across bagfile splits and writer close()->open() operations. However, the message counts inside TopicInformation are reset to zero on close() and open().

Note

topics_names_to_info_ needs to be protected with

std::mutex topics_info_mutex_
LocalMessageDefinitionSource message_definitions_
std::unordered_map<std::string, rosbag2_storage::MessageDefinition> topic_names_to_message_definitions_
rosbag2_storage::BagMetadata metadata_