blob: b31bba0169f27fb2349c93e69254548791cc0841 [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");
Austin Schuh6f3babe2020-01-26 20:34:50 -080032
Brian Silverman9891b292020-06-23 16:34:22 -070033// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
34// glog and to allow the user to readily redirect just the logged output
35// independent of any debugging information on stderr.
36void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
Austin Schuh746690f2020-08-01 16:15:57 -070037 const aos::Context &context,
38 aos::FastStringBuilder *builder) {
39 builder->Reset();
40 aos::FlatbufferToJson(builder, channel->schema(),
41 static_cast<const uint8_t *>(context.data),
42 {false, static_cast<size_t>(FLAGS_max_vector_size)});
43
Austin Schuha81454b2020-05-12 19:58:36 -070044 if (context.monotonic_remote_time != context.monotonic_event_time) {
45 std::cout << node_name << context.realtime_event_time << " ("
46 << context.monotonic_event_time << ") sent "
47 << context.realtime_remote_time << " ("
48 << context.monotonic_remote_time << ") "
49 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070050 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070051 } else {
52 std::cout << node_name << context.realtime_event_time << " ("
53 << context.monotonic_event_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 }
57}
58
James Kuszmaul38735e82019-12-07 16:42:06 -080059int main(int argc, char **argv) {
60 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -080061 "Usage:\n"
62 " log_cat [args] logfile1 logfile2 ...\n"
63 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -080064 "This program provides a basic interface to dump data from a logfile to "
65 "stdout. Given a logfile, channel name filter, and type filter, it will "
66 "print all the messages in the logfile matching the filters. The message "
67 "filters work by taking the values of --name and --type and printing any "
68 "channel whose name contains --name as a substr and whose type contains "
69 "--type as a substr. Not specifying --name or --type leaves them free. "
70 "Calling this program without --name or --type specified prints out all "
71 "the logged data.");
72 aos::InitGoogle(&argc, &argv);
73
Austin Schuh6f3babe2020-01-26 20:34:50 -080074 if (FLAGS_raw) {
75 if (argc != 2) {
76 LOG(FATAL) << "Expected 1 logfile as an argument.";
James Kuszmaul38735e82019-12-07 16:42:06 -080077 }
Austin Schuh6f3babe2020-01-26 20:34:50 -080078 aos::logger::MessageReader reader(argv[1]);
79
80 while (true) {
81 std::optional<aos::FlatbufferVector<aos::logger::MessageHeader>> message =
82 reader.ReadMessage();
83 if (!message) {
84 break;
85 }
Austin Schuha81454b2020-05-12 19:58:36 -070086 const aos::Channel *channel =
87 reader.log_file_header()->configuration()->channels()->Get(
88 message.value().message().channel_index());
Austin Schuh6f3babe2020-01-26 20:34:50 -080089
Austin Schuha81454b2020-05-12 19:58:36 -070090 if (FLAGS_format_raw && message.value().message().data() != nullptr) {
91 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -070092 << aos::FlatbufferToJson(
93 message.value(),
94 {.multi_line = false, .max_vector_size = 4})
95 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -070096 << aos::FlatbufferToJson(
97 channel->schema(),
98 message.value().message().data()->data(),
Ravago Jonescf453ab2020-05-06 21:14:53 -070099 {false, static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700100 << std::endl;
101 } else {
102 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700103 << aos::FlatbufferToJson(
104 message.value(),
105 {false, static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700106 << std::endl;
107 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800108 }
109 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800110 }
111
Austin Schuh6f3babe2020-01-26 20:34:50 -0800112 if (argc < 2) {
113 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
114 }
115
116 std::vector<std::vector<std::string>> logfiles;
117
118 for (int i = 1; i < argc; ++i) {
119 logfiles.emplace_back(std::vector<std::string>{std::string(argv[i])});
120 }
121
122 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700123
Austin Schuh746690f2020-08-01 16:15:57 -0700124 aos::FastStringBuilder builder;
125
Austin Schuha81454b2020-05-12 19:58:36 -0700126 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
127 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800128
129 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
130
131 for (const aos::Node *node : reader.Nodes()) {
132 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700133 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800134 printer_event_loop->SkipTimingReport();
135 printer_event_loop->SkipAosLog();
136
Brian Silverman9891b292020-06-23 16:34:22 -0700137 struct MessageInfo {
138 std::string node_name;
139 std::unique_ptr<aos::RawFetcher> fetcher;
140 };
141 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700142
Austin Schuh6f3babe2020-01-26 20:34:50 -0800143 bool found_channel = false;
144 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700145 printer_event_loop->configuration()->channels();
146
Austin Schuh6f3babe2020-01-26 20:34:50 -0800147 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
148 const aos::Channel *channel = channels->Get(i);
149 const flatbuffers::string_view name = channel->name()->string_view();
150 const flatbuffers::string_view type = channel->type()->string_view();
151 if (name.find(FLAGS_name) != std::string::npos &&
152 type.find(FLAGS_type) != std::string::npos) {
153 if (!aos::configuration::ChannelIsReadableOnNode(
154 channel, printer_event_loop->node())) {
155 continue;
156 }
157 VLOG(1) << "Listening on " << name << " " << type;
158
159 std::string node_name =
160 node == nullptr ? ""
161 : std::string(node->name()->string_view()) + " ";
162
163 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700164
Brian Silverman9891b292020-06-23 16:34:22 -0700165 // Fetch the last message on this channel from before the log start
166 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700167 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700168 std::unique_ptr<aos::RawFetcher> fetcher =
169 printer_event_loop->MakeRawFetcher(channel);
170 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700171 MessageInfo message{.node_name = node_name,
172 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700173 // Insert it sorted into the vector so we can print in time order
174 // instead of channel order at the start.
175 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700176 messages_before_start.begin(), messages_before_start.end(),
177 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
178 if (lhs.fetcher->context().monotonic_event_time <
179 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700180 return true;
181 }
Brian Silverman9891b292020-06-23 16:34:22 -0700182 if (lhs.fetcher->context().monotonic_event_time >
183 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700184 return false;
185 }
Brian Silverman9891b292020-06-23 16:34:22 -0700186 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700187 });
Brian Silverman9891b292020-06-23 16:34:22 -0700188 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700189 }
190 }
191
Austin Schuh6f3babe2020-01-26 20:34:50 -0800192 printer_event_loop->MakeRawWatcher(
Austin Schuh746690f2020-08-01 16:15:57 -0700193 channel,
194 [channel, node_name, &builder](const aos::Context &context,
195 const void * /*message*/) {
196 PrintMessage(node_name, channel, context, &builder);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800197 });
198 found_channel = true;
199 }
200 }
201
202 if (!found_channel) {
203 LOG(FATAL) << "Could not find any channels";
204 }
Brian Silverman9891b292020-06-23 16:34:22 -0700205
206 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700207 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700208 for (const MessageInfo &message : messages_before_start) {
209 PrintMessage(message.node_name, message.fetcher->channel(),
Austin Schuh746690f2020-08-01 16:15:57 -0700210 message.fetcher->context(), &builder);
Austin Schuha81454b2020-05-12 19:58:36 -0700211 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800212 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700213
214 std::cout << std::endl;
215 std::cout << "Log starting at " << reader.realtime_start_time() << " ("
216 << reader.monotonic_start_time() << ")";
217 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800218 }
219
Austin Schuha81454b2020-05-12 19:58:36 -0700220 if (FLAGS_fetch) {
221 // New line to separate fetched messages from non-fetched messages.
222 std::cout << std::endl;
223 }
224
225 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800226
Austin Schuh51a92592020-08-09 13:17:00 -0700227 reader.Deregister();
228
James Kuszmaul38735e82019-12-07 16:42:06 -0800229 return 0;
230}