blob: 1f166e482732e8834b2bdcb40d1071651a17a78a [file] [log] [blame]
Brian Silverman9891b292020-06-23 16:34:22 -07001#include <algorithm>
James Kuszmaul38735e82019-12-07 16:42:06 -08002#include <iostream>
Brian Silverman9891b292020-06-23 16:34:22 -07003#include <memory>
4#include <optional>
5#include <string>
6#include <string_view>
7#include <vector>
James Kuszmaul38735e82019-12-07 16:42:06 -08008
Austin Schuh0e8db662021-07-06 10:43:47 -07009#include "absl/strings/escaping.h"
Austin Schuh893d7f42022-09-16 15:01:35 -070010#include "aos/aos_cli_utils.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080011#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -080012#include "aos/events/logging/log_reader.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080013#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 Kuszmaul38735e82019-12-07 16:42:06 -080018DEFINE_string(
19 name, "",
20 "Name to match for printing out channels. Empty means no name filter.");
21DEFINE_string(type, "",
22 "Channel type to match for printing out channels. Empty means no "
23 "type filter.");
Austin Schuh041fe9f2021-10-16 23:01:15 -070024DEFINE_bool(json, false, "If true, print fully valid JSON");
Austin Schuha81454b2020-05-12 19:58:36 -070025DEFINE_bool(fetch, false,
26 "If true, also print out the messages from before the start of the "
27 "log file");
Austin Schuh6f3babe2020-01-26 20:34:50 -080028DEFINE_bool(raw, false,
29 "If true, just print the data out unsorted and unparsed");
Brian Silverman8ff74aa2021-02-05 16:37:15 -080030DEFINE_string(raw_header, "",
31 "If set, the file to read the header from in raw mode");
Austin Schuhff3bc902022-05-11 16:10:57 -070032DEFINE_bool(distributed_clock, false,
33 "If true, print out the distributed time");
Austin Schuha81454b2020-05-12 19:58:36 -070034DEFINE_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 Schuh893d7f42022-09-16 15:01:35 -070037DEFINE_int64(max_vector_size, 100,
Austin Schuhae46f362020-04-11 19:52:56 -070038 "If positive, vectors longer than this will not be printed");
Ravago Jones5cc9df52020-09-02 21:29:58 -070039DEFINE_bool(pretty, false,
40 "If true, pretty print the messages on multiple lines");
Austin Schuh893d7f42022-09-16 15:01:35 -070041DEFINE_bool(
42 pretty_max, false,
43 "If true, expand every field to its own line (expands more than -pretty)");
44DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
Austin Schuh569c7f92020-12-11 20:01:42 -080045DEFINE_bool(print, true,
46 "If true, actually print the messages. If false, discard them, "
47 "confirming they can be parsed.");
Tyler Chatowee0afa82021-08-01 22:00:36 -070048DEFINE_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 Schuh7af06d52021-06-28 15:46:59 -070052DEFINE_bool(print_parts_only, false,
53 "If true, only print out the results of logfile sorting.");
Austin Schuh25b17652021-07-21 15:42:56 -070054DEFINE_bool(channels, false,
55 "If true, print out all the configured channels for this log.");
Milind Upadhyay184dfda2022-03-26 15:54:38 -070056DEFINE_double(monotonic_start_time, 0.0,
57 "If set, only print messages sent at or after this many seconds "
58 "after epoch.");
59DEFINE_double(monotonic_end_time, 0.0,
60 "If set, only print messages sent at or before this many seconds "
61 "after epoch.");
Brian J Griglak043e0e22022-08-18 12:51:18 -060062DEFINE_bool(use_hex, false, "Are integers in the messages printed in hex notation.");
Austin Schuh6f3babe2020-01-26 20:34:50 -080063
Austin Schuh041fe9f2021-10-16 23:01:15 -070064using aos::monotonic_clock;
65namespace chrono = std::chrono;
66
Austin Schuh58646e22021-08-23 23:51:46 -070067// Prints out raw log parts to stdout.
68int PrintRaw(int argc, char **argv) {
69 if (argc != 2) {
70 LOG(FATAL) << "Expected 1 logfile as an argument.";
71 }
72 aos::logger::SpanReader reader(argv[1]);
73 absl::Span<const uint8_t> raw_log_file_header_span = reader.ReadMessage();
74
75 if (raw_log_file_header_span == absl::Span<const uint8_t>()) {
76 LOG(WARNING) << "Empty log file on " << reader.filename();
77 return 0;
78 }
79
80 // Now, reproduce the log file header deduplication logic inline so we can
81 // print out all the headers we find.
82 aos::SizePrefixedFlatbufferVector<aos::logger::LogFileHeader> log_file_header(
83 raw_log_file_header_span);
84 if (!log_file_header.Verify()) {
85 LOG(ERROR) << "Header corrupted on " << reader.filename();
86 return 1;
87 }
88 while (true) {
89 absl::Span<const uint8_t> maybe_header_data = reader.PeekMessage();
90 if (maybe_header_data == absl::Span<const uint8_t>()) {
91 break;
92 }
93
94 aos::SizePrefixedFlatbufferSpan<aos::logger::LogFileHeader> maybe_header(
95 maybe_header_data);
96 if (maybe_header.Verify()) {
97 std::cout << aos::FlatbufferToJson(
98 log_file_header, {.multi_line = FLAGS_pretty,
99 .max_vector_size = static_cast<size_t>(
100 FLAGS_max_vector_size)})
101 << std::endl;
102 LOG(WARNING) << "Found duplicate LogFileHeader in " << reader.filename();
103 log_file_header =
104 aos::SizePrefixedFlatbufferVector<aos::logger::LogFileHeader>(
105 maybe_header_data);
106
107 reader.ConsumeMessage();
108 } else {
109 break;
110 }
111 }
112
113 // And now use the final sha256 to match the raw_header.
114 std::optional<aos::logger::MessageReader> raw_header_reader;
115 const aos::logger::LogFileHeader *full_header = &log_file_header.message();
116 if (!FLAGS_raw_header.empty()) {
117 raw_header_reader.emplace(FLAGS_raw_header);
118 std::cout << aos::FlatbufferToJson(full_header,
119 {.multi_line = FLAGS_pretty,
120 .max_vector_size = static_cast<size_t>(
121 FLAGS_max_vector_size)})
122 << std::endl;
123 CHECK_EQ(
124 full_header->configuration_sha256()->string_view(),
125 aos::logger::Sha256(raw_header_reader->raw_log_file_header().span()));
126 full_header = raw_header_reader->log_file_header();
127 }
128
129 if (!FLAGS_print) {
130 return 0;
131 }
132
133 std::cout << aos::FlatbufferToJson(full_header,
134 {.multi_line = FLAGS_pretty,
135 .max_vector_size = static_cast<size_t>(
136 FLAGS_max_vector_size)})
137 << std::endl;
138 CHECK(full_header->has_configuration())
139 << ": Missing configuration! You may want to provide the path to the "
140 "logged configuration file using the --raw_header flag.";
141
142 while (true) {
143 const aos::SizePrefixedFlatbufferSpan<aos::logger::MessageHeader> message(
144 reader.ReadMessage());
145 if (message.span() == absl::Span<const uint8_t>()) {
146 break;
147 }
148 CHECK(message.Verify());
149
150 const auto *const channels = full_header->configuration()->channels();
151 const size_t channel_index = message.message().channel_index();
152 CHECK_LT(channel_index, channels->size());
153 const aos::Channel *const channel = channels->Get(channel_index);
154
155 CHECK(message.Verify()) << absl::BytesToHexString(
156 std::string_view(reinterpret_cast<const char *>(message.span().data()),
157 message.span().size()));
158
159 if (message.message().data() != nullptr) {
160 CHECK(channel->has_schema());
161
162 CHECK(flatbuffers::Verify(
163 *channel->schema(), *channel->schema()->root_table(),
164 message.message().data()->data(), message.message().data()->size()))
165 << ": Corrupted flatbuffer on " << channel->name()->c_str() << " "
166 << channel->type()->c_str();
167 }
168
169 if (FLAGS_format_raw && message.message().data() != nullptr) {
170 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
171 << aos::FlatbufferToJson(message, {.multi_line = FLAGS_pretty,
172 .max_vector_size = 4})
173 << ": "
174 << aos::FlatbufferToJson(
175 channel->schema(), message.message().data()->data(),
176 {FLAGS_pretty,
177 static_cast<size_t>(FLAGS_max_vector_size)})
178 << std::endl;
179 } else {
180 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
181 << aos::FlatbufferToJson(
182 message, {FLAGS_pretty,
183 static_cast<size_t>(FLAGS_max_vector_size)})
184 << std::endl;
185 }
186 }
187 return 0;
188}
189
190// This class prints out all data from a node on a boot.
191class NodePrinter {
192 public:
193 NodePrinter(aos::EventLoop *event_loop, uint64_t *message_print_counter,
194 aos::SimulatedEventLoopFactory *factory,
195 aos::FastStringBuilder *builder)
196 : factory_(factory),
Austin Schuhff3bc902022-05-11 16:10:57 -0700197 node_factory_(factory->GetNodeEventLoopFactory(event_loop->node())),
Austin Schuh58646e22021-08-23 23:51:46 -0700198 event_loop_(event_loop),
199 message_print_counter_(message_print_counter),
200 node_name_(
201 event_loop_->node() == nullptr
202 ? ""
Austin Schuh041fe9f2021-10-16 23:01:15 -0700203 : std::string(event_loop->node()->name()->string_view())),
Austin Schuh58646e22021-08-23 23:51:46 -0700204 builder_(builder) {
205 event_loop_->SkipTimingReport();
206 event_loop_->SkipAosLog();
207
208 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
209 event_loop_->configuration()->channels();
210
Milind Upadhyay184dfda2022-03-26 15:54:38 -0700211 const monotonic_clock::time_point start_time =
212 (FLAGS_monotonic_start_time == 0.0
213 ? monotonic_clock::min_time
214 : monotonic_clock::time_point(
215 std::chrono::duration_cast<monotonic_clock::duration>(
216 std::chrono::duration<double>(
217 FLAGS_monotonic_start_time))));
218 const monotonic_clock::time_point end_time =
219 (FLAGS_monotonic_end_time == 0.0
220 ? monotonic_clock::max_time
221 : monotonic_clock::time_point(
222 std::chrono::duration_cast<monotonic_clock::duration>(
223 std::chrono::duration<double>(
224 FLAGS_monotonic_end_time))));
225
Austin Schuh58646e22021-08-23 23:51:46 -0700226 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
227 const aos::Channel *channel = channels->Get(i);
228 const flatbuffers::string_view name = channel->name()->string_view();
229 const flatbuffers::string_view type = channel->type()->string_view();
230 if (name.find(FLAGS_name) != std::string::npos &&
231 type.find(FLAGS_type) != std::string::npos) {
232 if (!aos::configuration::ChannelIsReadableOnNode(channel,
233 event_loop_->node())) {
234 continue;
235 }
236 VLOG(1) << "Listening on " << name << " " << type;
237
238 CHECK_NOTNULL(channel->schema());
Austin Schuh60e77942022-05-16 17:48:24 -0700239 event_loop_->MakeRawWatcher(channel, [this, channel, start_time,
240 end_time](
241 const aos::Context &context,
242 const void * /*message*/) {
243 if (!FLAGS_print) {
244 return;
245 }
Austin Schuh58646e22021-08-23 23:51:46 -0700246
Austin Schuh60e77942022-05-16 17:48:24 -0700247 if (!FLAGS_fetch && !started_) {
248 return;
249 }
Austin Schuh58646e22021-08-23 23:51:46 -0700250
Austin Schuh60e77942022-05-16 17:48:24 -0700251 if (context.monotonic_event_time < start_time ||
252 context.monotonic_event_time > end_time) {
253 return;
254 }
Milind Upadhyay184dfda2022-03-26 15:54:38 -0700255
Austin Schuh893d7f42022-09-16 15:01:35 -0700256 PrintMessage(
257 node_name_, node_factory_, channel, context, builder_,
258 {
259 .pretty = FLAGS_pretty,
260 .max_vector_size = static_cast<size_t>(FLAGS_max_vector_size),
261 .pretty_max = FLAGS_pretty_max,
262 .print_timestamps = FLAGS_print_timestamps,
263 .json = FLAGS_json,
264 .distributed_clock = FLAGS_distributed_clock,
265 .use_hex = FLAGS_use_hex,
266 });
Austin Schuh60e77942022-05-16 17:48:24 -0700267 ++(*message_print_counter_);
268 if (FLAGS_count > 0 && *message_print_counter_ >= FLAGS_count) {
269 factory_->Exit();
270 }
271 });
Austin Schuh58646e22021-08-23 23:51:46 -0700272 }
273 }
274 }
275
276 void SetStarted(bool started, aos::monotonic_clock::time_point monotonic_now,
277 aos::realtime_clock::time_point realtime_now) {
278 started_ = started;
Austin Schuh041fe9f2021-10-16 23:01:15 -0700279 if (FLAGS_json) {
280 return;
281 }
Austin Schuh58646e22021-08-23 23:51:46 -0700282 if (started_) {
283 std::cout << std::endl;
284 std::cout << (event_loop_->node() != nullptr
285 ? (event_loop_->node()->name()->str() + " ")
286 : "")
287 << "Log starting at " << realtime_now << " (" << monotonic_now
288 << ")";
289 std::cout << std::endl << std::endl;
290 } else {
291 std::cout << std::endl;
292 std::cout << (event_loop_->node() != nullptr
293 ? (event_loop_->node()->name()->str() + " ")
294 : "")
295 << "Log shutting down at " << realtime_now << " ("
296 << monotonic_now << ")";
297 std::cout << std::endl << std::endl;
298 }
299 }
300
301 private:
302 struct MessageInfo {
303 std::string node_name;
304 std::unique_ptr<aos::RawFetcher> fetcher;
305 };
306
307 aos::SimulatedEventLoopFactory *factory_;
Austin Schuhff3bc902022-05-11 16:10:57 -0700308 aos::NodeEventLoopFactory *node_factory_;
Austin Schuh58646e22021-08-23 23:51:46 -0700309 aos::EventLoop *event_loop_;
310
311 uint64_t *message_print_counter_ = nullptr;
312
313 std::string node_name_;
314
315 bool started_ = false;
316
317 aos::FastStringBuilder *builder_;
318};
319
James Kuszmaul38735e82019-12-07 16:42:06 -0800320int main(int argc, char **argv) {
321 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800322 "Usage:\n"
323 " log_cat [args] logfile1 logfile2 ...\n"
324 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -0800325 "This program provides a basic interface to dump data from a logfile to "
326 "stdout. Given a logfile, channel name filter, and type filter, it will "
327 "print all the messages in the logfile matching the filters. The message "
328 "filters work by taking the values of --name and --type and printing any "
329 "channel whose name contains --name as a substr and whose type contains "
330 "--type as a substr. Not specifying --name or --type leaves them free. "
331 "Calling this program without --name or --type specified prints out all "
332 "the logged data.");
333 aos::InitGoogle(&argc, &argv);
334
Austin Schuh6f3babe2020-01-26 20:34:50 -0800335 if (FLAGS_raw) {
Austin Schuh58646e22021-08-23 23:51:46 -0700336 return PrintRaw(argc, argv);
James Kuszmaul38735e82019-12-07 16:42:06 -0800337 }
338
Austin Schuh6f3babe2020-01-26 20:34:50 -0800339 if (argc < 2) {
340 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
341 }
342
Austin Schuh11d43732020-09-21 17:28:30 -0700343 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuh58646e22021-08-23 23:51:46 -0700344 aos::logger::SortParts(aos::logger::FindLogs(argc, argv));
Austin Schuh5212cad2020-09-09 23:12:09 -0700345
Austin Schuhfe3fb342021-01-16 18:50:37 -0800346 for (auto &it : logfiles) {
347 VLOG(1) << it;
Austin Schuh7af06d52021-06-28 15:46:59 -0700348 if (FLAGS_print_parts_only) {
349 std::cout << it << std::endl;
350 }
351 }
352 if (FLAGS_print_parts_only) {
353 return 0;
Austin Schuhfe3fb342021-01-16 18:50:37 -0800354 }
355
Austin Schuh6f3babe2020-01-26 20:34:50 -0800356 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700357
Austin Schuh25b17652021-07-21 15:42:56 -0700358 if (FLAGS_channels) {
359 const aos::Configuration *config = reader.configuration();
360 for (const aos::Channel *channel : *config->channels()) {
361 std::cout << channel->name()->c_str() << " " << channel->type()->c_str()
362 << '\n';
363 }
364 return 0;
365 }
366
Austin Schuh58646e22021-08-23 23:51:46 -0700367 {
368 bool found_channel = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800369 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Austin Schuh58646e22021-08-23 23:51:46 -0700370 reader.configuration()->channels();
Brian Silverman9891b292020-06-23 16:34:22 -0700371
Austin Schuh6f3babe2020-01-26 20:34:50 -0800372 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
373 const aos::Channel *channel = channels->Get(i);
374 const flatbuffers::string_view name = channel->name()->string_view();
375 const flatbuffers::string_view type = channel->type()->string_view();
376 if (name.find(FLAGS_name) != std::string::npos &&
377 type.find(FLAGS_type) != std::string::npos) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800378 found_channel = true;
379 }
380 }
Austin Schuh58646e22021-08-23 23:51:46 -0700381 if (!found_channel) {
382 LOG(FATAL) << "Could not find any channels";
Austin Schuha81454b2020-05-12 19:58:36 -0700383 }
James Kuszmaul38735e82019-12-07 16:42:06 -0800384 }
385
Austin Schuh58646e22021-08-23 23:51:46 -0700386 aos::FastStringBuilder builder;
James Kuszmaul912af072020-10-31 16:06:54 -0700387
Austin Schuh58646e22021-08-23 23:51:46 -0700388 uint64_t message_print_counter = 0;
389
390 std::vector<NodePrinter *> printers;
Sanjay Narayananbeb328c2021-09-01 16:24:20 -0700391 printers.resize(aos::configuration::NodesCount(reader.configuration()),
392 nullptr);
393
394 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
395
396 reader.RegisterWithoutStarting(&event_loop_factory);
Austin Schuh58646e22021-08-23 23:51:46 -0700397
398 for (const aos::Node *node :
399 aos::configuration::GetNodes(event_loop_factory.configuration())) {
400 size_t node_index = aos::configuration::GetNodeIndex(
401 event_loop_factory.configuration(), node);
402 // Spin up the printer, and hook up the SetStarted method so that it gets
403 // notified when the log starts and stops.
404 aos::NodeEventLoopFactory *node_factory =
405 event_loop_factory.GetNodeEventLoopFactory(node);
406 node_factory->OnStartup([&event_loop_factory, node_factory,
407 &message_print_counter, &builder, &printers,
408 node_index]() {
409 printers[node_index] = node_factory->AlwaysStart<NodePrinter>(
410 "printer", &message_print_counter, &event_loop_factory, &builder);
411 });
412 node_factory->OnShutdown(
413 [&printers, node_index]() { printers[node_index] = nullptr; });
414
415 reader.OnStart(node, [&printers, node_index, node_factory]() {
416 CHECK(printers[node_index]);
417 printers[node_index]->SetStarted(true, node_factory->monotonic_now(),
418 node_factory->realtime_now());
419 });
420 reader.OnEnd(node, [&printers, node_index, node_factory]() {
421 CHECK(printers[node_index]);
422 printers[node_index]->SetStarted(false, node_factory->monotonic_now(),
423 node_factory->realtime_now());
424 });
Austin Schuha81454b2020-05-12 19:58:36 -0700425 }
426
427 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800428
Austin Schuh51a92592020-08-09 13:17:00 -0700429 reader.Deregister();
430
James Kuszmaul38735e82019-12-07 16:42:06 -0800431 return 0;
432}