Apollo 10.0
自动驾驶开放平台
record.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <string>
18
19#include "cyber/proto/record.pb.h"
20
21#include "cyber/cyber.h"
26
28using ::apollo::cyber::record::RecordMessage;
29using ::apollo::cyber::record::RecordReader;
30using ::apollo::cyber::record::RecordWriter;
31
32const char CHANNEL_NAME_1[] = "/test/channel1";
33const char CHANNEL_NAME_2[] = "/test/channel2";
34const char MESSAGE_TYPE_1[] = "apollo.cyber.proto.Test";
35const char MESSAGE_TYPE_2[] = "apollo.cyber.proto.Channel";
36const char PROTO_DESC[] = "1234567890";
37const char STR_10B[] = "1234567890";
38const char TEST_FILE[] = "test.record";
39
40void test_write(const std::string &writefile) {
41 RecordWriter writer;
42 writer.SetSizeOfFileSegmentation(0);
43 writer.SetIntervalOfFileSegmentation(0);
44 writer.Open(writefile);
45 writer.WriteChannel(CHANNEL_NAME_1, MESSAGE_TYPE_1, PROTO_DESC);
46 for (uint32_t i = 0; i < 100; ++i) {
47 auto msg = std::make_shared<RawMessage>("abc" + std::to_string(i));
48 writer.WriteMessage(CHANNEL_NAME_1, msg, 888 + i);
49 }
50 writer.Close();
51}
52
53void test_read(const std::string &readfile) {
54 RecordReader reader(readfile);
55 RecordMessage message;
56 uint64_t msg_count = reader.GetMessageNumber(CHANNEL_NAME_1);
57 AINFO << "MSGTYPE: " << reader.GetMessageType(CHANNEL_NAME_1);
58 AINFO << "MSGDESC: " << reader.GetProtoDesc(CHANNEL_NAME_1);
59
60 // read all message
61 uint64_t i = 0;
62 uint64_t valid = 0;
63 for (i = 0; i < msg_count; ++i) {
64 if (reader.ReadMessage(&message)) {
65 AINFO << "msg[" << i << "]-> "
66 << "channel name: " << message.channel_name
67 << "; content: " << message.content
68 << "; msg time: " << message.time;
69 valid++;
70 } else {
71 AERROR << "read msg[" << i << "] failed";
72 }
73 }
74 AINFO << "static msg=================";
75 AINFO << "MSG validmsg:totalcount: " << valid << ":" << msg_count;
76}
77
78int main(int argc, char *argv[]) {
79 apollo::cyber::Init(argv[0]);
81 sleep(1);
83 return 0;
84}
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:98
const char STR_10B[]
Definition record.cc:37
int main(int argc, char *argv[])
Definition record.cc:78
const char CHANNEL_NAME_1[]
Definition record.cc:32
const char MESSAGE_TYPE_1[]
Definition record.cc:34
const char CHANNEL_NAME_2[]
Definition record.cc:33
void test_read(const std::string &readfile)
Definition record.cc:53
void test_write(const std::string &writefile)
Definition record.cc:40
const char TEST_FILE[]
Definition record.cc:38
const char PROTO_DESC[]
Definition record.cc:36
const char MESSAGE_TYPE_2[]
Definition record.cc:35