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"); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 32 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 33 | // 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. |
| 36 | void PrintMessage(const std::string_view node_name, const aos::Channel *channel, |
| 37 | const aos::Context &context) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 38 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 39 | std::cout << node_name << context.realtime_event_time << " (" |
| 40 | << context.monotonic_event_time << ") sent " |
| 41 | << context.realtime_remote_time << " (" |
| 42 | << context.monotonic_remote_time << ") " |
| 43 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
| 44 | << ": " |
| 45 | << aos::FlatbufferToJson( |
| 46 | channel->schema(), |
| 47 | static_cast<const uint8_t *>(context.data), |
| 48 | FLAGS_max_vector_size) |
| 49 | << std::endl; |
| 50 | } else { |
| 51 | std::cout << node_name << context.realtime_event_time << " (" |
| 52 | << context.monotonic_event_time << ") " |
| 53 | << channel->name()->c_str() << ' ' << channel->type()->c_str() |
| 54 | << ": " |
| 55 | << aos::FlatbufferToJson( |
| 56 | channel->schema(), |
| 57 | static_cast<const uint8_t *>(context.data), |
| 58 | FLAGS_max_vector_size) |
| 59 | << std::endl; |
| 60 | } |
| 61 | } |
| 62 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 63 | int main(int argc, char **argv) { |
| 64 | gflags::SetUsageMessage( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 65 | "Usage:\n" |
| 66 | " log_cat [args] logfile1 logfile2 ...\n" |
| 67 | "\n" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 68 | "This program provides a basic interface to dump data from a logfile to " |
| 69 | "stdout. Given a logfile, channel name filter, and type filter, it will " |
| 70 | "print all the messages in the logfile matching the filters. The message " |
| 71 | "filters work by taking the values of --name and --type and printing any " |
| 72 | "channel whose name contains --name as a substr and whose type contains " |
| 73 | "--type as a substr. Not specifying --name or --type leaves them free. " |
| 74 | "Calling this program without --name or --type specified prints out all " |
| 75 | "the logged data."); |
| 76 | aos::InitGoogle(&argc, &argv); |
| 77 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 78 | if (FLAGS_raw) { |
| 79 | if (argc != 2) { |
| 80 | LOG(FATAL) << "Expected 1 logfile as an argument."; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 81 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 82 | aos::logger::MessageReader reader(argv[1]); |
| 83 | |
| 84 | while (true) { |
| 85 | std::optional<aos::FlatbufferVector<aos::logger::MessageHeader>> message = |
| 86 | reader.ReadMessage(); |
| 87 | if (!message) { |
| 88 | break; |
| 89 | } |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 90 | const aos::Channel *channel = |
| 91 | reader.log_file_header()->configuration()->channels()->Get( |
| 92 | message.value().message().channel_index()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 93 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 94 | if (FLAGS_format_raw && message.value().message().data() != nullptr) { |
| 95 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
| 96 | << aos::FlatbufferToJson(message.value(), false, 4) << ": " |
| 97 | << aos::FlatbufferToJson( |
| 98 | channel->schema(), |
| 99 | message.value().message().data()->data(), |
| 100 | FLAGS_max_vector_size) |
| 101 | << std::endl; |
| 102 | } else { |
| 103 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
| 104 | << aos::FlatbufferToJson(message.value(), false, |
| 105 | FLAGS_max_vector_size) |
| 106 | << std::endl; |
| 107 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 108 | } |
| 109 | return 0; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 112 | 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 Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 123 | |
| 124 | aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration()); |
| 125 | reader.Register(&event_loop_factory); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 126 | |
| 127 | std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops; |
| 128 | |
| 129 | for (const aos::Node *node : reader.Nodes()) { |
| 130 | std::unique_ptr<aos::EventLoop> printer_event_loop = |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 131 | event_loop_factory.MakeEventLoop("printer", node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 132 | printer_event_loop->SkipTimingReport(); |
| 133 | printer_event_loop->SkipAosLog(); |
| 134 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 135 | struct MessageInfo { |
| 136 | std::string node_name; |
| 137 | std::unique_ptr<aos::RawFetcher> fetcher; |
| 138 | }; |
| 139 | std::vector<MessageInfo> messages_before_start; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 140 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 141 | bool found_channel = false; |
| 142 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 143 | printer_event_loop->configuration()->channels(); |
| 144 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 145 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 146 | const aos::Channel *channel = channels->Get(i); |
| 147 | const flatbuffers::string_view name = channel->name()->string_view(); |
| 148 | const flatbuffers::string_view type = channel->type()->string_view(); |
| 149 | if (name.find(FLAGS_name) != std::string::npos && |
| 150 | type.find(FLAGS_type) != std::string::npos) { |
| 151 | if (!aos::configuration::ChannelIsReadableOnNode( |
| 152 | channel, printer_event_loop->node())) { |
| 153 | continue; |
| 154 | } |
| 155 | VLOG(1) << "Listening on " << name << " " << type; |
| 156 | |
| 157 | std::string node_name = |
| 158 | node == nullptr ? "" |
| 159 | : std::string(node->name()->string_view()) + " "; |
| 160 | |
| 161 | CHECK_NOTNULL(channel->schema()); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 162 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 163 | // Fetch the last message on this channel from before the log start |
| 164 | // time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 165 | if (FLAGS_fetch) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 166 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 167 | printer_event_loop->MakeRawFetcher(channel); |
| 168 | if (fetcher->Fetch()) { |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 169 | MessageInfo message{.node_name = node_name, |
| 170 | .fetcher = std::move(fetcher)}; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 171 | // Insert it sorted into the vector so we can print in time order |
| 172 | // instead of channel order at the start. |
| 173 | auto it = std::lower_bound( |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 174 | messages_before_start.begin(), messages_before_start.end(), |
| 175 | message, [](const MessageInfo &lhs, const MessageInfo &rhs) { |
| 176 | if (lhs.fetcher->context().monotonic_event_time < |
| 177 | rhs.fetcher->context().monotonic_event_time) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 180 | if (lhs.fetcher->context().monotonic_event_time > |
| 181 | rhs.fetcher->context().monotonic_event_time) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 182 | return false; |
| 183 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 184 | return lhs.fetcher->channel() < rhs.fetcher->channel(); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 185 | }); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 186 | messages_before_start.insert(it, std::move(message)); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 190 | printer_event_loop->MakeRawWatcher( |
| 191 | channel, [channel, node_name](const aos::Context &context, |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 192 | const void * /*message*/) { |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 193 | PrintMessage(node_name, channel, context); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 194 | }); |
| 195 | found_channel = true; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (!found_channel) { |
| 200 | LOG(FATAL) << "Could not find any channels"; |
| 201 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 202 | |
| 203 | // Print the messages from before the log start time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 204 | // TODO(austin): Sort between nodes too when it becomes annoying enough. |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 205 | for (const MessageInfo &message : messages_before_start) { |
| 206 | PrintMessage(message.node_name, message.fetcher->channel(), |
| 207 | message.fetcher->context()); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 208 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 209 | printer_event_loops.emplace_back(std::move(printer_event_loop)); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 210 | |
| 211 | std::cout << std::endl; |
| 212 | std::cout << "Log starting at " << reader.realtime_start_time() << " (" |
| 213 | << reader.monotonic_start_time() << ")"; |
| 214 | std::cout << std::endl << std::endl; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 217 | if (FLAGS_fetch) { |
| 218 | // New line to separate fetched messages from non-fetched messages. |
| 219 | std::cout << std::endl; |
| 220 | } |
| 221 | |
| 222 | event_loop_factory.Run(); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 223 | |
| 224 | aos::Cleanup(); |
| 225 | return 0; |
| 226 | } |