blob: f9256510a330d096969334742080446e8f355d8e [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) {
Austin Schuhdb605092022-09-26 15:16:53 -070069 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 Schuh58646e22021-08-23 23:51:46 -070080 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.
201class 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 Schuhff3bc902022-05-11 16:10:57 -0700207 node_factory_(factory->GetNodeEventLoopFactory(event_loop->node())),
Austin Schuh58646e22021-08-23 23:51:46 -0700208 event_loop_(event_loop),
209 message_print_counter_(message_print_counter),
210 node_name_(
211 event_loop_->node() == nullptr
212 ? ""
Austin Schuh041fe9f2021-10-16 23:01:15 -0700213 : std::string(event_loop->node()->name()->string_view())),
Austin Schuh58646e22021-08-23 23:51:46 -0700214 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 Upadhyay184dfda2022-03-26 15:54:38 -0700221 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 Schuh58646e22021-08-23 23:51:46 -0700236 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 Schuh60e77942022-05-16 17:48:24 -0700249 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 Schuh58646e22021-08-23 23:51:46 -0700256
Austin Schuh60e77942022-05-16 17:48:24 -0700257 if (!FLAGS_fetch && !started_) {
258 return;
259 }
Austin Schuh58646e22021-08-23 23:51:46 -0700260
Austin Schuh60e77942022-05-16 17:48:24 -0700261 if (context.monotonic_event_time < start_time ||
262 context.monotonic_event_time > end_time) {
263 return;
264 }
Milind Upadhyay184dfda2022-03-26 15:54:38 -0700265
Austin Schuh893d7f42022-09-16 15:01:35 -0700266 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 Schuh60e77942022-05-16 17:48:24 -0700277 ++(*message_print_counter_);
278 if (FLAGS_count > 0 && *message_print_counter_ >= FLAGS_count) {
279 factory_->Exit();
280 }
281 });
Austin Schuh58646e22021-08-23 23:51:46 -0700282 }
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 Schuh041fe9f2021-10-16 23:01:15 -0700289 if (FLAGS_json) {
290 return;
291 }
Austin Schuh58646e22021-08-23 23:51:46 -0700292 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 Schuhff3bc902022-05-11 16:10:57 -0700318 aos::NodeEventLoopFactory *node_factory_;
Austin Schuh58646e22021-08-23 23:51:46 -0700319 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 Kuszmaul38735e82019-12-07 16:42:06 -0800330int main(int argc, char **argv) {
331 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800332 "Usage:\n"
333 " log_cat [args] logfile1 logfile2 ...\n"
334 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -0800335 "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 Schuh6f3babe2020-01-26 20:34:50 -0800345 if (FLAGS_raw) {
Austin Schuh58646e22021-08-23 23:51:46 -0700346 return PrintRaw(argc, argv);
James Kuszmaul38735e82019-12-07 16:42:06 -0800347 }
348
Austin Schuh6f3babe2020-01-26 20:34:50 -0800349 if (argc < 2) {
350 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
351 }
352
Austin Schuh11d43732020-09-21 17:28:30 -0700353 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuh58646e22021-08-23 23:51:46 -0700354 aos::logger::SortParts(aos::logger::FindLogs(argc, argv));
Austin Schuh5212cad2020-09-09 23:12:09 -0700355
Austin Schuhfe3fb342021-01-16 18:50:37 -0800356 for (auto &it : logfiles) {
357 VLOG(1) << it;
Austin Schuh7af06d52021-06-28 15:46:59 -0700358 if (FLAGS_print_parts_only) {
359 std::cout << it << std::endl;
360 }
361 }
362 if (FLAGS_print_parts_only) {
363 return 0;
Austin Schuhfe3fb342021-01-16 18:50:37 -0800364 }
365
Austin Schuh6f3babe2020-01-26 20:34:50 -0800366 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700367
Austin Schuh25b17652021-07-21 15:42:56 -0700368 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 Schuh58646e22021-08-23 23:51:46 -0700377 {
378 bool found_channel = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800379 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Austin Schuh58646e22021-08-23 23:51:46 -0700380 reader.configuration()->channels();
Brian Silverman9891b292020-06-23 16:34:22 -0700381
Austin Schuh6f3babe2020-01-26 20:34:50 -0800382 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 Schuh6f3babe2020-01-26 20:34:50 -0800388 found_channel = true;
389 }
390 }
Austin Schuh58646e22021-08-23 23:51:46 -0700391 if (!found_channel) {
392 LOG(FATAL) << "Could not find any channels";
Austin Schuha81454b2020-05-12 19:58:36 -0700393 }
James Kuszmaul38735e82019-12-07 16:42:06 -0800394 }
395
Austin Schuh58646e22021-08-23 23:51:46 -0700396 aos::FastStringBuilder builder;
James Kuszmaul912af072020-10-31 16:06:54 -0700397
Austin Schuh58646e22021-08-23 23:51:46 -0700398 uint64_t message_print_counter = 0;
399
400 std::vector<NodePrinter *> printers;
Sanjay Narayananbeb328c2021-09-01 16:24:20 -0700401 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 Schuh58646e22021-08-23 23:51:46 -0700407
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 Schuha81454b2020-05-12 19:58:36 -0700435 }
436
437 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800438
Austin Schuh51a92592020-08-09 13:17:00 -0700439 reader.Deregister();
440
James Kuszmaul38735e82019-12-07 16:42:06 -0800441 return 0;
442}