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