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