Apollo 10.0
自动驾驶开放平台
apollo::cyber::record::RecordWriter类 参考

The record writer. 更多...

#include <record_writer.h>

类 apollo::cyber::record::RecordWriter 继承关系图:
apollo::cyber::record::RecordWriter 的协作图:

Public 类型

using MessageNumberMap = std::unordered_map< std::string, uint64_t >
 
using MessageTypeMap = std::unordered_map< std::string, std::string >
 
using MessageProtoDescMap = std::unordered_map< std::string, std::string >
 
using FileWriterPtr = std::unique_ptr< RecordFileWriter >
 

Public 成员函数

 RecordWriter ()
 The default constructor.
 
 RecordWriter (const proto::Header &header)
 The constructor with record header as parameter.
 
virtual ~RecordWriter ()
 Virtual Destructor.
 
bool Open (const std::string &file)
 Open a record to write.
 
void Close ()
 Clean the record.
 
bool WriteChannel (const std::string &channel_name, const std::string &message_type, const std::string &proto_desc)
 Write a channel to record.
 
template<typename MessageT >
bool WriteMessage (const std::string &channel_name, const MessageT &message, const uint64_t time_nanosec, const std::string &proto_desc="")
 Write a message to record.
 
bool SetSizeOfFileSegmentation (uint64_t size_kilobytes)
 Set max size (KB) to segment record file
 
bool SetIntervalOfFileSegmentation (uint64_t time_sec)
 Set max interval (Second) to segment record file.
 
uint64_t GetMessageNumber (const std::string &channel_name) const override
 Get message number by channel name.
 
const std::string & GetMessageType (const std::string &channel_name) const override
 Get message type by channel name.
 
const std::string & GetProtoDesc (const std::string &channel_name) const override
 Get proto descriptor string by channel name.
 
std::set< std::string > GetChannelList () const override
 Get channel list.
 
bool IsNewChannel (const std::string &channel_name) const
 Is a new channel recording or not.
 
template<>
bool WriteMessage (const std::string &channel_name, const std::string &message, const uint64_t time_nanosec, const std::string &proto_desc)
 
template<>
bool WriteMessage (const std::string &channel_name, const std::shared_ptr< message::RawMessage > &message, const uint64_t time_nanosec, const std::string &proto_desc)
 
- Public 成员函数 继承自 apollo::cyber::record::RecordBase
virtual ~RecordBase ()=default
 Destructor.
 
const proto::HeaderGetHeader () const
 Get record header.
 
const std::string GetFile () const
 Get record file path.
 

额外继承的成员函数

- Protected 属性 继承自 apollo::cyber::record::RecordBase
std::string file_
 
proto::Header header_
 
bool is_opened_ = false
 

详细描述

The record writer.

在文件 record_writer.h44 行定义.

成员类型定义说明

◆ FileWriterPtr

◆ MessageNumberMap

using apollo::cyber::record::RecordWriter::MessageNumberMap = std::unordered_map<std::string, uint64_t>

在文件 record_writer.h46 行定义.

◆ MessageProtoDescMap

using apollo::cyber::record::RecordWriter::MessageProtoDescMap = std::unordered_map<std::string, std::string>

在文件 record_writer.h48 行定义.

◆ MessageTypeMap

using apollo::cyber::record::RecordWriter::MessageTypeMap = std::unordered_map<std::string, std::string>

在文件 record_writer.h47 行定义.

构造及析构函数说明

◆ RecordWriter() [1/2]

apollo::cyber::record::RecordWriter::RecordWriter ( )

The default constructor.

在文件 record_writer.cc33 行定义.

static proto::Header GetHeader()
Build a default record header.

◆ RecordWriter() [2/2]

apollo::cyber::record::RecordWriter::RecordWriter ( const proto::Header header)
explicit

The constructor with record header as parameter.

参数
header

在文件 record_writer.cc35 行定义.

35{ header_ = header; }

◆ ~RecordWriter()

apollo::cyber::record::RecordWriter::~RecordWriter ( )
virtual

Virtual Destructor.

在文件 record_writer.cc37 行定义.

37{ Close(); }

成员函数说明

◆ Close()

void apollo::cyber::record::RecordWriter::Close ( )

Clean the record.

在文件 record_writer.cc65 行定义.

65 {
66 if (is_opened_) {
67 file_writer_->Close();
68 is_opened_ = false;
69 }
70}

◆ GetChannelList()

std::set< std::string > apollo::cyber::record::RecordWriter::GetChannelList ( ) const
overridevirtual

Get channel list.

返回
List container with all channel name string.

实现了 apollo::cyber::record::RecordBase.

在文件 record_writer.cc222 行定义.

222 {
223 std::set<std::string> channel_list;
224 for (const auto& item : channel_message_number_map_) {
225 channel_list.insert(item.first);
226 }
227 return channel_list;
228}

◆ GetMessageNumber()

uint64_t apollo::cyber::record::RecordWriter::GetMessageNumber ( const std::string &  channel_name) const
overridevirtual

Get message number by channel name.

参数
channel_name
返回
Message number.

实现了 apollo::cyber::record::RecordBase.

在文件 record_writer.cc196 行定义.

196 {
197 auto search = channel_message_number_map_.find(channel_name);
198 if (search != channel_message_number_map_.end()) {
199 return search->second;
200 }
201 return 0;
202}

◆ GetMessageType()

const std::string & apollo::cyber::record::RecordWriter::GetMessageType ( const std::string &  channel_name) const
overridevirtual

Get message type by channel name.

参数
channel_name
返回
Message type.

实现了 apollo::cyber::record::RecordBase.

在文件 record_writer.cc204 行定义.

205 {
206 auto search = channel_message_type_map_.find(channel_name);
207 if (search != channel_message_type_map_.end()) {
208 return search->second;
209 }
210 return kEmptyString;
211}

◆ GetProtoDesc()

const std::string & apollo::cyber::record::RecordWriter::GetProtoDesc ( const std::string &  channel_name) const
overridevirtual

Get proto descriptor string by channel name.

参数
channel_name
返回
Proto descriptor string by channel name.

实现了 apollo::cyber::record::RecordBase.

在文件 record_writer.cc213 行定义.

214 {
215 auto search = channel_proto_desc_map_.find(channel_name);
216 if (search != channel_proto_desc_map_.end()) {
217 return search->second;
218 }
219 return kEmptyString;
220}

◆ IsNewChannel()

bool apollo::cyber::record::RecordWriter::IsNewChannel ( const std::string &  channel_name) const

Is a new channel recording or not.

返回
True for yes, false for no.

在文件 record_writer.cc176 行定义.

176 {
177 return channel_message_number_map_.find(channel_name) ==
178 channel_message_number_map_.end();
179}

◆ Open()

bool apollo::cyber::record::RecordWriter::Open ( const std::string &  file)

Open a record to write.

参数
file
返回
True for success, false for fail.

在文件 record_writer.cc39 行定义.

39 {
40 file_ = file;
41 file_index_ = 0;
42 sstream_.str(std::string());
43 sstream_.clear();
44 sstream_ << "." << std::setw(5) << std::setfill('0') << file_index_++ << ".";
46 path_ = file_ + sstream_.str() +
47 UnixSecondsToString(time(nullptr), "%Y%m%d%H%M%S");
48 } else {
49 path_ = file_;
50 }
51 file_writer_.reset(new RecordFileWriter());
52 if (!file_writer_->Open(path_)) {
53 AERROR << "Failed to open output record file: " << path_;
54 return false;
55 }
56 if (!file_writer_->WriteHeader(header_)) {
57 AERROR << "Failed to write header: " << path_;
58 file_writer_->Close();
59 return false;
60 }
61 is_opened_ = true;
62 return is_opened_;
63}
#define AERROR
Definition log.h:44
std::string UnixSecondsToString(uint64_t unix_seconds, const std::string &format_str="%Y-%m-%d-%H:%M:%S")
optional uint64 segment_interval
Definition record.proto:67
optional uint64 segment_raw_size
Definition record.proto:77

◆ SetIntervalOfFileSegmentation()

bool apollo::cyber::record::RecordWriter::SetIntervalOfFileSegmentation ( uint64_t  time_sec)

Set max interval (Second) to segment record file.

参数
time_sec
返回
True for success, false for fail.

在文件 record_writer.cc167 行定义.

167 {
168 if (is_opened_) {
169 AWARN << "Please call this interface before opening file.";
170 return false;
171 }
172 header_.set_segment_interval(time_sec * 1000000000UL);
173 return true;
174}
#define AWARN
Definition log.h:43

◆ SetSizeOfFileSegmentation()

bool apollo::cyber::record::RecordWriter::SetSizeOfFileSegmentation ( uint64_t  size_kilobytes)

Set max size (KB) to segment record file

参数
size_kilobytes
返回
True for success, false for fail.

在文件 record_writer.cc158 行定义.

158 {
159 if (is_opened_) {
160 AWARN << "Please call this interface before opening file.";
161 return false;
162 }
163 header_.set_segment_raw_size(size_kilobytes * 1024UL);
164 return true;
165}

◆ WriteChannel()

bool apollo::cyber::record::RecordWriter::WriteChannel ( const std::string &  channel_name,
const std::string &  message_type,
const std::string &  proto_desc 
)

Write a channel to record.

参数
channel_name
message_type
proto_desc
返回
True for success, false for fail.

在文件 record_writer.cc107 行定义.

109 {
110 std::lock_guard<std::mutex> lg(mutex_);
111 if (IsNewChannel(channel_name)) {
112 OnNewChannel(channel_name, message_type, proto_desc);
113 Channel channel;
114 channel.set_name(channel_name);
115 channel.set_message_type(message_type);
116 channel.set_proto_desc(proto_desc);
117 if (!file_writer_->WriteChannel(channel)) {
118 AERROR << "Failed to write channel: " << channel_name;
119 return false;
120 }
121 } else {
122 AWARN << "Intercept write channel request, duplicate channel: "
123 << channel_name;
124 }
125 return true;
126}
bool IsNewChannel(const std::string &channel_name) const
Is a new channel recording or not.

◆ WriteMessage() [1/3]

template<typename MessageT >
bool apollo::cyber::record::RecordWriter::WriteMessage ( const std::string &  channel_name,
const MessageT &  message,
const uint64_t  time_nanosec,
const std::string &  proto_desc = "" 
)

Write a message to record.

模板参数
MessageT
参数
channel_name
message
time_nanosec
proto_desc
返回
True for success, false for fail.

在文件 record_writer.h218 行定义.

221 {
222 const std::string& message_type = GetMessageType(channel_name);
223 if (message_type.empty()) {
224 if (!WriteChannel(channel_name, message::GetMessageName<MessageT>(),
225 proto_desc)) {
226 AERROR << "Failed to write meta data to channel [" << channel_name
227 << "].";
228 return false;
229 }
230 } else {
231 if (MessageT::descriptor()->full_name() != message_type) {
232 AERROR << "Message type is invalid, expect: " << message_type
233 << ", actual: " << message::GetMessageName<MessageT>();
234 return false;
235 }
236 }
237 std::string content("");
238 if (!message.SerializeToString(&content)) {
239 AERROR << "Failed to serialize message, channel: " << channel_name;
240 return false;
241 }
242 return WriteMessage(channel_name, content, time_nanosec);
243}
bool WriteMessage(const std::string &channel_name, const MessageT &message, const uint64_t time_nanosec, const std::string &proto_desc="")
Write a message to record.
const std::string & GetMessageType(const std::string &channel_name) const override
Get message type by channel name.
bool WriteChannel(const std::string &channel_name, const std::string &message_type, const std::string &proto_desc)
Write a channel to record.

◆ WriteMessage() [2/3]

template<>
bool apollo::cyber::record::RecordWriter::WriteMessage ( const std::string &  channel_name,
const std::shared_ptr< message::RawMessage > &  message,
const uint64_t  time_nanosec,
const std::string &  proto_desc 
)
inline

在文件 record_writer.h206 行定义.

209 {
210 if (message == nullptr) {
211 AERROR << "nullptr error, channel: " << channel_name;
212 return false;
213 }
214 return WriteMessage(channel_name, message->message, time_nanosec);
215}

◆ WriteMessage() [3/3]

template<>
bool apollo::cyber::record::RecordWriter::WriteMessage ( const std::string &  channel_name,
const std::string &  message,
const uint64_t  time_nanosec,
const std::string &  proto_desc 
)
inline

在文件 record_writer.h194 行定义.

197 {
198 proto::SingleMessage single_msg;
199 single_msg.set_channel_name(channel_name);
200 single_msg.set_content(message);
201 single_msg.set_time(time_nanosec);
202 return WriteMessage(single_msg);
203}

该类的文档由以下文件生成: