diff --git a/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/McapWriter.hpp b/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/McapWriter.hpp index 3848ce7b0..de3f39b69 100644 --- a/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/McapWriter.hpp +++ b/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/McapWriter.hpp @@ -203,6 +203,9 @@ class DDSRECORDER_PARTICIPANTS_DllAPI McapWriter // Whether the writer can write to the MCAP library bool enabled_{false}; + // Whether the output file is open + bool opened_{false}; + // Track the size of the current MCAP file McapSizeTracker size_tracker_; diff --git a/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/impl/McapWriter.ipp b/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/impl/McapWriter.ipp index e38564ca9..a87fcb438 100644 --- a/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/impl/McapWriter.ipp +++ b/ddsrecorder_participants/include/ddsrecorder_participants/recorder/mcap/impl/McapWriter.ipp @@ -49,6 +49,12 @@ void McapWriter::write( on_disk_full_(); } } + catch (const FullDiskException& e) + { + logError(DDSRECORDER_MCAP_HANDLER, + "FAIL_MCAP_WRITE | Disk is full. Error message:\n " << e.what()); + on_disk_full_(); + } } } /* namespace participants */ diff --git a/ddsrecorder_participants/src/cpp/recorder/mcap/McapWriter.cpp b/ddsrecorder_participants/src/cpp/recorder/mcap/McapWriter.cpp index 6d24e07a6..e212bedde 100644 --- a/ddsrecorder_participants/src/cpp/recorder/mcap/McapWriter.cpp +++ b/ddsrecorder_participants/src/cpp/recorder/mcap/McapWriter.cpp @@ -145,6 +145,12 @@ void McapWriter::update_dynamic_types( on_disk_full_(); } } + catch (const FullDiskException& e) + { + logError(DDSRECORDER_MCAP_HANDLER, + "FAIL_MCAP_WRITE | Disk is full. Error message:\n " << e.what()); + on_disk_full_(); + } dynamic_types_payload_.reset(const_cast(&dynamic_types_payload)); file_tracker_->set_current_file_size(size_tracker_.get_potential_mcap_size()); @@ -159,6 +165,11 @@ void McapWriter::set_on_disk_full_callback( void McapWriter::open_new_file_nts_( const std::uint64_t min_file_size) { + if (opened_) + { + return; + } + try { file_tracker_->new_file(min_file_size); @@ -169,6 +180,10 @@ void McapWriter::open_new_file_nts_( "The minimum MCAP size (" + utils::from_bytes(min_file_size) + ") is greater than the maximum MCAP " "size (" + utils::from_bytes(configuration_.max_file_size) + ")."); } + catch (const utils::InconsistencyException& e) + { + throw FullDiskException(e.what()); + } const auto filename = file_tracker_->get_current_filename(); const auto status = writer_.open(filename, mcap_configuration_); @@ -182,6 +197,8 @@ void McapWriter::open_new_file_nts_( throw utils::InitializationException(error_msg); } + opened_ = true; + // Set the file's maximum size const auto max_file_size = std::min( configuration_.max_file_size, @@ -204,6 +221,11 @@ void McapWriter::open_new_file_nts_( void McapWriter::close_current_file_nts_() { + if (!opened_) + { + return; + } + if (record_types_ && dynamic_types_payload_ != nullptr) { // NOTE: This write should never fail since the minimum size accounts for it. @@ -214,6 +236,8 @@ void McapWriter::close_current_file_nts_() size_tracker_.reset(file_tracker_->get_current_filename()); writer_.close(); + opened_ = false; + file_tracker_->close_file(); } @@ -221,6 +245,11 @@ template <> void McapWriter::write_nts_( const mcap::Attachment& attachment) { + if (!opened_) + { + return; + } + logInfo(DDSRECORDER_MCAP_WRITER, "MCAP_WRITE | Writing attachment: " << attachment.name << " (" << utils::from_bytes(attachment.dataSize) << ")."); @@ -243,6 +272,11 @@ template <> void McapWriter::write_nts_( const mcap::Channel& channel) { + if (!opened_) + { + return; + } + logInfo(DDSRECORDER_MCAP_WRITER, "MCAP_WRITE | Writing channel " << channel.topic << "."); @@ -264,10 +298,8 @@ template <> void McapWriter::write_nts_( const McapMessage& msg) { - if (!enabled_) + if (!opened_ || !enabled_) { - logWarning(DDSRECORDER_MCAP_WRITER, - "MCAP_WRITE | Attempting to write a message in a disabled writer."); return; } @@ -291,6 +323,11 @@ template <> void McapWriter::write_nts_( const mcap::Metadata& metadata) { + if (!opened_) + { + return; + } + logInfo(DDSRECORDER_MCAP_WRITER, "MCAP_WRITE | Writing metadata: " << metadata.name << "."); @@ -312,6 +349,11 @@ template <> void McapWriter::write_nts_( const mcap::Schema& schema) { + if (!opened_) + { + return; + } + logInfo(DDSRECORDER_MCAP_WRITER, "MCAP_WRITE | Writing schema: " << schema.name << "."); @@ -327,6 +369,11 @@ void McapWriter::write_nts_( void McapWriter::write_attachment_nts_() { + if (!opened_) + { + return; + } + mcap::Attachment attachment; // Write down the attachment with the dynamic types @@ -342,7 +389,7 @@ void McapWriter::write_attachment_nts_() void McapWriter::write_channels_nts_() { - if (channels_.empty()) + if (!opened_ || channels_.empty()) { return; } @@ -359,6 +406,11 @@ void McapWriter::write_channels_nts_() void McapWriter::write_metadata_nts_() { + if (!opened_) + { + return; + } + mcap::Metadata metadata; // Write down the metadata with the version @@ -371,7 +423,7 @@ void McapWriter::write_metadata_nts_() void McapWriter::write_schemas_nts_() { - if (schemas_.empty()) + if (!opened_ || schemas_.empty()) { return; }