blob: d5626a929ecbf5ac63bfae94b8522897ddf5efa8 [file] [log] [blame]
James Kuszmaul38735e82019-12-07 16:42:06 -08001#include "aos/events/logging/logger.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08002
3#include <fcntl.h>
Austin Schuh4c4e0092019-12-22 16:18:03 -08004#include <limits.h>
Austin Schuhe309d2a2019-11-29 13:25:21 -08005#include <sys/stat.h>
6#include <sys/types.h>
7#include <sys/uio.h>
8#include <vector>
9
Austin Schuhe309d2a2019-11-29 13:25:21 -080010#include "absl/types/span.h"
11#include "aos/events/event_loop.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080012#include "aos/events/logging/logger_generated.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080013#include "aos/flatbuffer_merge.h"
Austin Schuh288479d2019-12-18 19:47:52 -080014#include "aos/network/team_number.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080015#include "aos/time/time.h"
16#include "flatbuffers/flatbuffers.h"
17
Austin Schuh15649d62019-12-28 16:36:38 -080018DEFINE_bool(skip_missing_forwarding_entries, false,
19 "If true, drop any forwarding entries with missing data. If "
20 "false, CHECK.");
Austin Schuhe309d2a2019-11-29 13:25:21 -080021
22namespace aos {
23namespace logger {
24
25namespace chrono = std::chrono;
26
Austin Schuhe309d2a2019-11-29 13:25:21 -080027Logger::Logger(DetachedBufferWriter *writer, EventLoop *event_loop,
28 std::chrono::milliseconds polling_period)
29 : event_loop_(event_loop),
30 writer_(writer),
31 timer_handler_(event_loop_->AddTimer([this]() { DoLogData(); })),
32 polling_period_(polling_period) {
33 for (const Channel *channel : *event_loop_->configuration()->channels()) {
34 FetcherStruct fs;
Austin Schuh15649d62019-12-28 16:36:38 -080035 const bool is_readable =
36 configuration::ChannelIsReadableOnNode(channel, event_loop_->node());
37 const bool log_message = configuration::ChannelMessageIsLoggedOnNode(
38 channel, event_loop_->node()) &&
39 is_readable;
40
41 const bool log_delivery_times =
42 (event_loop_->node() == nullptr)
43 ? false
44 : configuration::ConnectionDeliveryTimeIsLoggedOnNode(
45 channel, event_loop_->node(), event_loop_->node());
46
47 if (log_message || log_delivery_times) {
48 fs.fetcher = event_loop->MakeRawFetcher(channel);
49 VLOG(1) << "Logging channel "
50 << configuration::CleanedChannelToString(channel);
51
52 if (log_delivery_times) {
53 if (log_message) {
54 VLOG(1) << " Logging message and delivery times";
55 fs.log_type = LogType::kLogMessageAndDeliveryTime;
56 } else {
57 VLOG(1) << " Logging delivery times only";
58 fs.log_type = LogType::kLogDeliveryTimeOnly;
59 }
60 } else {
61 // We don't have a particularly great use case right now for logging a
62 // forwarded message, but either not logging the delivery times, or
63 // logging them on another node. Fail rather than produce bad results.
64 CHECK(configuration::ChannelIsSendableOnNode(channel,
65 event_loop_->node()))
66 << ": Logger only knows how to log remote messages with "
67 "forwarding timestamps.";
68 VLOG(1) << " Logging message only";
69 fs.log_type = LogType::kLogMessage;
70 }
71 }
72
Austin Schuhe309d2a2019-11-29 13:25:21 -080073 fs.written = false;
74 fetchers_.emplace_back(std::move(fs));
75 }
76
77 // When things start, we want to log the header, then the most recent messages
78 // available on each fetcher to capture the previous state, then start
79 // polling.
80 event_loop_->OnRun([this, polling_period]() {
81 // Grab data from each channel right before we declare the log file started
82 // so we can capture the latest message on each channel. This lets us have
83 // non periodic messages with configuration that now get logged.
84 for (FetcherStruct &f : fetchers_) {
Austin Schuh15649d62019-12-28 16:36:38 -080085 if (f.fetcher.get() != nullptr) {
86 f.written = !f.fetcher->Fetch();
87 }
Austin Schuhe309d2a2019-11-29 13:25:21 -080088 }
89
90 // We need to pick a point in time to declare the log file "started". This
91 // starts here. It needs to be after everything is fetched so that the
92 // fetchers are all pointed at the most recent message before the start
93 // time.
94 const monotonic_clock::time_point monotonic_now =
95 event_loop_->monotonic_now();
96 const realtime_clock::time_point realtime_now = event_loop_->realtime_now();
97 last_synchronized_time_ = monotonic_now;
98
99 {
100 // Now write the header with this timestamp in it.
101 flatbuffers::FlatBufferBuilder fbb;
102 fbb.ForceDefaults(1);
103
104 flatbuffers::Offset<aos::Configuration> configuration_offset =
105 CopyFlatBuffer(event_loop_->configuration(), &fbb);
106
Austin Schuh288479d2019-12-18 19:47:52 -0800107 flatbuffers::Offset<flatbuffers::String> string_offset =
108 fbb.CreateString(network::GetHostname());
109
Austin Schuhfd960622020-01-01 13:22:55 -0800110 flatbuffers::Offset<Node> node_offset;
111 if (event_loop_->node() != nullptr) {
112 node_offset = CopyFlatBuffer(event_loop_->node(), &fbb);
113 }
Austin Schuh15649d62019-12-28 16:36:38 -0800114 LOG(INFO) << "Logging node as " << FlatbufferToJson(event_loop_->node());
115
Austin Schuhe309d2a2019-11-29 13:25:21 -0800116 aos::logger::LogFileHeader::Builder log_file_header_builder(fbb);
117
Austin Schuh288479d2019-12-18 19:47:52 -0800118 log_file_header_builder.add_name(string_offset);
119
Austin Schuhfd960622020-01-01 13:22:55 -0800120 // Only add the node if we are running in a multinode configuration.
121 if (event_loop_->node() != nullptr) {
122 log_file_header_builder.add_node(node_offset);
123 }
Austin Schuh15649d62019-12-28 16:36:38 -0800124
Austin Schuhe309d2a2019-11-29 13:25:21 -0800125 log_file_header_builder.add_configuration(configuration_offset);
126 // The worst case theoretical out of order is the polling period times 2.
127 // One message could get logged right after the boundary, but be for right
128 // before the next boundary. And the reverse could happen for another
129 // message. Report back 3x to be extra safe, and because the cost isn't
130 // huge on the read side.
131 log_file_header_builder.add_max_out_of_order_duration(
132 std::chrono::duration_cast<std::chrono::nanoseconds>(3 *
133 polling_period)
134 .count());
135
Austin Schuh629c9172019-12-23 20:34:43 -0800136 log_file_header_builder.add_monotonic_start_time(
Austin Schuhe309d2a2019-11-29 13:25:21 -0800137 std::chrono::duration_cast<std::chrono::nanoseconds>(
138 monotonic_now.time_since_epoch())
139 .count());
Austin Schuh629c9172019-12-23 20:34:43 -0800140 log_file_header_builder.add_realtime_start_time(
Austin Schuhe309d2a2019-11-29 13:25:21 -0800141 std::chrono::duration_cast<std::chrono::nanoseconds>(
142 realtime_now.time_since_epoch())
143 .count());
144
145 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
146 writer_->QueueSizedFlatbuffer(&fbb);
147 }
148
149 timer_handler_->Setup(event_loop_->monotonic_now() + polling_period,
150 polling_period);
151 });
152}
153
154void Logger::DoLogData() {
155 // We want to guarentee that messages aren't out of order by more than
156 // max_out_of_order_duration. To do this, we need sync points. Every write
157 // cycle should be a sync point.
158 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
159
160 do {
161 // Move the sync point up by at most polling_period. This forces one sync
162 // per iteration, even if it is small.
163 last_synchronized_time_ =
164 std::min(last_synchronized_time_ + polling_period_, monotonic_now);
165 size_t channel_index = 0;
166 // Write each channel to disk, one at a time.
167 for (FetcherStruct &f : fetchers_) {
Austin Schuh15649d62019-12-28 16:36:38 -0800168 // Skip any channels which we aren't supposed to log.
169 if (f.fetcher.get() != nullptr) {
170 while (true) {
171 if (f.written) {
172 if (!f.fetcher->FetchNext()) {
173 VLOG(2) << "No new data on "
174 << configuration::CleanedChannelToString(
175 f.fetcher->channel());
176 break;
177 } else {
178 f.written = false;
179 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800180 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800181
Austin Schuh15649d62019-12-28 16:36:38 -0800182 CHECK(!f.written);
183
184 // TODO(james): Write tests to exercise this logic.
185 if (f.fetcher->context().monotonic_event_time <
186 last_synchronized_time_) {
187 // Write!
188 flatbuffers::FlatBufferBuilder fbb(f.fetcher->context().size +
189 max_header_size_);
190 fbb.ForceDefaults(1);
191
192 fbb.FinishSizePrefixed(PackMessage(&fbb, f.fetcher->context(),
193 channel_index, f.log_type));
194
195 VLOG(2) << "Writing data for channel "
196 << configuration::CleanedChannelToString(
197 f.fetcher->channel());
198
199 max_header_size_ = std::max(
200 max_header_size_, fbb.GetSize() - f.fetcher->context().size);
201 writer_->QueueSizedFlatbuffer(&fbb);
202
203 f.written = true;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800204 } else {
Austin Schuh15649d62019-12-28 16:36:38 -0800205 break;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800206 }
207 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800208 }
209
210 ++channel_index;
211 }
212
213 CHECK_EQ(channel_index, fetchers_.size());
214
215 // If we missed cycles, we could be pretty far behind. Spin until we are
216 // caught up.
217 } while (last_synchronized_time_ + polling_period_ < monotonic_now);
218
219 writer_->Flush();
220}
221
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800222LogReader::LogReader(std::string_view filename,
223 const Configuration *replay_configuration)
224 : sorted_message_reader_(filename),
225 replay_configuration_(replay_configuration) {
226 channels_.resize(logged_configuration()->channels()->size());
Austin Schuh6331ef92020-01-07 18:28:09 -0800227 MakeRemappedConfig();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800228}
229
James Kuszmaul7daef362019-12-31 18:28:17 -0800230LogReader::~LogReader() {
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800231 Deregister();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800232}
233
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800234const Configuration *LogReader::logged_configuration() const {
Austin Schuh05b70472020-01-01 17:11:17 -0800235 return sorted_message_reader_.configuration();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800236}
237
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800238const Configuration *LogReader::configuration() const {
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800239 return remapped_configuration_;
240}
241
242const Node *LogReader::node() const {
243 // Because the Node pointer will only be valid if it actually points to memory
244 // owned by remapped_configuration_, we need to wait for the
245 // remapped_configuration_ to be populated before accessing it.
246 CHECK(remapped_configuration_ != nullptr)
247 << ": Need to call Register before the node() pointer will be valid.";
248 if (sorted_message_reader_.node() == nullptr) {
249 return nullptr;
250 }
251 return configuration::GetNode(
252 configuration(), sorted_message_reader_.node()->name()->string_view());
253}
Austin Schuh15649d62019-12-28 16:36:38 -0800254
Austin Schuhe309d2a2019-11-29 13:25:21 -0800255monotonic_clock::time_point LogReader::monotonic_start_time() {
Austin Schuh05b70472020-01-01 17:11:17 -0800256 return sorted_message_reader_.monotonic_start_time();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800257}
258
259realtime_clock::time_point LogReader::realtime_start_time() {
Austin Schuh05b70472020-01-01 17:11:17 -0800260 return sorted_message_reader_.realtime_start_time();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800261}
262
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800263void LogReader::Register() {
264 event_loop_factory_unique_ptr_ =
265 std::make_unique<SimulatedEventLoopFactory>(configuration(), node());
266 Register(event_loop_factory_unique_ptr_.get());
267}
268
Austin Schuh92547522019-12-28 14:33:43 -0800269void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) {
Austin Schuh92547522019-12-28 14:33:43 -0800270 event_loop_factory_ = event_loop_factory;
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800271 event_loop_unique_ptr_ = event_loop_factory_->MakeEventLoop("log_reader");
Austin Schuh92547522019-12-28 14:33:43 -0800272 // We don't run timing reports when trying to print out logged data, because
273 // otherwise we would end up printing out the timing reports themselves...
274 // This is only really relevant when we are replaying into a simulation.
275 event_loop_unique_ptr_->SkipTimingReport();
276
277 Register(event_loop_unique_ptr_.get());
278 event_loop_factory_->RunFor(monotonic_start_time() -
Austin Schuha5e14192020-01-06 18:02:41 -0800279 event_loop_->monotonic_now());
Austin Schuh92547522019-12-28 14:33:43 -0800280}
281
Austin Schuhe309d2a2019-11-29 13:25:21 -0800282void LogReader::Register(EventLoop *event_loop) {
283 event_loop_ = event_loop;
284
Austin Schuh39788ff2019-12-01 18:22:57 -0800285 // Otherwise we replay the timing report and try to resend it...
286 event_loop_->SkipTimingReport();
287
Austin Schuhe309d2a2019-11-29 13:25:21 -0800288 for (size_t i = 0; i < channels_.size(); ++i) {
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800289 const Channel *const original_channel =
290 logged_configuration()->channels()->Get(i);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800291
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800292 std::string_view channel_name = original_channel->name()->string_view();
293 std::string_view channel_type = original_channel->type()->string_view();
294 // If the channel is remapped, find the correct channel name to use.
295 if (remapped_channels_.count(i) > 0) {
296 VLOG(2) << "Got remapped channel on "
297 << configuration::CleanedChannelToString(original_channel);
298 channel_name = remapped_channels_[i];
299 }
Austin Schuh6331ef92020-01-07 18:28:09 -0800300
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800301 VLOG(1) << "Going to remap channel " << channel_name << " " << channel_type;
Austin Schuh6331ef92020-01-07 18:28:09 -0800302 const Channel *channel = configuration::GetChannel(
303 event_loop_->configuration(), channel_name, channel_type,
304 event_loop_->name(), event_loop_->node());
305
306 CHECK(channel != nullptr)
307 << ": Unable to send {\"name\": \"" << channel_name
308 << "\", \"type\": \"" << channel_type
309 << "\"} because it is not in the provided configuration.";
310
311 channels_[i] = event_loop_->MakeRawSender(channel);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800312 }
313
314 timer_handler_ = event_loop_->AddTimer([this]() {
James Kuszmaul314f1672020-01-03 20:02:08 -0800315 if (sorted_message_reader_.active_channel_count() == 0u) {
316 event_loop_factory_->Exit();
317 return;
318 }
Austin Schuh05b70472020-01-01 17:11:17 -0800319 monotonic_clock::time_point channel_timestamp;
320 int channel_index;
321 FlatbufferVector<MessageHeader> channel_data =
322 FlatbufferVector<MessageHeader>::Empty();
323
324 std::tie(channel_timestamp, channel_index, channel_data) =
325 sorted_message_reader_.PopOldestChannel();
326
Austin Schuhe309d2a2019-11-29 13:25:21 -0800327 const monotonic_clock::time_point monotonic_now =
Austin Schuhad154822019-12-27 15:45:13 -0800328 event_loop_->context().monotonic_event_time;
Austin Schuh05b70472020-01-01 17:11:17 -0800329 CHECK(monotonic_now == channel_timestamp)
Austin Schuhe309d2a2019-11-29 13:25:21 -0800330 << ": Now " << monotonic_now.time_since_epoch().count()
Austin Schuh05b70472020-01-01 17:11:17 -0800331 << " trying to send " << channel_timestamp.time_since_epoch().count();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800332
Austin Schuh05b70472020-01-01 17:11:17 -0800333 if (channel_timestamp > monotonic_start_time() ||
Austin Schuh15649d62019-12-28 16:36:38 -0800334 event_loop_factory_ != nullptr) {
335 if (!FLAGS_skip_missing_forwarding_entries ||
Austin Schuh05b70472020-01-01 17:11:17 -0800336 channel_data.message().data() != nullptr) {
337 CHECK(channel_data.message().data() != nullptr)
338 << ": Got a message without data. Forwarding entry which was "
339 "not "
Austin Schuh15649d62019-12-28 16:36:38 -0800340 "matched? Use --skip_missing_forwarding_entries to ignore "
341 "this.";
Austin Schuh92547522019-12-28 14:33:43 -0800342
Austin Schuh15649d62019-12-28 16:36:38 -0800343 // If we have access to the factory, use it to fix the realtime time.
344 if (event_loop_factory_ != nullptr) {
345 event_loop_factory_->SetRealtimeOffset(
Austin Schuh05b70472020-01-01 17:11:17 -0800346 monotonic_clock::time_point(chrono::nanoseconds(
347 channel_data.message().monotonic_sent_time())),
348 realtime_clock::time_point(chrono::nanoseconds(
349 channel_data.message().realtime_sent_time())));
Austin Schuh15649d62019-12-28 16:36:38 -0800350 }
351
Austin Schuh05b70472020-01-01 17:11:17 -0800352 channels_[channel_index]->Send(
353 channel_data.message().data()->Data(),
354 channel_data.message().data()->size(),
355 monotonic_clock::time_point(chrono::nanoseconds(
356 channel_data.message().monotonic_remote_time())),
357 realtime_clock::time_point(chrono::nanoseconds(
358 channel_data.message().realtime_remote_time())),
359 channel_data.message().remote_queue_index());
Austin Schuh92547522019-12-28 14:33:43 -0800360 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800361 } else {
362 LOG(WARNING) << "Not sending data from before the start of the log file. "
Austin Schuh05b70472020-01-01 17:11:17 -0800363 << channel_timestamp.time_since_epoch().count() << " start "
Austin Schuhe309d2a2019-11-29 13:25:21 -0800364 << monotonic_start_time().time_since_epoch().count() << " "
Austin Schuh05b70472020-01-01 17:11:17 -0800365 << FlatbufferToJson(channel_data);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800366 }
367
Austin Schuh05b70472020-01-01 17:11:17 -0800368 if (sorted_message_reader_.active_channel_count() > 0u) {
369 timer_handler_->Setup(sorted_message_reader_.oldest_message().first);
James Kuszmaul314f1672020-01-03 20:02:08 -0800370 } else {
371 // Set a timer up immediately after now to die. If we don't do this, then
372 // the senders waiting on the message we just read will never get called.
373 timer_handler_->Setup(monotonic_now + event_loop_factory_->send_delay() +
374 std::chrono::nanoseconds(1));
Austin Schuhe309d2a2019-11-29 13:25:21 -0800375 }
376 });
377
Austin Schuh05b70472020-01-01 17:11:17 -0800378 if (sorted_message_reader_.active_channel_count() > 0u) {
379 event_loop_->OnRun([this]() {
380 timer_handler_->Setup(sorted_message_reader_.oldest_message().first);
381 });
Austin Schuhe309d2a2019-11-29 13:25:21 -0800382 }
383}
384
385void LogReader::Deregister() {
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800386 // Make sure that things get destroyed in the correct order, rather than
387 // relying on getting the order correct in the class definition.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800388 for (size_t i = 0; i < channels_.size(); ++i) {
Austin Schuh05b70472020-01-01 17:11:17 -0800389 channels_[i].reset();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800390 }
Austin Schuh92547522019-12-28 14:33:43 -0800391
Austin Schuh92547522019-12-28 14:33:43 -0800392 event_loop_unique_ptr_.reset();
393 event_loop_ = nullptr;
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800394 event_loop_factory_unique_ptr_.reset();
395 event_loop_factory_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800396}
397
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800398void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type,
399 std::string_view add_prefix) {
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800400 for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) {
401 const Channel *const channel = logged_configuration()->channels()->Get(ii);
402 if (channel->name()->str() == name &&
403 channel->type()->string_view() == type) {
404 CHECK_EQ(0u, remapped_channels_.count(ii))
405 << "Already remapped channel "
406 << configuration::CleanedChannelToString(channel);
407 remapped_channels_[ii] = std::string(add_prefix) + std::string(name);
408 VLOG(1) << "Remapping channel "
409 << configuration::CleanedChannelToString(channel)
410 << " to have name " << remapped_channels_[ii];
Austin Schuh6331ef92020-01-07 18:28:09 -0800411 MakeRemappedConfig();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800412 return;
413 }
414 }
415 LOG(FATAL) << "Unabled to locate channel with name " << name << " and type "
416 << type;
417}
418
419void LogReader::MakeRemappedConfig() {
420 // If no remapping occurred and we are using the original config, then there
421 // is nothing interesting to do here.
422 if (remapped_channels_.empty() && replay_configuration_ == nullptr) {
423 remapped_configuration_ = sorted_message_reader_.configuration();
424 return;
425 }
426 // Config to copy Channel definitions from. Use the specified
427 // replay_configuration_ if it has been provided.
428 const Configuration *const base_config = replay_configuration_ == nullptr
429 ? logged_configuration()
430 : replay_configuration_;
431 // The remapped config will be identical to the base_config, except that it
432 // will have a bunch of extra channels in the channel list, which are exact
433 // copies of the remapped channels, but with different names.
434 // Because the flatbuffers API is a pain to work with, this requires a bit of
435 // a song-and-dance to get copied over.
436 // The order of operations is to:
437 // 1) Make a flatbuffer builder for a config that will just contain a list of
438 // the new channels that we want to add.
439 // 2) For each channel that we are remapping:
440 // a) Make a buffer/builder and construct into it a Channel table that only
441 // contains the new name for the channel.
442 // b) Merge the new channel with just the name into the channel that we are
443 // trying to copy, built in the flatbuffer builder made in 1. This gives
444 // us the new channel definition that we need.
445 // 3) Using this list of offsets, build the Configuration of just new
446 // Channels.
447 // 4) Merge the Configuration with the new Channels into the base_config.
448 // 5) Call MergeConfiguration() on that result to give MergeConfiguration a
449 // chance to sanitize the config.
450
451 // This is the builder that we use for the config containing all the new
452 // channels.
453 flatbuffers::FlatBufferBuilder new_config_fbb;
454 new_config_fbb.ForceDefaults(1);
455 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
456 for (auto &pair : remapped_channels_) {
457 // This is the builder that we use for creating the Channel with just the
458 // new name.
459 flatbuffers::FlatBufferBuilder new_name_fbb;
460 new_name_fbb.ForceDefaults(1);
461 const flatbuffers::Offset<flatbuffers::String> name_offset =
462 new_name_fbb.CreateString(pair.second);
463 ChannelBuilder new_name_builder(new_name_fbb);
464 new_name_builder.add_name(name_offset);
465 new_name_fbb.Finish(new_name_builder.Finish());
466 const FlatbufferDetachedBuffer<Channel> new_name = new_name_fbb.Release();
467 // Retrieve the channel that we want to copy, confirming that it is actually
468 // present in base_config.
469 const Channel *const base_channel = CHECK_NOTNULL(configuration::GetChannel(
470 base_config, logged_configuration()->channels()->Get(pair.first), "",
471 nullptr));
472 // Actually create the new channel and put it into the vector of Offsets
473 // that we will use to create the new Configuration.
474 channel_offsets.emplace_back(MergeFlatBuffers<Channel>(
475 reinterpret_cast<const flatbuffers::Table *>(base_channel),
476 reinterpret_cast<const flatbuffers::Table *>(&new_name.message()),
477 &new_config_fbb));
478 }
479 // Create the Configuration containing the new channels that we want to add.
480 const auto
481 new_name_vector_offsets = new_config_fbb.CreateVector(channel_offsets);
482 ConfigurationBuilder new_config_builder(new_config_fbb);
483 new_config_builder.add_channels(new_name_vector_offsets);
484 new_config_fbb.Finish(new_config_builder.Finish());
485 const FlatbufferDetachedBuffer<Configuration> new_name_config =
486 new_config_fbb.Release();
487 // Merge the new channels configuration into the base_config, giving us the
488 // remapped configuration.
489 remapped_configuration_buffer_ =
490 std::make_unique<FlatbufferDetachedBuffer<Configuration>>(
491 MergeFlatBuffers<Configuration>(base_config,
492 &new_name_config.message()));
493 // Call MergeConfiguration to deal with sanitizing the config.
494 remapped_configuration_buffer_ =
495 std::make_unique<FlatbufferDetachedBuffer<Configuration>>(
496 configuration::MergeConfiguration(*remapped_configuration_buffer_));
497
498 remapped_configuration_ = &remapped_configuration_buffer_->message();
499}
500
Austin Schuhe309d2a2019-11-29 13:25:21 -0800501} // namespace logger
502} // namespace aos