blob: f406d24c384bb0174ed44c75a523ca582a4cfd7a [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 Schuh6f3babe2020-01-26 20:34:50 -080034
Brian Silverman9891b292020-06-23 16:34:22 -070035// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
36// glog and to allow the user to readily redirect just the logged output
37// independent of any debugging information on stderr.
38void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
Austin Schuh746690f2020-08-01 16:15:57 -070039 const aos::Context &context,
40 aos::FastStringBuilder *builder) {
41 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070042 aos::FlatbufferToJson(
43 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
44 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Austin Schuh746690f2020-08-01 16:15:57 -070045
Austin Schuha81454b2020-05-12 19:58:36 -070046 if (context.monotonic_remote_time != context.monotonic_event_time) {
47 std::cout << node_name << context.realtime_event_time << " ("
48 << context.monotonic_event_time << ") sent "
49 << context.realtime_remote_time << " ("
50 << context.monotonic_remote_time << ") "
51 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070052 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070053 } else {
54 std::cout << node_name << context.realtime_event_time << " ("
55 << context.monotonic_event_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 }
59}
60
James Kuszmaul38735e82019-12-07 16:42:06 -080061int main(int argc, char **argv) {
62 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -080063 "Usage:\n"
64 " log_cat [args] logfile1 logfile2 ...\n"
65 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -080066 "This program provides a basic interface to dump data from a logfile to "
67 "stdout. Given a logfile, channel name filter, and type filter, it will "
68 "print all the messages in the logfile matching the filters. The message "
69 "filters work by taking the values of --name and --type and printing any "
70 "channel whose name contains --name as a substr and whose type contains "
71 "--type as a substr. Not specifying --name or --type leaves them free. "
72 "Calling this program without --name or --type specified prints out all "
73 "the logged data.");
74 aos::InitGoogle(&argc, &argv);
75
Austin Schuh6f3babe2020-01-26 20:34:50 -080076 if (FLAGS_raw) {
77 if (argc != 2) {
78 LOG(FATAL) << "Expected 1 logfile as an argument.";
James Kuszmaul38735e82019-12-07 16:42:06 -080079 }
Austin Schuh6f3babe2020-01-26 20:34:50 -080080 aos::logger::MessageReader reader(argv[1]);
Austin Schuh2f8fd752020-09-01 22:38:28 -070081 std::cout << aos::FlatbufferToJson(reader.log_file_header(),
82 {.multi_line = FLAGS_pretty,
83 .max_vector_size = static_cast<size_t>(
84 FLAGS_max_vector_size)})
85 << std::endl;
Austin Schuh6f3babe2020-01-26 20:34:50 -080086
87 while (true) {
88 std::optional<aos::FlatbufferVector<aos::logger::MessageHeader>> message =
89 reader.ReadMessage();
90 if (!message) {
91 break;
92 }
Austin Schuha81454b2020-05-12 19:58:36 -070093 const aos::Channel *channel =
94 reader.log_file_header()->configuration()->channels()->Get(
95 message.value().message().channel_index());
Austin Schuh6f3babe2020-01-26 20:34:50 -080096
Austin Schuha81454b2020-05-12 19:58:36 -070097 if (FLAGS_format_raw && message.value().message().data() != nullptr) {
98 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -070099 << aos::FlatbufferToJson(
100 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700101 {.multi_line = FLAGS_pretty, .max_vector_size = 4})
Ravago Jonescf453ab2020-05-06 21:14:53 -0700102 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -0700103 << aos::FlatbufferToJson(
104 channel->schema(),
105 message.value().message().data()->data(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700106 {FLAGS_pretty,
107 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700108 << std::endl;
109 } else {
110 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700111 << aos::FlatbufferToJson(
112 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700113 {FLAGS_pretty,
114 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700115 << std::endl;
116 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800117 }
118 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800119 }
120
Austin Schuh6f3babe2020-01-26 20:34:50 -0800121 if (argc < 2) {
122 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
123 }
124
Austin Schuh5212cad2020-09-09 23:12:09 -0700125 std::vector<std::string> unsorted_logfiles;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800126 for (int i = 1; i < argc; ++i) {
Austin Schuh5212cad2020-09-09 23:12:09 -0700127 unsorted_logfiles.emplace_back(std::string(argv[i]));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800128 }
129
Austin Schuh5212cad2020-09-09 23:12:09 -0700130 std::vector<std::vector<std::string>> logfiles =
131 aos::logger::SortParts(unsorted_logfiles);
132
Austin Schuh6f3babe2020-01-26 20:34:50 -0800133 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700134
Austin Schuh746690f2020-08-01 16:15:57 -0700135 aos::FastStringBuilder builder;
136
Austin Schuha81454b2020-05-12 19:58:36 -0700137 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
138 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800139
140 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
141
142 for (const aos::Node *node : reader.Nodes()) {
143 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700144 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800145 printer_event_loop->SkipTimingReport();
146 printer_event_loop->SkipAosLog();
147
Brian Silverman9891b292020-06-23 16:34:22 -0700148 struct MessageInfo {
149 std::string node_name;
150 std::unique_ptr<aos::RawFetcher> fetcher;
151 };
152 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700153
Austin Schuh6f3babe2020-01-26 20:34:50 -0800154 bool found_channel = false;
155 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700156 printer_event_loop->configuration()->channels();
157
Austin Schuh6f3babe2020-01-26 20:34:50 -0800158 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
159 const aos::Channel *channel = channels->Get(i);
160 const flatbuffers::string_view name = channel->name()->string_view();
161 const flatbuffers::string_view type = channel->type()->string_view();
162 if (name.find(FLAGS_name) != std::string::npos &&
163 type.find(FLAGS_type) != std::string::npos) {
164 if (!aos::configuration::ChannelIsReadableOnNode(
165 channel, printer_event_loop->node())) {
166 continue;
167 }
168 VLOG(1) << "Listening on " << name << " " << type;
169
170 std::string node_name =
171 node == nullptr ? ""
172 : std::string(node->name()->string_view()) + " ";
173
174 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700175
Brian Silverman9891b292020-06-23 16:34:22 -0700176 // Fetch the last message on this channel from before the log start
177 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700178 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700179 std::unique_ptr<aos::RawFetcher> fetcher =
180 printer_event_loop->MakeRawFetcher(channel);
181 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700182 MessageInfo message{.node_name = node_name,
183 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700184 // Insert it sorted into the vector so we can print in time order
185 // instead of channel order at the start.
186 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700187 messages_before_start.begin(), messages_before_start.end(),
188 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
189 if (lhs.fetcher->context().monotonic_event_time <
190 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700191 return true;
192 }
Brian Silverman9891b292020-06-23 16:34:22 -0700193 if (lhs.fetcher->context().monotonic_event_time >
194 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700195 return false;
196 }
Brian Silverman9891b292020-06-23 16:34:22 -0700197 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700198 });
Brian Silverman9891b292020-06-23 16:34:22 -0700199 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700200 }
201 }
202
Austin Schuh6f3babe2020-01-26 20:34:50 -0800203 printer_event_loop->MakeRawWatcher(
Ravago Jones5cc9df52020-09-02 21:29:58 -0700204 channel, [channel, node_name, &builder](const aos::Context &context,
205 const void * /*message*/) {
Austin Schuh746690f2020-08-01 16:15:57 -0700206 PrintMessage(node_name, channel, context, &builder);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800207 });
208 found_channel = true;
209 }
210 }
211
212 if (!found_channel) {
213 LOG(FATAL) << "Could not find any channels";
214 }
Brian Silverman9891b292020-06-23 16:34:22 -0700215
216 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700217 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700218 for (const MessageInfo &message : messages_before_start) {
219 PrintMessage(message.node_name, message.fetcher->channel(),
Austin Schuh746690f2020-08-01 16:15:57 -0700220 message.fetcher->context(), &builder);
Austin Schuha81454b2020-05-12 19:58:36 -0700221 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800222 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700223
224 std::cout << std::endl;
Austin Schuhee711052020-08-24 16:06:09 -0700225 std::cout << (node != nullptr ? (node->name()->str() + " ") : "")
226 << "Log starting at " << reader.realtime_start_time(node) << " ("
227 << reader.monotonic_start_time(node) << ")";
Brian Silverman9891b292020-06-23 16:34:22 -0700228 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800229 }
230
Austin Schuha81454b2020-05-12 19:58:36 -0700231 if (FLAGS_fetch) {
232 // New line to separate fetched messages from non-fetched messages.
233 std::cout << std::endl;
234 }
235
236 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800237
Austin Schuh51a92592020-08-09 13:17:00 -0700238 reader.Deregister();
239
James Kuszmaul38735e82019-12-07 16:42:06 -0800240 return 0;
241}