blob: 2e23e0ab2c1e285e216ba444a9add71f38f79601 [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
9#include "aos/configuration.h"
10#include "aos/events/logging/logger.h"
11#include "aos/events/simulated_event_loop.h"
12#include "aos/init.h"
13#include "aos/json_to_flatbuffer.h"
14#include "gflags/gflags.h"
15
James Kuszmaul38735e82019-12-07 16:42:06 -080016DEFINE_string(
17 name, "",
18 "Name to match for printing out channels. Empty means no name filter.");
19DEFINE_string(type, "",
20 "Channel type to match for printing out channels. Empty means no "
21 "type filter.");
Austin Schuha81454b2020-05-12 19:58:36 -070022DEFINE_bool(fetch, false,
23 "If true, also print out the messages from before the start of the "
24 "log file");
Austin Schuh6f3babe2020-01-26 20:34:50 -080025DEFINE_bool(raw, false,
26 "If true, just print the data out unsorted and unparsed");
Brian Silverman8ff74aa2021-02-05 16:37:15 -080027DEFINE_string(raw_header, "",
28 "If set, the file to read the header from in raw mode");
Austin Schuha81454b2020-05-12 19:58:36 -070029DEFINE_bool(format_raw, true,
30 "If true and --raw is specified, print out raw data, but use the "
31 "schema to format the data.");
Austin Schuhae46f362020-04-11 19:52:56 -070032DEFINE_int32(max_vector_size, 100,
33 "If positive, vectors longer than this will not be printed");
Ravago Jones5cc9df52020-09-02 21:29:58 -070034DEFINE_bool(pretty, false,
35 "If true, pretty print the messages on multiple lines");
Austin Schuh569c7f92020-12-11 20:01:42 -080036DEFINE_bool(print, true,
37 "If true, actually print the messages. If false, discard them, "
38 "confirming they can be parsed.");
Austin Schuh6f3babe2020-01-26 20:34:50 -080039
Brian Silverman9891b292020-06-23 16:34:22 -070040// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
41// glog and to allow the user to readily redirect just the logged output
42// independent of any debugging information on stderr.
43void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
Austin Schuh746690f2020-08-01 16:15:57 -070044 const aos::Context &context,
45 aos::FastStringBuilder *builder) {
46 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070047 aos::FlatbufferToJson(
48 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
49 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Austin Schuh746690f2020-08-01 16:15:57 -070050
Austin Schuha81454b2020-05-12 19:58:36 -070051 if (context.monotonic_remote_time != context.monotonic_event_time) {
52 std::cout << node_name << context.realtime_event_time << " ("
53 << context.monotonic_event_time << ") sent "
54 << context.realtime_remote_time << " ("
55 << context.monotonic_remote_time << ") "
56 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070057 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070058 } else {
59 std::cout << node_name << context.realtime_event_time << " ("
60 << context.monotonic_event_time << ") "
61 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070062 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070063 }
64}
65
James Kuszmaul38735e82019-12-07 16:42:06 -080066int main(int argc, char **argv) {
67 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -080068 "Usage:\n"
69 " log_cat [args] logfile1 logfile2 ...\n"
70 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -080071 "This program provides a basic interface to dump data from a logfile to "
72 "stdout. Given a logfile, channel name filter, and type filter, it will "
73 "print all the messages in the logfile matching the filters. The message "
74 "filters work by taking the values of --name and --type and printing any "
75 "channel whose name contains --name as a substr and whose type contains "
76 "--type as a substr. Not specifying --name or --type leaves them free. "
77 "Calling this program without --name or --type specified prints out all "
78 "the logged data.");
79 aos::InitGoogle(&argc, &argv);
80
Austin Schuh6f3babe2020-01-26 20:34:50 -080081 if (FLAGS_raw) {
82 if (argc != 2) {
83 LOG(FATAL) << "Expected 1 logfile as an argument.";
James Kuszmaul38735e82019-12-07 16:42:06 -080084 }
Austin Schuh6f3babe2020-01-26 20:34:50 -080085 aos::logger::MessageReader reader(argv[1]);
Brian Silverman8ff74aa2021-02-05 16:37:15 -080086
87 std::optional<aos::logger::MessageReader> raw_header_reader;
88 const aos::logger::LogFileHeader *full_header = reader.log_file_header();
89 if (!FLAGS_raw_header.empty()) {
90 raw_header_reader.emplace(FLAGS_raw_header);
91 CHECK_EQ(
92 reader.log_file_header()->configuration_sha256()->string_view(),
93 aos::logger::Sha256(raw_header_reader->raw_log_file_header().span()));
94 full_header = raw_header_reader->log_file_header();
95 }
96 std::cout << aos::FlatbufferToJson(full_header,
Austin Schuh2f8fd752020-09-01 22:38:28 -070097 {.multi_line = FLAGS_pretty,
98 .max_vector_size = static_cast<size_t>(
99 FLAGS_max_vector_size)})
100 << std::endl;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800101
102 while (true) {
Austin Schuhadd6eb32020-11-09 21:24:26 -0800103 std::optional<
104 aos::SizePrefixedFlatbufferVector<aos::logger::MessageHeader>>
105 message = reader.ReadMessage();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800106 if (!message) {
107 break;
108 }
Brian Silverman8ff74aa2021-02-05 16:37:15 -0800109 const auto *const channels = full_header->configuration()->channels();
110 const size_t channel_index = message.value().message().channel_index();
111 CHECK_LT(channel_index, channels->size());
112 const aos::Channel *const channel = channels->Get(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800113
Austin Schuha81454b2020-05-12 19:58:36 -0700114 if (FLAGS_format_raw && message.value().message().data() != nullptr) {
115 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700116 << aos::FlatbufferToJson(
117 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700118 {.multi_line = FLAGS_pretty, .max_vector_size = 4})
Ravago Jonescf453ab2020-05-06 21:14:53 -0700119 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -0700120 << aos::FlatbufferToJson(
121 channel->schema(),
122 message.value().message().data()->data(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700123 {FLAGS_pretty,
124 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700125 << std::endl;
126 } else {
127 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700128 << aos::FlatbufferToJson(
129 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700130 {FLAGS_pretty,
131 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700132 << std::endl;
133 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800134 }
135 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800136 }
137
Austin Schuh6f3babe2020-01-26 20:34:50 -0800138 if (argc < 2) {
139 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
140 }
141
Ravago Jones6b23e172020-12-05 17:39:01 -0800142 const std::vector<std::string> unsorted_logfiles =
143 aos::logger::FindLogs(argc, argv);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800144
Austin Schuh11d43732020-09-21 17:28:30 -0700145 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuh5212cad2020-09-09 23:12:09 -0700146 aos::logger::SortParts(unsorted_logfiles);
147
Austin Schuhfe3fb342021-01-16 18:50:37 -0800148 for (auto &it : logfiles) {
149 VLOG(1) << it;
150 }
151
Austin Schuh6f3babe2020-01-26 20:34:50 -0800152 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700153
Austin Schuh746690f2020-08-01 16:15:57 -0700154 aos::FastStringBuilder builder;
155
Austin Schuha81454b2020-05-12 19:58:36 -0700156 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
157 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800158
159 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
160
James Kuszmaul912af072020-10-31 16:06:54 -0700161 bool found_channel = false;
162
Austin Schuh07676622021-01-21 18:59:17 -0800163 for (const aos::Node *node :
164 aos::configuration::GetNodes(event_loop_factory.configuration())) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800165 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700166 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800167 printer_event_loop->SkipTimingReport();
168 printer_event_loop->SkipAosLog();
169
Brian Silverman9891b292020-06-23 16:34:22 -0700170 struct MessageInfo {
171 std::string node_name;
172 std::unique_ptr<aos::RawFetcher> fetcher;
173 };
174 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700175
Austin Schuh6f3babe2020-01-26 20:34:50 -0800176 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700177 printer_event_loop->configuration()->channels();
178
Austin Schuh6f3babe2020-01-26 20:34:50 -0800179 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
180 const aos::Channel *channel = channels->Get(i);
181 const flatbuffers::string_view name = channel->name()->string_view();
182 const flatbuffers::string_view type = channel->type()->string_view();
183 if (name.find(FLAGS_name) != std::string::npos &&
184 type.find(FLAGS_type) != std::string::npos) {
185 if (!aos::configuration::ChannelIsReadableOnNode(
186 channel, printer_event_loop->node())) {
187 continue;
188 }
189 VLOG(1) << "Listening on " << name << " " << type;
190
191 std::string node_name =
192 node == nullptr ? ""
193 : std::string(node->name()->string_view()) + " ";
194
195 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700196
Brian Silverman9891b292020-06-23 16:34:22 -0700197 // Fetch the last message on this channel from before the log start
198 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700199 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700200 std::unique_ptr<aos::RawFetcher> fetcher =
201 printer_event_loop->MakeRawFetcher(channel);
202 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700203 MessageInfo message{.node_name = node_name,
204 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700205 // Insert it sorted into the vector so we can print in time order
206 // instead of channel order at the start.
207 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700208 messages_before_start.begin(), messages_before_start.end(),
209 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
210 if (lhs.fetcher->context().monotonic_event_time <
211 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700212 return true;
213 }
Brian Silverman9891b292020-06-23 16:34:22 -0700214 if (lhs.fetcher->context().monotonic_event_time >
215 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700216 return false;
217 }
Brian Silverman9891b292020-06-23 16:34:22 -0700218 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700219 });
Brian Silverman9891b292020-06-23 16:34:22 -0700220 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700221 }
222 }
223
Austin Schuh6f3babe2020-01-26 20:34:50 -0800224 printer_event_loop->MakeRawWatcher(
Ravago Jones5cc9df52020-09-02 21:29:58 -0700225 channel, [channel, node_name, &builder](const aos::Context &context,
226 const void * /*message*/) {
Austin Schuh569c7f92020-12-11 20:01:42 -0800227 if (FLAGS_print) {
228 PrintMessage(node_name, channel, context, &builder);
229 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800230 });
231 found_channel = true;
232 }
233 }
234
Brian Silverman9891b292020-06-23 16:34:22 -0700235 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700236 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700237 for (const MessageInfo &message : messages_before_start) {
Austin Schuh569c7f92020-12-11 20:01:42 -0800238 if (FLAGS_print) {
239 PrintMessage(message.node_name, message.fetcher->channel(),
240 message.fetcher->context(), &builder);
241 }
Austin Schuha81454b2020-05-12 19:58:36 -0700242 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800243 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700244
245 std::cout << std::endl;
Austin Schuhee711052020-08-24 16:06:09 -0700246 std::cout << (node != nullptr ? (node->name()->str() + " ") : "")
247 << "Log starting at " << reader.realtime_start_time(node) << " ("
248 << reader.monotonic_start_time(node) << ")";
Brian Silverman9891b292020-06-23 16:34:22 -0700249 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800250 }
251
James Kuszmaul912af072020-10-31 16:06:54 -0700252 if (!found_channel) {
253 LOG(FATAL) << "Could not find any channels";
254 }
255
Austin Schuha81454b2020-05-12 19:58:36 -0700256 if (FLAGS_fetch) {
257 // New line to separate fetched messages from non-fetched messages.
258 std::cout << std::endl;
259 }
260
261 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800262
Austin Schuh51a92592020-08-09 13:17:00 -0700263 reader.Deregister();
264
James Kuszmaul38735e82019-12-07 16:42:06 -0800265 return 0;
266}