Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 1 | #include <algorithm> |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 2 | #include <iostream> |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 3 | #include <memory> |
| 4 | #include <optional> |
| 5 | #include <string> |
| 6 | #include <string_view> |
| 7 | #include <vector> |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 8 | |
Austin Schuh | 0e8db66 | 2021-07-06 10:43:47 -0700 | [diff] [blame] | 9 | #include "absl/strings/escaping.h" |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 10 | #include "aos/aos_cli_utils.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 11 | #include "aos/configuration.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 12 | #include "aos/events/logging/log_reader.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 13 | #include "aos/events/simulated_event_loop.h" |
| 14 | #include "aos/init.h" |
| 15 | #include "aos/json_to_flatbuffer.h" |
| 16 | #include "gflags/gflags.h" |
| 17 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 18 | DEFINE_string( |
| 19 | name, "", |
| 20 | "Name to match for printing out channels. Empty means no name filter."); |
| 21 | DEFINE_string(type, "", |
| 22 | "Channel type to match for printing out channels. Empty means no " |
| 23 | "type filter."); |
Austin Schuh | 041fe9f | 2021-10-16 23:01:15 -0700 | [diff] [blame] | 24 | DEFINE_bool(json, false, "If true, print fully valid JSON"); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 25 | DEFINE_bool(fetch, false, |
| 26 | "If true, also print out the messages from before the start of the " |
| 27 | "log file"); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 28 | DEFINE_bool(raw, false, |
| 29 | "If true, just print the data out unsorted and unparsed"); |
Brian Silverman | 8ff74aa | 2021-02-05 16:37:15 -0800 | [diff] [blame] | 30 | DEFINE_string(raw_header, "", |
| 31 | "If set, the file to read the header from in raw mode"); |
Austin Schuh | ff3bc90 | 2022-05-11 16:10:57 -0700 | [diff] [blame] | 32 | DEFINE_bool(distributed_clock, false, |
| 33 | "If true, print out the distributed time"); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 34 | DEFINE_bool(format_raw, true, |
| 35 | "If true and --raw is specified, print out raw data, but use the " |
| 36 | "schema to format the data."); |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 37 | DEFINE_int64(max_vector_size, 100, |
Austin Schuh | ae46f36 | 2020-04-11 19:52:56 -0700 | [diff] [blame] | 38 | "If positive, vectors longer than this will not be printed"); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 39 | DEFINE_bool(pretty, false, |
| 40 | "If true, pretty print the messages on multiple lines"); |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 41 | DEFINE_bool( |
| 42 | pretty_max, false, |
| 43 | "If true, expand every field to its own line (expands more than -pretty)"); |
| 44 | DEFINE_bool(print_timestamps, true, "If true, timestamps are printed."); |
Austin Schuh | 569c7f9 | 2020-12-11 20:01:42 -0800 | [diff] [blame] | 45 | DEFINE_bool(print, true, |
| 46 | "If true, actually print the messages. If false, discard them, " |
| 47 | "confirming they can be parsed."); |
Tyler Chatow | ee0afa8 | 2021-08-01 22:00:36 -0700 | [diff] [blame] | 48 | DEFINE_uint64( |
| 49 | count, 0, |
| 50 | "If >0, log_cat will exit after printing this many messages. This " |
| 51 | "includes messages from before the start of the log if --fetch is set."); |
Austin Schuh | 7af06d5 | 2021-06-28 15:46:59 -0700 | [diff] [blame] | 52 | DEFINE_bool(print_parts_only, false, |
| 53 | "If true, only print out the results of logfile sorting."); |
Austin Schuh | 25b1765 | 2021-07-21 15:42:56 -0700 | [diff] [blame] | 54 | DEFINE_bool(channels, false, |
| 55 | "If true, print out all the configured channels for this log."); |
Milind Upadhyay | 184dfda | 2022-03-26 15:54:38 -0700 | [diff] [blame] | 56 | DEFINE_double(monotonic_start_time, 0.0, |
| 57 | "If set, only print messages sent at or after this many seconds " |
| 58 | "after epoch."); |
| 59 | DEFINE_double(monotonic_end_time, 0.0, |
| 60 | "If set, only print messages sent at or before this many seconds " |
| 61 | "after epoch."); |
Brian J Griglak | 043e0e2 | 2022-08-18 12:51:18 -0600 | [diff] [blame] | 62 | DEFINE_bool(use_hex, false, "Are integers in the messages printed in hex notation."); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 63 | |
Austin Schuh | 041fe9f | 2021-10-16 23:01:15 -0700 | [diff] [blame] | 64 | using aos::monotonic_clock; |
| 65 | namespace chrono = std::chrono; |
| 66 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 67 | // Prints out raw log parts to stdout. |
| 68 | int PrintRaw(int argc, char **argv) { |
Austin Schuh | db60509 | 2022-09-26 15:16:53 -0700 | [diff] [blame] | 69 | if (argc == 1) { |
| 70 | CHECK(!FLAGS_raw_header.empty()); |
| 71 | aos::logger::MessageReader raw_header_reader(FLAGS_raw_header); |
| 72 | std::cout << aos::FlatbufferToJson(raw_header_reader.raw_log_file_header(), |
| 73 | {.multi_line = FLAGS_pretty, |
| 74 | .max_vector_size = static_cast<size_t>( |
| 75 | FLAGS_max_vector_size)}) |
| 76 | << std::endl; |
| 77 | return 0; |
| 78 | } |
| 79 | if (argc != 2 && argc != 1) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 80 | LOG(FATAL) << "Expected 1 logfile as an argument."; |
| 81 | } |
| 82 | aos::logger::SpanReader reader(argv[1]); |
| 83 | absl::Span<const uint8_t> raw_log_file_header_span = reader.ReadMessage(); |
| 84 | |
| 85 | if (raw_log_file_header_span == absl::Span<const uint8_t>()) { |
| 86 | LOG(WARNING) << "Empty log file on " << reader.filename(); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | // Now, reproduce the log file header deduplication logic inline so we can |
| 91 | // print out all the headers we find. |
| 92 | aos::SizePrefixedFlatbufferVector<aos::logger::LogFileHeader> log_file_header( |
| 93 | raw_log_file_header_span); |
| 94 | if (!log_file_header.Verify()) { |
| 95 | LOG(ERROR) << "Header corrupted on " << reader.filename(); |
| 96 | return 1; |
| 97 | } |
| 98 | while (true) { |
| 99 | absl::Span<const uint8_t> maybe_header_data = reader.PeekMessage(); |
| 100 | if (maybe_header_data == absl::Span<const uint8_t>()) { |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | aos::SizePrefixedFlatbufferSpan<aos::logger::LogFileHeader> maybe_header( |
| 105 | maybe_header_data); |
| 106 | if (maybe_header.Verify()) { |
| 107 | std::cout << aos::FlatbufferToJson( |
| 108 | log_file_header, {.multi_line = FLAGS_pretty, |
| 109 | .max_vector_size = static_cast<size_t>( |
| 110 | FLAGS_max_vector_size)}) |
| 111 | << std::endl; |
| 112 | LOG(WARNING) << "Found duplicate LogFileHeader in " << reader.filename(); |
| 113 | log_file_header = |
| 114 | aos::SizePrefixedFlatbufferVector<aos::logger::LogFileHeader>( |
| 115 | maybe_header_data); |
| 116 | |
| 117 | reader.ConsumeMessage(); |
| 118 | } else { |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // And now use the final sha256 to match the raw_header. |
| 124 | std::optional<aos::logger::MessageReader> raw_header_reader; |
| 125 | const aos::logger::LogFileHeader *full_header = &log_file_header.message(); |
| 126 | if (!FLAGS_raw_header.empty()) { |
| 127 | raw_header_reader.emplace(FLAGS_raw_header); |
| 128 | std::cout << aos::FlatbufferToJson(full_header, |
| 129 | {.multi_line = FLAGS_pretty, |
| 130 | .max_vector_size = static_cast<size_t>( |
| 131 | FLAGS_max_vector_size)}) |
| 132 | << std::endl; |
| 133 | CHECK_EQ( |
| 134 | full_header->configuration_sha256()->string_view(), |
| 135 | aos::logger::Sha256(raw_header_reader->raw_log_file_header().span())); |
| 136 | full_header = raw_header_reader->log_file_header(); |
| 137 | } |
| 138 | |
| 139 | if (!FLAGS_print) { |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | std::cout << aos::FlatbufferToJson(full_header, |
| 144 | {.multi_line = FLAGS_pretty, |
| 145 | .max_vector_size = static_cast<size_t>( |
| 146 | FLAGS_max_vector_size)}) |
| 147 | << std::endl; |
| 148 | CHECK(full_header->has_configuration()) |
| 149 | << ": Missing configuration! You may want to provide the path to the " |
| 150 | "logged configuration file using the --raw_header flag."; |
| 151 | |
| 152 | while (true) { |
| 153 | const aos::SizePrefixedFlatbufferSpan<aos::logger::MessageHeader> message( |
| 154 | reader.ReadMessage()); |
| 155 | if (message.span() == absl::Span<const uint8_t>()) { |
| 156 | break; |
| 157 | } |
| 158 | CHECK(message.Verify()); |
| 159 | |
| 160 | const auto *const channels = full_header->configuration()->channels(); |
| 161 | const size_t channel_index = message.message().channel_index(); |
| 162 | CHECK_LT(channel_index, channels->size()); |
| 163 | const aos::Channel *const channel = channels->Get(channel_index); |
| 164 | |
| 165 | CHECK(message.Verify()) << absl::BytesToHexString( |
| 166 | std::string_view(reinterpret_cast<const char *>(message.span().data()), |
| 167 | message.span().size())); |
| 168 | |
| 169 | if (message.message().data() != nullptr) { |
| 170 | CHECK(channel->has_schema()); |
| 171 | |
| 172 | CHECK(flatbuffers::Verify( |
| 173 | *channel->schema(), *channel->schema()->root_table(), |
| 174 | message.message().data()->data(), message.message().data()->size())) |
| 175 | << ": Corrupted flatbuffer on " << channel->name()->c_str() << " " |
| 176 | << channel->type()->c_str(); |
| 177 | } |
| 178 | |
| 179 | if (FLAGS_format_raw && message.message().data() != nullptr) { |
| 180 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
| 181 | << aos::FlatbufferToJson(message, {.multi_line = FLAGS_pretty, |
| 182 | .max_vector_size = 4}) |
| 183 | << ": " |
| 184 | << aos::FlatbufferToJson( |
| 185 | channel->schema(), message.message().data()->data(), |
| 186 | {FLAGS_pretty, |
| 187 | static_cast<size_t>(FLAGS_max_vector_size)}) |
| 188 | << std::endl; |
| 189 | } else { |
| 190 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
| 191 | << aos::FlatbufferToJson( |
| 192 | message, {FLAGS_pretty, |
| 193 | static_cast<size_t>(FLAGS_max_vector_size)}) |
| 194 | << std::endl; |
| 195 | } |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | // This class prints out all data from a node on a boot. |
| 201 | class NodePrinter { |
| 202 | public: |
| 203 | NodePrinter(aos::EventLoop *event_loop, uint64_t *message_print_counter, |
| 204 | aos::SimulatedEventLoopFactory *factory, |
| 205 | aos::FastStringBuilder *builder) |
| 206 | : factory_(factory), |
Austin Schuh | ff3bc90 | 2022-05-11 16:10:57 -0700 | [diff] [blame] | 207 | node_factory_(factory->GetNodeEventLoopFactory(event_loop->node())), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 208 | event_loop_(event_loop), |
| 209 | message_print_counter_(message_print_counter), |
| 210 | node_name_( |
| 211 | event_loop_->node() == nullptr |
| 212 | ? "" |
Austin Schuh | 041fe9f | 2021-10-16 23:01:15 -0700 | [diff] [blame] | 213 | : std::string(event_loop->node()->name()->string_view())), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 214 | builder_(builder) { |
| 215 | event_loop_->SkipTimingReport(); |
| 216 | event_loop_->SkipAosLog(); |
| 217 | |
| 218 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 219 | event_loop_->configuration()->channels(); |
| 220 | |
Milind Upadhyay | 184dfda | 2022-03-26 15:54:38 -0700 | [diff] [blame] | 221 | const monotonic_clock::time_point start_time = |
| 222 | (FLAGS_monotonic_start_time == 0.0 |
| 223 | ? monotonic_clock::min_time |
| 224 | : monotonic_clock::time_point( |
| 225 | std::chrono::duration_cast<monotonic_clock::duration>( |
| 226 | std::chrono::duration<double>( |
| 227 | FLAGS_monotonic_start_time)))); |
| 228 | const monotonic_clock::time_point end_time = |
| 229 | (FLAGS_monotonic_end_time == 0.0 |
| 230 | ? monotonic_clock::max_time |
| 231 | : monotonic_clock::time_point( |
| 232 | std::chrono::duration_cast<monotonic_clock::duration>( |
| 233 | std::chrono::duration<double>( |
| 234 | FLAGS_monotonic_end_time)))); |
| 235 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 236 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 237 | const aos::Channel *channel = channels->Get(i); |
| 238 | const flatbuffers::string_view name = channel->name()->string_view(); |
| 239 | const flatbuffers::string_view type = channel->type()->string_view(); |
| 240 | if (name.find(FLAGS_name) != std::string::npos && |
| 241 | type.find(FLAGS_type) != std::string::npos) { |
| 242 | if (!aos::configuration::ChannelIsReadableOnNode(channel, |
| 243 | event_loop_->node())) { |
| 244 | continue; |
| 245 | } |
| 246 | VLOG(1) << "Listening on " << name << " " << type; |
| 247 | |
| 248 | CHECK_NOTNULL(channel->schema()); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 249 | event_loop_->MakeRawWatcher(channel, [this, channel, start_time, |
| 250 | end_time]( |
| 251 | const aos::Context &context, |
| 252 | const void * /*message*/) { |
| 253 | if (!FLAGS_print) { |
| 254 | return; |
| 255 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 256 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 257 | if (!FLAGS_fetch && !started_) { |
| 258 | return; |
| 259 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 260 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 261 | if (context.monotonic_event_time < start_time || |
| 262 | context.monotonic_event_time > end_time) { |
| 263 | return; |
| 264 | } |
Milind Upadhyay | 184dfda | 2022-03-26 15:54:38 -0700 | [diff] [blame] | 265 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 266 | PrintMessage( |
| 267 | node_name_, node_factory_, channel, context, builder_, |
| 268 | { |
| 269 | .pretty = FLAGS_pretty, |
| 270 | .max_vector_size = static_cast<size_t>(FLAGS_max_vector_size), |
| 271 | .pretty_max = FLAGS_pretty_max, |
| 272 | .print_timestamps = FLAGS_print_timestamps, |
| 273 | .json = FLAGS_json, |
| 274 | .distributed_clock = FLAGS_distributed_clock, |
| 275 | .use_hex = FLAGS_use_hex, |
| 276 | }); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 277 | ++(*message_print_counter_); |
| 278 | if (FLAGS_count > 0 && *message_print_counter_ >= FLAGS_count) { |
| 279 | factory_->Exit(); |
| 280 | } |
| 281 | }); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void SetStarted(bool started, aos::monotonic_clock::time_point monotonic_now, |
| 287 | aos::realtime_clock::time_point realtime_now) { |
| 288 | started_ = started; |
Austin Schuh | 041fe9f | 2021-10-16 23:01:15 -0700 | [diff] [blame] | 289 | if (FLAGS_json) { |
| 290 | return; |
| 291 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 292 | if (started_) { |
| 293 | std::cout << std::endl; |
| 294 | std::cout << (event_loop_->node() != nullptr |
| 295 | ? (event_loop_->node()->name()->str() + " ") |
| 296 | : "") |
| 297 | << "Log starting at " << realtime_now << " (" << monotonic_now |
| 298 | << ")"; |
| 299 | std::cout << std::endl << std::endl; |
| 300 | } else { |
| 301 | std::cout << std::endl; |
| 302 | std::cout << (event_loop_->node() != nullptr |
| 303 | ? (event_loop_->node()->name()->str() + " ") |
| 304 | : "") |
| 305 | << "Log shutting down at " << realtime_now << " (" |
| 306 | << monotonic_now << ")"; |
| 307 | std::cout << std::endl << std::endl; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private: |
| 312 | struct MessageInfo { |
| 313 | std::string node_name; |
| 314 | std::unique_ptr<aos::RawFetcher> fetcher; |
| 315 | }; |
| 316 | |
| 317 | aos::SimulatedEventLoopFactory *factory_; |
Austin Schuh | ff3bc90 | 2022-05-11 16:10:57 -0700 | [diff] [blame] | 318 | aos::NodeEventLoopFactory *node_factory_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 319 | aos::EventLoop *event_loop_; |
| 320 | |
| 321 | uint64_t *message_print_counter_ = nullptr; |
| 322 | |
| 323 | std::string node_name_; |
| 324 | |
| 325 | bool started_ = false; |
| 326 | |
| 327 | aos::FastStringBuilder *builder_; |
| 328 | }; |
| 329 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 330 | int main(int argc, char **argv) { |
| 331 | gflags::SetUsageMessage( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 332 | "Usage:\n" |
| 333 | " log_cat [args] logfile1 logfile2 ...\n" |
| 334 | "\n" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 335 | "This program provides a basic interface to dump data from a logfile to " |
| 336 | "stdout. Given a logfile, channel name filter, and type filter, it will " |
| 337 | "print all the messages in the logfile matching the filters. The message " |
| 338 | "filters work by taking the values of --name and --type and printing any " |
| 339 | "channel whose name contains --name as a substr and whose type contains " |
| 340 | "--type as a substr. Not specifying --name or --type leaves them free. " |
| 341 | "Calling this program without --name or --type specified prints out all " |
| 342 | "the logged data."); |
| 343 | aos::InitGoogle(&argc, &argv); |
| 344 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 345 | if (FLAGS_raw) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 346 | return PrintRaw(argc, argv); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 349 | if (argc < 2) { |
| 350 | LOG(FATAL) << "Expected at least 1 logfile as an argument."; |
| 351 | } |
| 352 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 353 | const std::vector<aos::logger::LogFile> logfiles = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 354 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)); |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 355 | |
Austin Schuh | fe3fb34 | 2021-01-16 18:50:37 -0800 | [diff] [blame] | 356 | for (auto &it : logfiles) { |
| 357 | VLOG(1) << it; |
Austin Schuh | 7af06d5 | 2021-06-28 15:46:59 -0700 | [diff] [blame] | 358 | if (FLAGS_print_parts_only) { |
| 359 | std::cout << it << std::endl; |
| 360 | } |
| 361 | } |
| 362 | if (FLAGS_print_parts_only) { |
| 363 | return 0; |
Austin Schuh | fe3fb34 | 2021-01-16 18:50:37 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 366 | aos::logger::LogReader reader(logfiles); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 367 | |
Austin Schuh | 25b1765 | 2021-07-21 15:42:56 -0700 | [diff] [blame] | 368 | if (FLAGS_channels) { |
| 369 | const aos::Configuration *config = reader.configuration(); |
| 370 | for (const aos::Channel *channel : *config->channels()) { |
| 371 | std::cout << channel->name()->c_str() << " " << channel->type()->c_str() |
| 372 | << '\n'; |
| 373 | } |
| 374 | return 0; |
| 375 | } |
| 376 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 377 | { |
| 378 | bool found_channel = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 379 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 380 | reader.configuration()->channels(); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 381 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 382 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 383 | const aos::Channel *channel = channels->Get(i); |
| 384 | const flatbuffers::string_view name = channel->name()->string_view(); |
| 385 | const flatbuffers::string_view type = channel->type()->string_view(); |
| 386 | if (name.find(FLAGS_name) != std::string::npos && |
| 387 | type.find(FLAGS_type) != std::string::npos) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 388 | found_channel = true; |
| 389 | } |
| 390 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 391 | if (!found_channel) { |
| 392 | LOG(FATAL) << "Could not find any channels"; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 393 | } |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 396 | aos::FastStringBuilder builder; |
James Kuszmaul | 912af07 | 2020-10-31 16:06:54 -0700 | [diff] [blame] | 397 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 398 | uint64_t message_print_counter = 0; |
| 399 | |
| 400 | std::vector<NodePrinter *> printers; |
Sanjay Narayanan | beb328c | 2021-09-01 16:24:20 -0700 | [diff] [blame] | 401 | printers.resize(aos::configuration::NodesCount(reader.configuration()), |
| 402 | nullptr); |
| 403 | |
| 404 | aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration()); |
| 405 | |
| 406 | reader.RegisterWithoutStarting(&event_loop_factory); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 407 | |
| 408 | for (const aos::Node *node : |
| 409 | aos::configuration::GetNodes(event_loop_factory.configuration())) { |
| 410 | size_t node_index = aos::configuration::GetNodeIndex( |
| 411 | event_loop_factory.configuration(), node); |
| 412 | // Spin up the printer, and hook up the SetStarted method so that it gets |
| 413 | // notified when the log starts and stops. |
| 414 | aos::NodeEventLoopFactory *node_factory = |
| 415 | event_loop_factory.GetNodeEventLoopFactory(node); |
| 416 | node_factory->OnStartup([&event_loop_factory, node_factory, |
| 417 | &message_print_counter, &builder, &printers, |
| 418 | node_index]() { |
| 419 | printers[node_index] = node_factory->AlwaysStart<NodePrinter>( |
| 420 | "printer", &message_print_counter, &event_loop_factory, &builder); |
| 421 | }); |
| 422 | node_factory->OnShutdown( |
| 423 | [&printers, node_index]() { printers[node_index] = nullptr; }); |
| 424 | |
| 425 | reader.OnStart(node, [&printers, node_index, node_factory]() { |
| 426 | CHECK(printers[node_index]); |
| 427 | printers[node_index]->SetStarted(true, node_factory->monotonic_now(), |
| 428 | node_factory->realtime_now()); |
| 429 | }); |
| 430 | reader.OnEnd(node, [&printers, node_index, node_factory]() { |
| 431 | CHECK(printers[node_index]); |
| 432 | printers[node_index]->SetStarted(false, node_factory->monotonic_now(), |
| 433 | node_factory->realtime_now()); |
| 434 | }); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | event_loop_factory.Run(); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 438 | |
Austin Schuh | 51a9259 | 2020-08-09 13:17:00 -0700 | [diff] [blame] | 439 | reader.Deregister(); |
| 440 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 441 | return 0; |
| 442 | } |