blob: b9d53ff62e88c94d20a1656e529f332951526462 [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]);
81
82 while (true) {
83 std::optional<aos::FlatbufferVector<aos::logger::MessageHeader>> message =
84 reader.ReadMessage();
85 if (!message) {
86 break;
87 }
Austin Schuha81454b2020-05-12 19:58:36 -070088 const aos::Channel *channel =
89 reader.log_file_header()->configuration()->channels()->Get(
90 message.value().message().channel_index());
Austin Schuh6f3babe2020-01-26 20:34:50 -080091
Austin Schuha81454b2020-05-12 19:58:36 -070092 if (FLAGS_format_raw && message.value().message().data() != nullptr) {
93 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -070094 << aos::FlatbufferToJson(
95 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -070096 {.multi_line = FLAGS_pretty, .max_vector_size = 4})
Ravago Jonescf453ab2020-05-06 21:14:53 -070097 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -070098 << aos::FlatbufferToJson(
99 channel->schema(),
100 message.value().message().data()->data(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700101 {FLAGS_pretty,
102 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700103 << std::endl;
104 } else {
105 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700106 << aos::FlatbufferToJson(
107 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700108 {FLAGS_pretty,
109 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700110 << std::endl;
111 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800112 }
113 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800114 }
115
Austin Schuh6f3babe2020-01-26 20:34:50 -0800116 if (argc < 2) {
117 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
118 }
119
120 std::vector<std::vector<std::string>> logfiles;
121
122 for (int i = 1; i < argc; ++i) {
123 logfiles.emplace_back(std::vector<std::string>{std::string(argv[i])});
124 }
125
126 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700127
Austin Schuh746690f2020-08-01 16:15:57 -0700128 aos::FastStringBuilder builder;
129
Austin Schuha81454b2020-05-12 19:58:36 -0700130 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
131 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800132
133 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
134
135 for (const aos::Node *node : reader.Nodes()) {
136 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700137 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800138 printer_event_loop->SkipTimingReport();
139 printer_event_loop->SkipAosLog();
140
Brian Silverman9891b292020-06-23 16:34:22 -0700141 struct MessageInfo {
142 std::string node_name;
143 std::unique_ptr<aos::RawFetcher> fetcher;
144 };
145 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700146
Austin Schuh6f3babe2020-01-26 20:34:50 -0800147 bool found_channel = false;
148 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700149 printer_event_loop->configuration()->channels();
150
Austin Schuh6f3babe2020-01-26 20:34:50 -0800151 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
152 const aos::Channel *channel = channels->Get(i);
153 const flatbuffers::string_view name = channel->name()->string_view();
154 const flatbuffers::string_view type = channel->type()->string_view();
155 if (name.find(FLAGS_name) != std::string::npos &&
156 type.find(FLAGS_type) != std::string::npos) {
157 if (!aos::configuration::ChannelIsReadableOnNode(
158 channel, printer_event_loop->node())) {
159 continue;
160 }
161 VLOG(1) << "Listening on " << name << " " << type;
162
163 std::string node_name =
164 node == nullptr ? ""
165 : std::string(node->name()->string_view()) + " ";
166
167 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700168
Brian Silverman9891b292020-06-23 16:34:22 -0700169 // Fetch the last message on this channel from before the log start
170 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700171 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700172 std::unique_ptr<aos::RawFetcher> fetcher =
173 printer_event_loop->MakeRawFetcher(channel);
174 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700175 MessageInfo message{.node_name = node_name,
176 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700177 // Insert it sorted into the vector so we can print in time order
178 // instead of channel order at the start.
179 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700180 messages_before_start.begin(), messages_before_start.end(),
181 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
182 if (lhs.fetcher->context().monotonic_event_time <
183 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700184 return true;
185 }
Brian Silverman9891b292020-06-23 16:34:22 -0700186 if (lhs.fetcher->context().monotonic_event_time >
187 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700188 return false;
189 }
Brian Silverman9891b292020-06-23 16:34:22 -0700190 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700191 });
Brian Silverman9891b292020-06-23 16:34:22 -0700192 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700193 }
194 }
195
Austin Schuh6f3babe2020-01-26 20:34:50 -0800196 printer_event_loop->MakeRawWatcher(
Ravago Jones5cc9df52020-09-02 21:29:58 -0700197 channel, [channel, node_name, &builder](const aos::Context &context,
198 const void * /*message*/) {
Austin Schuh746690f2020-08-01 16:15:57 -0700199 PrintMessage(node_name, channel, context, &builder);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800200 });
201 found_channel = true;
202 }
203 }
204
205 if (!found_channel) {
206 LOG(FATAL) << "Could not find any channels";
207 }
Brian Silverman9891b292020-06-23 16:34:22 -0700208
209 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700210 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700211 for (const MessageInfo &message : messages_before_start) {
212 PrintMessage(message.node_name, message.fetcher->channel(),
Austin Schuh746690f2020-08-01 16:15:57 -0700213 message.fetcher->context(), &builder);
Austin Schuha81454b2020-05-12 19:58:36 -0700214 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800215 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700216
217 std::cout << std::endl;
Austin Schuhee711052020-08-24 16:06:09 -0700218 std::cout << (node != nullptr ? (node->name()->str() + " ") : "")
219 << "Log starting at " << reader.realtime_start_time(node) << " ("
220 << reader.monotonic_start_time(node) << ")";
Brian Silverman9891b292020-06-23 16:34:22 -0700221 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800222 }
223
Austin Schuha81454b2020-05-12 19:58:36 -0700224 if (FLAGS_fetch) {
225 // New line to separate fetched messages from non-fetched messages.
226 std::cout << std::endl;
227 }
228
229 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800230
Austin Schuh51a92592020-08-09 13:17:00 -0700231 reader.Deregister();
232
James Kuszmaul38735e82019-12-07 16:42:06 -0800233 return 0;
234}