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