blob: 1168e6fb68d7201ca70ccb1f513ef2b713381c64 [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");
Austin Schuha81454b2020-05-12 19:58:36 -070027DEFINE_bool(format_raw, true,
28 "If true and --raw is specified, print out raw data, but use the "
29 "schema to format the data.");
Austin Schuhae46f362020-04-11 19:52:56 -070030DEFINE_int32(max_vector_size, 100,
31 "If positive, vectors longer than this will not be printed");
Ravago Jones5cc9df52020-09-02 21:29:58 -070032DEFINE_bool(pretty, false,
33 "If true, pretty print the messages on multiple lines");
Austin Schuh569c7f92020-12-11 20:01:42 -080034DEFINE_bool(print, true,
35 "If true, actually print the messages. If false, discard them, "
36 "confirming they can be parsed.");
Austin Schuh6f3babe2020-01-26 20:34:50 -080037
Brian Silverman9891b292020-06-23 16:34:22 -070038// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
39// glog and to allow the user to readily redirect just the logged output
40// independent of any debugging information on stderr.
41void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
Austin Schuh746690f2020-08-01 16:15:57 -070042 const aos::Context &context,
43 aos::FastStringBuilder *builder) {
44 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070045 aos::FlatbufferToJson(
46 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
47 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Austin Schuh746690f2020-08-01 16:15:57 -070048
Austin Schuha81454b2020-05-12 19:58:36 -070049 if (context.monotonic_remote_time != context.monotonic_event_time) {
50 std::cout << node_name << context.realtime_event_time << " ("
51 << context.monotonic_event_time << ") sent "
52 << context.realtime_remote_time << " ("
53 << context.monotonic_remote_time << ") "
54 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070055 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070056 } else {
57 std::cout << node_name << context.realtime_event_time << " ("
58 << context.monotonic_event_time << ") "
59 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070060 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070061 }
62}
63
James Kuszmaul38735e82019-12-07 16:42:06 -080064int main(int argc, char **argv) {
65 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -080066 "Usage:\n"
67 " log_cat [args] logfile1 logfile2 ...\n"
68 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -080069 "This program provides a basic interface to dump data from a logfile to "
70 "stdout. Given a logfile, channel name filter, and type filter, it will "
71 "print all the messages in the logfile matching the filters. The message "
72 "filters work by taking the values of --name and --type and printing any "
73 "channel whose name contains --name as a substr and whose type contains "
74 "--type as a substr. Not specifying --name or --type leaves them free. "
75 "Calling this program without --name or --type specified prints out all "
76 "the logged data.");
77 aos::InitGoogle(&argc, &argv);
78
Austin Schuh6f3babe2020-01-26 20:34:50 -080079 if (FLAGS_raw) {
80 if (argc != 2) {
81 LOG(FATAL) << "Expected 1 logfile as an argument.";
James Kuszmaul38735e82019-12-07 16:42:06 -080082 }
Austin Schuh6f3babe2020-01-26 20:34:50 -080083 aos::logger::MessageReader reader(argv[1]);
Austin Schuh2f8fd752020-09-01 22:38:28 -070084 std::cout << aos::FlatbufferToJson(reader.log_file_header(),
85 {.multi_line = FLAGS_pretty,
86 .max_vector_size = static_cast<size_t>(
87 FLAGS_max_vector_size)})
88 << std::endl;
Austin Schuh6f3babe2020-01-26 20:34:50 -080089
90 while (true) {
Austin Schuhadd6eb32020-11-09 21:24:26 -080091 std::optional<
92 aos::SizePrefixedFlatbufferVector<aos::logger::MessageHeader>>
93 message = reader.ReadMessage();
Austin Schuh6f3babe2020-01-26 20:34:50 -080094 if (!message) {
95 break;
96 }
Austin Schuha81454b2020-05-12 19:58:36 -070097 const aos::Channel *channel =
98 reader.log_file_header()->configuration()->channels()->Get(
99 message.value().message().channel_index());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800100
Austin Schuha81454b2020-05-12 19:58:36 -0700101 if (FLAGS_format_raw && message.value().message().data() != nullptr) {
102 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700103 << aos::FlatbufferToJson(
104 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700105 {.multi_line = FLAGS_pretty, .max_vector_size = 4})
Ravago Jonescf453ab2020-05-06 21:14:53 -0700106 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -0700107 << aos::FlatbufferToJson(
108 channel->schema(),
109 message.value().message().data()->data(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700110 {FLAGS_pretty,
111 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700112 << std::endl;
113 } else {
114 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700115 << aos::FlatbufferToJson(
116 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700117 {FLAGS_pretty,
118 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700119 << std::endl;
120 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800121 }
122 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800123 }
124
Austin Schuh6f3babe2020-01-26 20:34:50 -0800125 if (argc < 2) {
126 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
127 }
128
Ravago Jones6b23e172020-12-05 17:39:01 -0800129 const std::vector<std::string> unsorted_logfiles =
130 aos::logger::FindLogs(argc, argv);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800131
Austin Schuh11d43732020-09-21 17:28:30 -0700132 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuh5212cad2020-09-09 23:12:09 -0700133 aos::logger::SortParts(unsorted_logfiles);
134
Austin Schuh6f3babe2020-01-26 20:34:50 -0800135 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700136
Austin Schuh746690f2020-08-01 16:15:57 -0700137 aos::FastStringBuilder builder;
138
Austin Schuha81454b2020-05-12 19:58:36 -0700139 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
140 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800141
142 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
143
James Kuszmaul912af072020-10-31 16:06:54 -0700144 bool found_channel = false;
145
Austin Schuh6f3babe2020-01-26 20:34:50 -0800146 for (const aos::Node *node : reader.Nodes()) {
147 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700148 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800149 printer_event_loop->SkipTimingReport();
150 printer_event_loop->SkipAosLog();
151
Brian Silverman9891b292020-06-23 16:34:22 -0700152 struct MessageInfo {
153 std::string node_name;
154 std::unique_ptr<aos::RawFetcher> fetcher;
155 };
156 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700157
Austin Schuh6f3babe2020-01-26 20:34:50 -0800158 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700159 printer_event_loop->configuration()->channels();
160
Austin Schuh6f3babe2020-01-26 20:34:50 -0800161 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
162 const aos::Channel *channel = channels->Get(i);
163 const flatbuffers::string_view name = channel->name()->string_view();
164 const flatbuffers::string_view type = channel->type()->string_view();
165 if (name.find(FLAGS_name) != std::string::npos &&
166 type.find(FLAGS_type) != std::string::npos) {
167 if (!aos::configuration::ChannelIsReadableOnNode(
168 channel, printer_event_loop->node())) {
169 continue;
170 }
171 VLOG(1) << "Listening on " << name << " " << type;
172
173 std::string node_name =
174 node == nullptr ? ""
175 : std::string(node->name()->string_view()) + " ";
176
177 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700178
Brian Silverman9891b292020-06-23 16:34:22 -0700179 // Fetch the last message on this channel from before the log start
180 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700181 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700182 std::unique_ptr<aos::RawFetcher> fetcher =
183 printer_event_loop->MakeRawFetcher(channel);
184 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700185 MessageInfo message{.node_name = node_name,
186 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700187 // Insert it sorted into the vector so we can print in time order
188 // instead of channel order at the start.
189 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700190 messages_before_start.begin(), messages_before_start.end(),
191 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
192 if (lhs.fetcher->context().monotonic_event_time <
193 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700194 return true;
195 }
Brian Silverman9891b292020-06-23 16:34:22 -0700196 if (lhs.fetcher->context().monotonic_event_time >
197 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700198 return false;
199 }
Brian Silverman9891b292020-06-23 16:34:22 -0700200 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700201 });
Brian Silverman9891b292020-06-23 16:34:22 -0700202 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700203 }
204 }
205
Austin Schuh6f3babe2020-01-26 20:34:50 -0800206 printer_event_loop->MakeRawWatcher(
Ravago Jones5cc9df52020-09-02 21:29:58 -0700207 channel, [channel, node_name, &builder](const aos::Context &context,
208 const void * /*message*/) {
Austin Schuh569c7f92020-12-11 20:01:42 -0800209 if (FLAGS_print) {
210 PrintMessage(node_name, channel, context, &builder);
211 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800212 });
213 found_channel = true;
214 }
215 }
216
Brian Silverman9891b292020-06-23 16:34:22 -0700217 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700218 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700219 for (const MessageInfo &message : messages_before_start) {
Austin Schuh569c7f92020-12-11 20:01:42 -0800220 if (FLAGS_print) {
221 PrintMessage(message.node_name, message.fetcher->channel(),
222 message.fetcher->context(), &builder);
223 }
Austin Schuha81454b2020-05-12 19:58:36 -0700224 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800225 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700226
227 std::cout << std::endl;
Austin Schuhee711052020-08-24 16:06:09 -0700228 std::cout << (node != nullptr ? (node->name()->str() + " ") : "")
229 << "Log starting at " << reader.realtime_start_time(node) << " ("
230 << reader.monotonic_start_time(node) << ")";
Brian Silverman9891b292020-06-23 16:34:22 -0700231 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800232 }
233
James Kuszmaul912af072020-10-31 16:06:54 -0700234 if (!found_channel) {
235 LOG(FATAL) << "Could not find any channels";
236 }
237
Austin Schuha81454b2020-05-12 19:58:36 -0700238 if (FLAGS_fetch) {
239 // New line to separate fetched messages from non-fetched messages.
240 std::cout << std::endl;
241 }
242
243 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800244
Austin Schuh51a92592020-08-09 13:17:00 -0700245 reader.Deregister();
246
James Kuszmaul38735e82019-12-07 16:42:06 -0800247 return 0;
248}