blob: edd0ac76234f833adb597622fd5231bff4e1ed40 [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"
James Kuszmaul38735e82019-12-07 16:42:06 -080010#include "aos/configuration.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -080011#include "aos/events/logging/log_reader.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080012#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 Kuszmaul38735e82019-12-07 16:42:06 -080017DEFINE_string(
18 name, "",
19 "Name to match for printing out channels. Empty means no name filter.");
20DEFINE_string(type, "",
21 "Channel type to match for printing out channels. Empty means no "
22 "type filter.");
Austin Schuha81454b2020-05-12 19:58:36 -070023DEFINE_bool(fetch, false,
24 "If true, also print out the messages from before the start of the "
25 "log file");
Austin Schuh6f3babe2020-01-26 20:34:50 -080026DEFINE_bool(raw, false,
27 "If true, just print the data out unsorted and unparsed");
Brian Silverman8ff74aa2021-02-05 16:37:15 -080028DEFINE_string(raw_header, "",
29 "If set, the file to read the header from in raw mode");
Austin Schuha81454b2020-05-12 19:58:36 -070030DEFINE_bool(format_raw, true,
31 "If true and --raw is specified, print out raw data, but use the "
32 "schema to format the data.");
Austin Schuhae46f362020-04-11 19:52:56 -070033DEFINE_int32(max_vector_size, 100,
34 "If positive, vectors longer than this will not be printed");
Ravago Jones5cc9df52020-09-02 21:29:58 -070035DEFINE_bool(pretty, false,
36 "If true, pretty print the messages on multiple lines");
Austin Schuh569c7f92020-12-11 20:01:42 -080037DEFINE_bool(print, true,
38 "If true, actually print the messages. If false, discard them, "
39 "confirming they can be parsed.");
Austin Schuh7af06d52021-06-28 15:46:59 -070040DEFINE_bool(print_parts_only, false,
41 "If true, only print out the results of logfile sorting.");
Austin Schuh6f3babe2020-01-26 20:34:50 -080042
Brian Silverman9891b292020-06-23 16:34:22 -070043// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
44// glog and to allow the user to readily redirect just the logged output
45// independent of any debugging information on stderr.
46void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
Austin Schuh746690f2020-08-01 16:15:57 -070047 const aos::Context &context,
48 aos::FastStringBuilder *builder) {
49 builder->Reset();
Austin Schuha9df9ad2021-06-16 14:49:39 -070050 CHECK(flatbuffers::Verify(*channel->schema(),
51 *channel->schema()->root_table(),
52 static_cast<const uint8_t *>(context.data),
53 static_cast<size_t>(context.size)))
54 << ": Corrupted flatbuffer on " << channel->name()->c_str() << " "
55 << channel->type()->c_str();
56
Ravago Jones5cc9df52020-09-02 21:29:58 -070057 aos::FlatbufferToJson(
58 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
59 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Austin Schuh746690f2020-08-01 16:15:57 -070060
Austin Schuha81454b2020-05-12 19:58:36 -070061 if (context.monotonic_remote_time != context.monotonic_event_time) {
62 std::cout << node_name << context.realtime_event_time << " ("
63 << context.monotonic_event_time << ") sent "
64 << context.realtime_remote_time << " ("
65 << context.monotonic_remote_time << ") "
66 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070067 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070068 } else {
69 std::cout << node_name << context.realtime_event_time << " ("
70 << context.monotonic_event_time << ") "
71 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070072 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070073 }
74}
75
James Kuszmaul38735e82019-12-07 16:42:06 -080076int main(int argc, char **argv) {
77 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -080078 "Usage:\n"
79 " log_cat [args] logfile1 logfile2 ...\n"
80 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -080081 "This program provides a basic interface to dump data from a logfile to "
82 "stdout. Given a logfile, channel name filter, and type filter, it will "
83 "print all the messages in the logfile matching the filters. The message "
84 "filters work by taking the values of --name and --type and printing any "
85 "channel whose name contains --name as a substr and whose type contains "
86 "--type as a substr. Not specifying --name or --type leaves them free. "
87 "Calling this program without --name or --type specified prints out all "
88 "the logged data.");
89 aos::InitGoogle(&argc, &argv);
90
Austin Schuh6f3babe2020-01-26 20:34:50 -080091 if (FLAGS_raw) {
92 if (argc != 2) {
93 LOG(FATAL) << "Expected 1 logfile as an argument.";
James Kuszmaul38735e82019-12-07 16:42:06 -080094 }
Austin Schuh0e8db662021-07-06 10:43:47 -070095 aos::logger::SpanReader reader(argv[1]);
96 absl::Span<const uint8_t> raw_log_file_header_span = reader.ReadMessage();
Brian Silverman8ff74aa2021-02-05 16:37:15 -080097
Austin Schuh0e8db662021-07-06 10:43:47 -070098 if (raw_log_file_header_span == absl::Span<const uint8_t>()) {
99 LOG(WARNING) << "Empty log file on " << reader.filename();
100 return 0;
101 }
102
103 // Now, reproduce the log file header deduplication logic inline so we can
104 // print out all the headers we find.
105 aos::SizePrefixedFlatbufferVector<aos::logger::LogFileHeader>
106 log_file_header(raw_log_file_header_span);
107 if (!log_file_header.Verify()) {
108 LOG(ERROR) << "Header corrupted on " << reader.filename();
109 return 1;
110 }
111 while (true) {
112 absl::Span<const uint8_t> maybe_header_data = reader.PeekMessage();
113 if (maybe_header_data == absl::Span<const uint8_t>()) {
114 break;
115 }
116
117 aos::SizePrefixedFlatbufferSpan<aos::logger::LogFileHeader> maybe_header(
118 maybe_header_data);
119 if (maybe_header.Verify()) {
120 std::cout << aos::FlatbufferToJson(
121 log_file_header,
122 {.multi_line = FLAGS_pretty,
123 .max_vector_size =
124 static_cast<size_t>(FLAGS_max_vector_size)})
125 << std::endl;
126 LOG(WARNING) << "Found duplicate LogFileHeader in "
127 << reader.filename();
128 log_file_header =
129 aos::SizePrefixedFlatbufferVector<aos::logger::LogFileHeader>(
130 maybe_header_data);
131
132 reader.ConsumeMessage();
133 } else {
134 break;
135 }
136 }
137
138 // And now use the final sha256 to match the raw_header.
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800139 std::optional<aos::logger::MessageReader> raw_header_reader;
Austin Schuh0e8db662021-07-06 10:43:47 -0700140 const aos::logger::LogFileHeader *full_header = &log_file_header.message();
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800141 if (!FLAGS_raw_header.empty()) {
142 raw_header_reader.emplace(FLAGS_raw_header);
Austin Schuh881530d2021-06-21 17:40:04 -0700143 std::cout << aos::FlatbufferToJson(
Austin Schuh0e8db662021-07-06 10:43:47 -0700144 full_header, {.multi_line = FLAGS_pretty,
145 .max_vector_size = static_cast<size_t>(
146 FLAGS_max_vector_size)})
Austin Schuh881530d2021-06-21 17:40:04 -0700147 << std::endl;
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800148 CHECK_EQ(
Austin Schuh0e8db662021-07-06 10:43:47 -0700149 full_header->configuration_sha256()->string_view(),
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800150 aos::logger::Sha256(raw_header_reader->raw_log_file_header().span()));
151 full_header = raw_header_reader->log_file_header();
152 }
153 std::cout << aos::FlatbufferToJson(full_header,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700154 {.multi_line = FLAGS_pretty,
155 .max_vector_size = static_cast<size_t>(
156 FLAGS_max_vector_size)})
157 << std::endl;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800158
159 while (true) {
Austin Schuh0e8db662021-07-06 10:43:47 -0700160 const aos::SizePrefixedFlatbufferSpan<aos::logger::MessageHeader> message(
161 reader.ReadMessage());
162 if (message.span() == absl::Span<const uint8_t>()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800163 break;
164 }
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800165 const auto *const channels = full_header->configuration()->channels();
Austin Schuh0e8db662021-07-06 10:43:47 -0700166 const size_t channel_index = message.message().channel_index();
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800167 CHECK_LT(channel_index, channels->size());
168 const aos::Channel *const channel = channels->Get(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800169
Austin Schuh0e8db662021-07-06 10:43:47 -0700170 CHECK(message.Verify()) << absl::BytesToHexString(std::string_view(
171 reinterpret_cast<const char *>(message.span().data()),
172 message.span().size()));
173
174 if (message.message().data() != nullptr) {
Austin Schuha9df9ad2021-06-16 14:49:39 -0700175 CHECK(channel->has_schema());
176
Austin Schuh0e8db662021-07-06 10:43:47 -0700177 CHECK(flatbuffers::Verify(
178 *channel->schema(), *channel->schema()->root_table(),
179 message.message().data()->data(), message.message().data()->size()))
Austin Schuha9df9ad2021-06-16 14:49:39 -0700180 << ": Corrupted flatbuffer on " << channel->name()->c_str() << " "
181 << channel->type()->c_str();
182 }
183
Austin Schuh0e8db662021-07-06 10:43:47 -0700184 if (FLAGS_format_raw && message.message().data() != nullptr) {
Austin Schuha81454b2020-05-12 19:58:36 -0700185 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Austin Schuh0e8db662021-07-06 10:43:47 -0700186 << aos::FlatbufferToJson(message, {.multi_line = FLAGS_pretty,
187 .max_vector_size = 4})
Ravago Jonescf453ab2020-05-06 21:14:53 -0700188 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -0700189 << aos::FlatbufferToJson(
Austin Schuh0e8db662021-07-06 10:43:47 -0700190 channel->schema(), message.message().data()->data(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700191 {FLAGS_pretty,
192 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700193 << std::endl;
194 } else {
195 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700196 << aos::FlatbufferToJson(
Austin Schuh0e8db662021-07-06 10:43:47 -0700197 message, {FLAGS_pretty,
198 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700199 << std::endl;
200 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800201 }
202 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800203 }
204
Austin Schuh6f3babe2020-01-26 20:34:50 -0800205 if (argc < 2) {
206 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
207 }
208
Ravago Jones6b23e172020-12-05 17:39:01 -0800209 const std::vector<std::string> unsorted_logfiles =
210 aos::logger::FindLogs(argc, argv);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800211
Austin Schuh11d43732020-09-21 17:28:30 -0700212 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuh5212cad2020-09-09 23:12:09 -0700213 aos::logger::SortParts(unsorted_logfiles);
214
Austin Schuhfe3fb342021-01-16 18:50:37 -0800215 for (auto &it : logfiles) {
216 VLOG(1) << it;
Austin Schuh7af06d52021-06-28 15:46:59 -0700217 if (FLAGS_print_parts_only) {
218 std::cout << it << std::endl;
219 }
220 }
221 if (FLAGS_print_parts_only) {
222 return 0;
Austin Schuhfe3fb342021-01-16 18:50:37 -0800223 }
224
Austin Schuh6f3babe2020-01-26 20:34:50 -0800225 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700226
Austin Schuh746690f2020-08-01 16:15:57 -0700227 aos::FastStringBuilder builder;
228
Austin Schuha81454b2020-05-12 19:58:36 -0700229 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
230 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800231
232 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
233
James Kuszmaul912af072020-10-31 16:06:54 -0700234 bool found_channel = false;
235
Austin Schuh07676622021-01-21 18:59:17 -0800236 for (const aos::Node *node :
237 aos::configuration::GetNodes(event_loop_factory.configuration())) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800238 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700239 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800240 printer_event_loop->SkipTimingReport();
241 printer_event_loop->SkipAosLog();
242
Brian Silverman9891b292020-06-23 16:34:22 -0700243 struct MessageInfo {
244 std::string node_name;
245 std::unique_ptr<aos::RawFetcher> fetcher;
246 };
247 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700248
Austin Schuh6f3babe2020-01-26 20:34:50 -0800249 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700250 printer_event_loop->configuration()->channels();
251
Austin Schuh6f3babe2020-01-26 20:34:50 -0800252 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
253 const aos::Channel *channel = channels->Get(i);
254 const flatbuffers::string_view name = channel->name()->string_view();
255 const flatbuffers::string_view type = channel->type()->string_view();
256 if (name.find(FLAGS_name) != std::string::npos &&
257 type.find(FLAGS_type) != std::string::npos) {
258 if (!aos::configuration::ChannelIsReadableOnNode(
259 channel, printer_event_loop->node())) {
260 continue;
261 }
262 VLOG(1) << "Listening on " << name << " " << type;
263
264 std::string node_name =
265 node == nullptr ? ""
266 : std::string(node->name()->string_view()) + " ";
267
268 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700269
Brian Silverman9891b292020-06-23 16:34:22 -0700270 // Fetch the last message on this channel from before the log start
271 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700272 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700273 std::unique_ptr<aos::RawFetcher> fetcher =
274 printer_event_loop->MakeRawFetcher(channel);
275 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700276 MessageInfo message{.node_name = node_name,
277 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700278 // Insert it sorted into the vector so we can print in time order
279 // instead of channel order at the start.
280 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700281 messages_before_start.begin(), messages_before_start.end(),
282 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
283 if (lhs.fetcher->context().monotonic_event_time <
284 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700285 return true;
286 }
Brian Silverman9891b292020-06-23 16:34:22 -0700287 if (lhs.fetcher->context().monotonic_event_time >
288 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700289 return false;
290 }
Brian Silverman9891b292020-06-23 16:34:22 -0700291 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700292 });
Brian Silverman9891b292020-06-23 16:34:22 -0700293 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700294 }
295 }
296
Austin Schuh6f3babe2020-01-26 20:34:50 -0800297 printer_event_loop->MakeRawWatcher(
Ravago Jones5cc9df52020-09-02 21:29:58 -0700298 channel, [channel, node_name, &builder](const aos::Context &context,
299 const void * /*message*/) {
Austin Schuh569c7f92020-12-11 20:01:42 -0800300 if (FLAGS_print) {
301 PrintMessage(node_name, channel, context, &builder);
302 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800303 });
304 found_channel = true;
305 }
306 }
307
Brian Silverman9891b292020-06-23 16:34:22 -0700308 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700309 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700310 for (const MessageInfo &message : messages_before_start) {
Austin Schuh569c7f92020-12-11 20:01:42 -0800311 if (FLAGS_print) {
312 PrintMessage(message.node_name, message.fetcher->channel(),
313 message.fetcher->context(), &builder);
314 }
Austin Schuha81454b2020-05-12 19:58:36 -0700315 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800316 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700317
318 std::cout << std::endl;
Austin Schuhee711052020-08-24 16:06:09 -0700319 std::cout << (node != nullptr ? (node->name()->str() + " ") : "")
320 << "Log starting at " << reader.realtime_start_time(node) << " ("
321 << reader.monotonic_start_time(node) << ")";
Brian Silverman9891b292020-06-23 16:34:22 -0700322 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800323 }
324
James Kuszmaul912af072020-10-31 16:06:54 -0700325 if (!found_channel) {
326 LOG(FATAL) << "Could not find any channels";
327 }
328
Austin Schuha81454b2020-05-12 19:58:36 -0700329 if (FLAGS_fetch) {
330 // New line to separate fetched messages from non-fetched messages.
331 std::cout << std::endl;
332 }
333
334 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800335
Austin Schuh51a92592020-08-09 13:17:00 -0700336 reader.Deregister();
337
James Kuszmaul38735e82019-12-07 16:42:06 -0800338 return 0;
339}