diff --git a/framework/cybertron/record/record_writer.h b/framework/cybertron/record/record_writer.h index 053e77557c9d8aba33ce983abb783e9c11bf6d0a..4bb156a61e1703e3b27f58c304afb1cd16f8c708 100644 --- a/framework/cybertron/record/record_writer.h +++ b/framework/cybertron/record/record_writer.h @@ -52,14 +52,9 @@ class RecordWriter : public RecordBase { const std::string& message_type, const std::string& proto_desc); - bool WriteMessage(const std::string& channel_name, - const std::shared_ptr& message, - const uint64_t time); - template - bool WritePbMessage(const std::string& channel_name, const MessageT& message, - const uint64_t time, const std::string& proto_desc = ""); - + bool WriteMessage(const std::string& channel_name, const MessageT& message, + const uint64_t time, const std::string& proto_desc = ""); void ShowProgress(); private: @@ -73,34 +68,40 @@ class RecordWriter : public RecordBase { std::unique_ptr file_writer_backup_ = nullptr; }; -inline bool RecordWriter::WriteMessage( - const std::string& channel_name, - const std::shared_ptr& message, const uint64_t time) { - if (message == nullptr) { - AERROR << "nullptr error, channel: " << channel_name; - return false; - } +template <> +inline bool RecordWriter::WriteMessage(const std::string& channel_name, + const std::string& content, + const uint64_t time, + const std::string& proto_desc) { OnNewMessage(channel_name); SingleMessage single_msg; single_msg.set_channel_name(channel_name); - single_msg.set_content(message->message); + single_msg.set_content(content); single_msg.set_time(time); - if (!WriteMessage(std::move(single_msg))) { - AERROR << "write single msg fail"; + return WriteMessage(single_msg); +} + +template <> +inline bool RecordWriter::WriteMessage( + const std::string& channel_name, const std::shared_ptr& message, + const uint64_t time, const std::string& proto_desc) { + if (message == nullptr) { + AERROR << "nullptr error, channel: " << channel_name; return false; } - return true; + return WriteMessage(channel_name, message->message, time); } template -bool RecordWriter::WritePbMessage(const std::string& channel_name, - const MessageT& message, const uint64_t time, - const std::string& proto_desc) { - std::string message_type = GetMessageType(channel_name); +bool RecordWriter::WriteMessage(const std::string& channel_name, + const MessageT& message, const uint64_t time, + const std::string& proto_desc) { + const std::string& message_type = GetMessageType(channel_name); if (message_type.empty()) { if (!WriteChannel(channel_name, MessageT::descriptor()->full_name(), proto_desc)) { - AERROR << "Write channel fail, channel: " << channel_name; + AERROR << "Failed to write meta data to channel [" << channel_name + << "]."; return false; } } else { @@ -115,16 +116,7 @@ bool RecordWriter::WritePbMessage(const std::string& channel_name, AERROR << "Failed to serialize message, channel: " << channel_name; return false; } - OnNewMessage(channel_name); - SingleMessage single_msg; - single_msg.set_channel_name(channel_name); - single_msg.set_content(content); - single_msg.set_time(time); - if (!WriteMessage(std::move(single_msg))) { - AERROR << "write single msg fail"; - return false; - } - return true; + return WriteMessage(channel_name, content, time); } } // namespace record diff --git a/framework/cybertron/test/record/record_test.cpp b/framework/cybertron/test/record/record_test.cpp index fb57f4128fe8b88e812dfecf170a61ab99edac3b..0463e1f9791d3c81d18da63f4645724c78d9f20e 100644 --- a/framework/cybertron/test/record/record_test.cpp +++ b/framework/cybertron/test/record/record_test.cpp @@ -134,7 +134,7 @@ TEST(RecordTest, TestMutiMessageFile) { std::string pbmsg_content(""); pbmsg.SerializeToString(&pbmsg_content); - ASSERT_TRUE(rw->WritePbMessage(CHANNEL_NAME_2, pbmsg, TIME_2, PROTO_DESC)); + ASSERT_TRUE(rw->WriteMessage(CHANNEL_NAME_2, pbmsg, TIME_2, PROTO_DESC)); ASSERT_EQ(1, rw->GetMessageNumber(CHANNEL_NAME_2)); ASSERT_TRUE(rw->WriteMessage(CHANNEL_NAME_1, rm, TIME_3));