Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 1 | #include <algorithm> |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 2 | #include <iostream> |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 3 | #include <memory> |
| 4 | #include <optional> |
| 5 | #include <string> |
| 6 | #include <string_view> |
| 7 | #include <vector> |
Ravago Jones | 8c23dd6 | 2020-10-24 16:21:55 -0700 | [diff] [blame] | 8 | #include "dirent.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 9 | |
| 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 Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 17 | DEFINE_string( |
| 18 | name, "", |
| 19 | "Name to match for printing out channels. Empty means no name filter."); |
| 20 | DEFINE_string(type, "", |
| 21 | "Channel type to match for printing out channels. Empty means no " |
| 22 | "type filter."); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 23 | DEFINE_bool(fetch, false, |
| 24 | "If true, also print out the messages from before the start of the " |
| 25 | "log file"); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 26 | DEFINE_bool(raw, false, |
| 27 | "If true, just print the data out unsorted and unparsed"); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 28 | DEFINE_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 Schuh | ae46f36 | 2020-04-11 19:52:56 -0700 | [diff] [blame] | 31 | DEFINE_int32(max_vector_size, 100, |
| 32 | "If positive, vectors longer than this will not be printed"); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 33 | DEFINE_bool(pretty, false, |
| 34 | "If true, pretty print the messages on multiple lines"); |
Austin Schuh | 569c7f9 | 2020-12-11 20:01:42 -0800 | [diff] [blame^] | 35 | DEFINE_bool(print, true, |
| 36 | "If true, actually print the messages. If false, discard them, " |
| 37 | "confirming they can be parsed."); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 38 | |
Austin Schuh | c55d166 | 2020-12-04 22:11:27 -0800 | [diff] [blame] | 39 | bool EndsWith(std::string_view str, std::string_view ending) { |
| 40 | return str.size() >= ending.size() && |
| 41 | str.substr(str.size() - ending.size()) == ending; |
| 42 | } |
| 43 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 44 | // Print the flatbuffer out to stdout, both to remove the unnecessary cruft from |
| 45 | // glog and to allow the user to readily redirect just the logged output |
| 46 | // independent of any debugging information on stderr. |
| 47 | void PrintMessage(const std::string_view node_name, const aos::Channel *channel, |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 48 | const aos::Context &context, |
| 49 | aos::FastStringBuilder *builder) { |
| 50 | builder->Reset(); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 51 | aos::FlatbufferToJson( |
| 52 | builder, channel->schema(), static_cast<const uint8_t *>(context.data), |
| 53 | {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)}); |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 54 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 55 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 56 | std::cout << node_name << context.realtime_event_time << " (" |
| 57 | << context.monotonic_event_time << ") sent " |
| 58 | << context.realtime_remote_time << " (" |
| 59 | << context.monotonic_remote_time << ") " |
| 60 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 61 | << ": " << *builder << std::endl; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 62 | } else { |
| 63 | std::cout << node_name << context.realtime_event_time << " (" |
| 64 | << context.monotonic_event_time << ") " |
| 65 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 66 | << ": " << *builder << std::endl; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Ravago Jones | 8c23dd6 | 2020-10-24 16:21:55 -0700 | [diff] [blame] | 70 | void SearchDirectory(std::vector<std::string> *files, std::string filename) { |
| 71 | DIR *directory = opendir(filename.c_str()); |
| 72 | |
| 73 | if (directory == nullptr) { |
| 74 | // its not a directory |
| 75 | // it could be a file |
| 76 | // or it could not exist |
Austin Schuh | c55d166 | 2020-12-04 22:11:27 -0800 | [diff] [blame] | 77 | if (EndsWith(filename, ".bfbs") || EndsWith(filename, ".bfbs.xz")) { |
| 78 | files->emplace_back(filename); |
| 79 | } |
Ravago Jones | 8c23dd6 | 2020-10-24 16:21:55 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | struct dirent *directory_entry; |
| 84 | while ((directory_entry = readdir(directory)) != nullptr) { |
| 85 | std::string next_filename = directory_entry->d_name; |
| 86 | if (next_filename == "." || next_filename == "..") { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | std::string path = filename + "/" + next_filename; |
| 91 | SearchDirectory(files, path); |
| 92 | } |
| 93 | |
| 94 | closedir(directory); |
| 95 | } |
| 96 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 97 | int main(int argc, char **argv) { |
| 98 | gflags::SetUsageMessage( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 99 | "Usage:\n" |
| 100 | " log_cat [args] logfile1 logfile2 ...\n" |
| 101 | "\n" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 102 | "This program provides a basic interface to dump data from a logfile to " |
| 103 | "stdout. Given a logfile, channel name filter, and type filter, it will " |
| 104 | "print all the messages in the logfile matching the filters. The message " |
| 105 | "filters work by taking the values of --name and --type and printing any " |
| 106 | "channel whose name contains --name as a substr and whose type contains " |
| 107 | "--type as a substr. Not specifying --name or --type leaves them free. " |
| 108 | "Calling this program without --name or --type specified prints out all " |
| 109 | "the logged data."); |
| 110 | aos::InitGoogle(&argc, &argv); |
| 111 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 112 | if (FLAGS_raw) { |
| 113 | if (argc != 2) { |
| 114 | LOG(FATAL) << "Expected 1 logfile as an argument."; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 115 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 116 | aos::logger::MessageReader reader(argv[1]); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 117 | std::cout << aos::FlatbufferToJson(reader.log_file_header(), |
| 118 | {.multi_line = FLAGS_pretty, |
| 119 | .max_vector_size = static_cast<size_t>( |
| 120 | FLAGS_max_vector_size)}) |
| 121 | << std::endl; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 122 | |
| 123 | while (true) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 124 | std::optional< |
| 125 | aos::SizePrefixedFlatbufferVector<aos::logger::MessageHeader>> |
| 126 | message = reader.ReadMessage(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 127 | if (!message) { |
| 128 | break; |
| 129 | } |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 130 | const aos::Channel *channel = |
| 131 | reader.log_file_header()->configuration()->channels()->Get( |
| 132 | message.value().message().channel_index()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 133 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 134 | if (FLAGS_format_raw && message.value().message().data() != nullptr) { |
| 135 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 136 | << aos::FlatbufferToJson( |
| 137 | message.value(), |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 138 | {.multi_line = FLAGS_pretty, .max_vector_size = 4}) |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 139 | << ": " |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 140 | << aos::FlatbufferToJson( |
| 141 | channel->schema(), |
| 142 | message.value().message().data()->data(), |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 143 | {FLAGS_pretty, |
| 144 | static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 145 | << std::endl; |
| 146 | } else { |
| 147 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 148 | << aos::FlatbufferToJson( |
| 149 | message.value(), |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 150 | {FLAGS_pretty, |
| 151 | static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 152 | << std::endl; |
| 153 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 154 | } |
| 155 | return 0; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 158 | if (argc < 2) { |
| 159 | LOG(FATAL) << "Expected at least 1 logfile as an argument."; |
| 160 | } |
| 161 | |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 162 | std::vector<std::string> unsorted_logfiles; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 163 | for (int i = 1; i < argc; ++i) { |
Ravago Jones | 8c23dd6 | 2020-10-24 16:21:55 -0700 | [diff] [blame] | 164 | SearchDirectory(&unsorted_logfiles, argv[i]); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 167 | const std::vector<aos::logger::LogFile> logfiles = |
Austin Schuh | 5212cad | 2020-09-09 23:12:09 -0700 | [diff] [blame] | 168 | aos::logger::SortParts(unsorted_logfiles); |
| 169 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 170 | aos::logger::LogReader reader(logfiles); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 171 | |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 172 | aos::FastStringBuilder builder; |
| 173 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 174 | aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration()); |
| 175 | reader.Register(&event_loop_factory); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 176 | |
| 177 | std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops; |
| 178 | |
James Kuszmaul | 912af07 | 2020-10-31 16:06:54 -0700 | [diff] [blame] | 179 | bool found_channel = false; |
| 180 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 181 | for (const aos::Node *node : reader.Nodes()) { |
| 182 | std::unique_ptr<aos::EventLoop> printer_event_loop = |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 183 | event_loop_factory.MakeEventLoop("printer", node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 184 | printer_event_loop->SkipTimingReport(); |
| 185 | printer_event_loop->SkipAosLog(); |
| 186 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 187 | struct MessageInfo { |
| 188 | std::string node_name; |
| 189 | std::unique_ptr<aos::RawFetcher> fetcher; |
| 190 | }; |
| 191 | std::vector<MessageInfo> messages_before_start; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 192 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 193 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 194 | printer_event_loop->configuration()->channels(); |
| 195 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 196 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 197 | const aos::Channel *channel = channels->Get(i); |
| 198 | const flatbuffers::string_view name = channel->name()->string_view(); |
| 199 | const flatbuffers::string_view type = channel->type()->string_view(); |
| 200 | if (name.find(FLAGS_name) != std::string::npos && |
| 201 | type.find(FLAGS_type) != std::string::npos) { |
| 202 | if (!aos::configuration::ChannelIsReadableOnNode( |
| 203 | channel, printer_event_loop->node())) { |
| 204 | continue; |
| 205 | } |
| 206 | VLOG(1) << "Listening on " << name << " " << type; |
| 207 | |
| 208 | std::string node_name = |
| 209 | node == nullptr ? "" |
| 210 | : std::string(node->name()->string_view()) + " "; |
| 211 | |
| 212 | CHECK_NOTNULL(channel->schema()); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 213 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 214 | // Fetch the last message on this channel from before the log start |
| 215 | // time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 216 | if (FLAGS_fetch) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 217 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 218 | printer_event_loop->MakeRawFetcher(channel); |
| 219 | if (fetcher->Fetch()) { |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 220 | MessageInfo message{.node_name = node_name, |
| 221 | .fetcher = std::move(fetcher)}; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 222 | // Insert it sorted into the vector so we can print in time order |
| 223 | // instead of channel order at the start. |
| 224 | auto it = std::lower_bound( |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 225 | messages_before_start.begin(), messages_before_start.end(), |
| 226 | message, [](const MessageInfo &lhs, const MessageInfo &rhs) { |
| 227 | if (lhs.fetcher->context().monotonic_event_time < |
| 228 | rhs.fetcher->context().monotonic_event_time) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 229 | return true; |
| 230 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 231 | if (lhs.fetcher->context().monotonic_event_time > |
| 232 | rhs.fetcher->context().monotonic_event_time) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 235 | return lhs.fetcher->channel() < rhs.fetcher->channel(); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 236 | }); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 237 | messages_before_start.insert(it, std::move(message)); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 241 | printer_event_loop->MakeRawWatcher( |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 242 | channel, [channel, node_name, &builder](const aos::Context &context, |
| 243 | const void * /*message*/) { |
Austin Schuh | 569c7f9 | 2020-12-11 20:01:42 -0800 | [diff] [blame^] | 244 | if (FLAGS_print) { |
| 245 | PrintMessage(node_name, channel, context, &builder); |
| 246 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 247 | }); |
| 248 | found_channel = true; |
| 249 | } |
| 250 | } |
| 251 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 252 | // Print the messages from before the log start time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 253 | // TODO(austin): Sort between nodes too when it becomes annoying enough. |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 254 | for (const MessageInfo &message : messages_before_start) { |
Austin Schuh | 569c7f9 | 2020-12-11 20:01:42 -0800 | [diff] [blame^] | 255 | if (FLAGS_print) { |
| 256 | PrintMessage(message.node_name, message.fetcher->channel(), |
| 257 | message.fetcher->context(), &builder); |
| 258 | } |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 259 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 260 | printer_event_loops.emplace_back(std::move(printer_event_loop)); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 261 | |
| 262 | std::cout << std::endl; |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 263 | std::cout << (node != nullptr ? (node->name()->str() + " ") : "") |
| 264 | << "Log starting at " << reader.realtime_start_time(node) << " (" |
| 265 | << reader.monotonic_start_time(node) << ")"; |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 266 | std::cout << std::endl << std::endl; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 267 | } |
| 268 | |
James Kuszmaul | 912af07 | 2020-10-31 16:06:54 -0700 | [diff] [blame] | 269 | if (!found_channel) { |
| 270 | LOG(FATAL) << "Could not find any channels"; |
| 271 | } |
| 272 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 273 | if (FLAGS_fetch) { |
| 274 | // New line to separate fetched messages from non-fetched messages. |
| 275 | std::cout << std::endl; |
| 276 | } |
| 277 | |
| 278 | event_loop_factory.Run(); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 279 | |
Austin Schuh | 51a9259 | 2020-08-09 13:17:00 -0700 | [diff] [blame] | 280 | reader.Deregister(); |
| 281 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 282 | return 0; |
| 283 | } |