blob: e0da59e37d990c49101821abbcce07d2dfe1bc0a [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>
Ravago Jones8c23dd62020-10-24 16:21:55 -07008#include "dirent.h"
James Kuszmaul38735e82019-12-07 16:42:06 -08009
10#include "aos/configuration.h"
11#include "aos/events/logging/logger.h"
12#include "aos/events/simulated_event_loop.h"
13#include "aos/init.h"
14#include "aos/json_to_flatbuffer.h"
15#include "gflags/gflags.h"
16
James Kuszmaul38735e82019-12-07 16:42:06 -080017DEFINE_string(
18 name, "",
19 "Name to match for printing out channels. Empty means no name filter.");
20DEFINE_string(type, "",
21 "Channel type to match for printing out channels. Empty means no "
22 "type filter.");
Austin Schuha81454b2020-05-12 19:58:36 -070023DEFINE_bool(fetch, false,
24 "If true, also print out the messages from before the start of the "
25 "log file");
Austin Schuh6f3babe2020-01-26 20:34:50 -080026DEFINE_bool(raw, false,
27 "If true, just print the data out unsorted and unparsed");
Austin Schuha81454b2020-05-12 19:58:36 -070028DEFINE_bool(format_raw, true,
29 "If true and --raw is specified, print out raw data, but use the "
30 "schema to format the data.");
Austin Schuhae46f362020-04-11 19:52:56 -070031DEFINE_int32(max_vector_size, 100,
32 "If positive, vectors longer than this will not be printed");
Ravago Jones5cc9df52020-09-02 21:29:58 -070033DEFINE_bool(pretty, false,
34 "If true, pretty print the messages on multiple lines");
Austin Schuh6f3babe2020-01-26 20:34:50 -080035
Brian Silverman9891b292020-06-23 16:34:22 -070036// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
37// glog and to allow the user to readily redirect just the logged output
38// independent of any debugging information on stderr.
39void PrintMessage(const std::string_view node_name, const aos::Channel *channel,
Austin Schuh746690f2020-08-01 16:15:57 -070040 const aos::Context &context,
41 aos::FastStringBuilder *builder) {
42 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070043 aos::FlatbufferToJson(
44 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
45 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Austin Schuh746690f2020-08-01 16:15:57 -070046
Austin Schuha81454b2020-05-12 19:58:36 -070047 if (context.monotonic_remote_time != context.monotonic_event_time) {
48 std::cout << node_name << context.realtime_event_time << " ("
49 << context.monotonic_event_time << ") sent "
50 << context.realtime_remote_time << " ("
51 << context.monotonic_remote_time << ") "
52 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070053 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070054 } else {
55 std::cout << node_name << context.realtime_event_time << " ("
56 << context.monotonic_event_time << ") "
57 << channel->name()->c_str() << ' ' << channel->type()->c_str()
Austin Schuh746690f2020-08-01 16:15:57 -070058 << ": " << *builder << std::endl;
Austin Schuha81454b2020-05-12 19:58:36 -070059 }
60}
61
Ravago Jones8c23dd62020-10-24 16:21:55 -070062void SearchDirectory(std::vector<std::string> *files, std::string filename) {
63 DIR *directory = opendir(filename.c_str());
64
65 if (directory == nullptr) {
66 // its not a directory
67 // it could be a file
68 // or it could not exist
69 files->emplace_back(filename);
70 return;
71 }
72
73 struct dirent *directory_entry;
74 while ((directory_entry = readdir(directory)) != nullptr) {
75 std::string next_filename = directory_entry->d_name;
76 if (next_filename == "." || next_filename == "..") {
77 continue;
78 }
79
80 std::string path = filename + "/" + next_filename;
81 SearchDirectory(files, path);
82 }
83
84 closedir(directory);
85}
86
James Kuszmaul38735e82019-12-07 16:42:06 -080087int main(int argc, char **argv) {
88 gflags::SetUsageMessage(
Austin Schuh6f3babe2020-01-26 20:34:50 -080089 "Usage:\n"
90 " log_cat [args] logfile1 logfile2 ...\n"
91 "\n"
James Kuszmaul38735e82019-12-07 16:42:06 -080092 "This program provides a basic interface to dump data from a logfile to "
93 "stdout. Given a logfile, channel name filter, and type filter, it will "
94 "print all the messages in the logfile matching the filters. The message "
95 "filters work by taking the values of --name and --type and printing any "
96 "channel whose name contains --name as a substr and whose type contains "
97 "--type as a substr. Not specifying --name or --type leaves them free. "
98 "Calling this program without --name or --type specified prints out all "
99 "the logged data.");
100 aos::InitGoogle(&argc, &argv);
101
Austin Schuh6f3babe2020-01-26 20:34:50 -0800102 if (FLAGS_raw) {
103 if (argc != 2) {
104 LOG(FATAL) << "Expected 1 logfile as an argument.";
James Kuszmaul38735e82019-12-07 16:42:06 -0800105 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800106 aos::logger::MessageReader reader(argv[1]);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700107 std::cout << aos::FlatbufferToJson(reader.log_file_header(),
108 {.multi_line = FLAGS_pretty,
109 .max_vector_size = static_cast<size_t>(
110 FLAGS_max_vector_size)})
111 << std::endl;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800112
113 while (true) {
114 std::optional<aos::FlatbufferVector<aos::logger::MessageHeader>> message =
115 reader.ReadMessage();
116 if (!message) {
117 break;
118 }
Austin Schuha81454b2020-05-12 19:58:36 -0700119 const aos::Channel *channel =
120 reader.log_file_header()->configuration()->channels()->Get(
121 message.value().message().channel_index());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800122
Austin Schuha81454b2020-05-12 19:58:36 -0700123 if (FLAGS_format_raw && message.value().message().data() != nullptr) {
124 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700125 << aos::FlatbufferToJson(
126 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700127 {.multi_line = FLAGS_pretty, .max_vector_size = 4})
Ravago Jonescf453ab2020-05-06 21:14:53 -0700128 << ": "
Austin Schuha81454b2020-05-12 19:58:36 -0700129 << aos::FlatbufferToJson(
130 channel->schema(),
131 message.value().message().data()->data(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700132 {FLAGS_pretty,
133 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700134 << std::endl;
135 } else {
136 std::cout << aos::configuration::StrippedChannelToString(channel) << " "
Ravago Jonescf453ab2020-05-06 21:14:53 -0700137 << aos::FlatbufferToJson(
138 message.value(),
Ravago Jones5cc9df52020-09-02 21:29:58 -0700139 {FLAGS_pretty,
140 static_cast<size_t>(FLAGS_max_vector_size)})
Austin Schuha81454b2020-05-12 19:58:36 -0700141 << std::endl;
142 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800143 }
144 return 0;
James Kuszmaul38735e82019-12-07 16:42:06 -0800145 }
146
Austin Schuh6f3babe2020-01-26 20:34:50 -0800147 if (argc < 2) {
148 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
149 }
150
Austin Schuh5212cad2020-09-09 23:12:09 -0700151 std::vector<std::string> unsorted_logfiles;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800152 for (int i = 1; i < argc; ++i) {
Ravago Jones8c23dd62020-10-24 16:21:55 -0700153 SearchDirectory(&unsorted_logfiles, argv[i]);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800154 }
155
Austin Schuh11d43732020-09-21 17:28:30 -0700156 const std::vector<aos::logger::LogFile> logfiles =
Austin Schuh5212cad2020-09-09 23:12:09 -0700157 aos::logger::SortParts(unsorted_logfiles);
158
Austin Schuh6f3babe2020-01-26 20:34:50 -0800159 aos::logger::LogReader reader(logfiles);
Austin Schuha81454b2020-05-12 19:58:36 -0700160
Austin Schuh746690f2020-08-01 16:15:57 -0700161 aos::FastStringBuilder builder;
162
Austin Schuha81454b2020-05-12 19:58:36 -0700163 aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration());
164 reader.Register(&event_loop_factory);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800165
166 std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops;
167
James Kuszmaul912af072020-10-31 16:06:54 -0700168 bool found_channel = false;
169
Austin Schuh6f3babe2020-01-26 20:34:50 -0800170 for (const aos::Node *node : reader.Nodes()) {
171 std::unique_ptr<aos::EventLoop> printer_event_loop =
Austin Schuha81454b2020-05-12 19:58:36 -0700172 event_loop_factory.MakeEventLoop("printer", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800173 printer_event_loop->SkipTimingReport();
174 printer_event_loop->SkipAosLog();
175
Brian Silverman9891b292020-06-23 16:34:22 -0700176 struct MessageInfo {
177 std::string node_name;
178 std::unique_ptr<aos::RawFetcher> fetcher;
179 };
180 std::vector<MessageInfo> messages_before_start;
Austin Schuha81454b2020-05-12 19:58:36 -0700181
Austin Schuh6f3babe2020-01-26 20:34:50 -0800182 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
Brian Silverman9891b292020-06-23 16:34:22 -0700183 printer_event_loop->configuration()->channels();
184
Austin Schuh6f3babe2020-01-26 20:34:50 -0800185 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
186 const aos::Channel *channel = channels->Get(i);
187 const flatbuffers::string_view name = channel->name()->string_view();
188 const flatbuffers::string_view type = channel->type()->string_view();
189 if (name.find(FLAGS_name) != std::string::npos &&
190 type.find(FLAGS_type) != std::string::npos) {
191 if (!aos::configuration::ChannelIsReadableOnNode(
192 channel, printer_event_loop->node())) {
193 continue;
194 }
195 VLOG(1) << "Listening on " << name << " " << type;
196
197 std::string node_name =
198 node == nullptr ? ""
199 : std::string(node->name()->string_view()) + " ";
200
201 CHECK_NOTNULL(channel->schema());
Austin Schuha81454b2020-05-12 19:58:36 -0700202
Brian Silverman9891b292020-06-23 16:34:22 -0700203 // Fetch the last message on this channel from before the log start
204 // time.
Austin Schuha81454b2020-05-12 19:58:36 -0700205 if (FLAGS_fetch) {
Austin Schuha81454b2020-05-12 19:58:36 -0700206 std::unique_ptr<aos::RawFetcher> fetcher =
207 printer_event_loop->MakeRawFetcher(channel);
208 if (fetcher->Fetch()) {
Brian Silverman9891b292020-06-23 16:34:22 -0700209 MessageInfo message{.node_name = node_name,
210 .fetcher = std::move(fetcher)};
Austin Schuha81454b2020-05-12 19:58:36 -0700211 // Insert it sorted into the vector so we can print in time order
212 // instead of channel order at the start.
213 auto it = std::lower_bound(
Brian Silverman9891b292020-06-23 16:34:22 -0700214 messages_before_start.begin(), messages_before_start.end(),
215 message, [](const MessageInfo &lhs, const MessageInfo &rhs) {
216 if (lhs.fetcher->context().monotonic_event_time <
217 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700218 return true;
219 }
Brian Silverman9891b292020-06-23 16:34:22 -0700220 if (lhs.fetcher->context().monotonic_event_time >
221 rhs.fetcher->context().monotonic_event_time) {
Austin Schuha81454b2020-05-12 19:58:36 -0700222 return false;
223 }
Brian Silverman9891b292020-06-23 16:34:22 -0700224 return lhs.fetcher->channel() < rhs.fetcher->channel();
Austin Schuha81454b2020-05-12 19:58:36 -0700225 });
Brian Silverman9891b292020-06-23 16:34:22 -0700226 messages_before_start.insert(it, std::move(message));
Austin Schuha81454b2020-05-12 19:58:36 -0700227 }
228 }
229
Austin Schuh6f3babe2020-01-26 20:34:50 -0800230 printer_event_loop->MakeRawWatcher(
Ravago Jones5cc9df52020-09-02 21:29:58 -0700231 channel, [channel, node_name, &builder](const aos::Context &context,
232 const void * /*message*/) {
Austin Schuh746690f2020-08-01 16:15:57 -0700233 PrintMessage(node_name, channel, context, &builder);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800234 });
235 found_channel = true;
236 }
237 }
238
Brian Silverman9891b292020-06-23 16:34:22 -0700239 // Print the messages from before the log start time.
Austin Schuha81454b2020-05-12 19:58:36 -0700240 // TODO(austin): Sort between nodes too when it becomes annoying enough.
Brian Silverman9891b292020-06-23 16:34:22 -0700241 for (const MessageInfo &message : messages_before_start) {
242 PrintMessage(message.node_name, message.fetcher->channel(),
Austin Schuh746690f2020-08-01 16:15:57 -0700243 message.fetcher->context(), &builder);
Austin Schuha81454b2020-05-12 19:58:36 -0700244 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800245 printer_event_loops.emplace_back(std::move(printer_event_loop));
Brian Silverman9891b292020-06-23 16:34:22 -0700246
247 std::cout << std::endl;
Austin Schuhee711052020-08-24 16:06:09 -0700248 std::cout << (node != nullptr ? (node->name()->str() + " ") : "")
249 << "Log starting at " << reader.realtime_start_time(node) << " ("
250 << reader.monotonic_start_time(node) << ")";
Brian Silverman9891b292020-06-23 16:34:22 -0700251 std::cout << std::endl << std::endl;
James Kuszmaul38735e82019-12-07 16:42:06 -0800252 }
253
James Kuszmaul912af072020-10-31 16:06:54 -0700254 if (!found_channel) {
255 LOG(FATAL) << "Could not find any channels";
256 }
257
Austin Schuha81454b2020-05-12 19:58:36 -0700258 if (FLAGS_fetch) {
259 // New line to separate fetched messages from non-fetched messages.
260 std::cout << std::endl;
261 }
262
263 event_loop_factory.Run();
James Kuszmaul38735e82019-12-07 16:42:06 -0800264
Austin Schuh51a92592020-08-09 13:17:00 -0700265 reader.Deregister();
266
James Kuszmaul38735e82019-12-07 16:42:06 -0800267 return 0;
268}