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, |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 37 | 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 Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 44 | 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 Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 50 | << ": " << *builder << std::endl; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 51 | } 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 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 | } |
| 57 | } |
| 58 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 59 | int main(int argc, char **argv) { |
| 60 | gflags::SetUsageMessage( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 61 | "Usage:\n" |
| 62 | " log_cat [args] logfile1 logfile2 ...\n" |
| 63 | "\n" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 64 | "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 Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 74 | if (FLAGS_raw) { |
| 75 | if (argc != 2) { |
| 76 | LOG(FATAL) << "Expected 1 logfile as an argument."; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 77 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 78 | 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 Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 86 | const aos::Channel *channel = |
| 87 | reader.log_file_header()->configuration()->channels()->Get( |
| 88 | message.value().message().channel_index()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 89 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 90 | if (FLAGS_format_raw && message.value().message().data() != nullptr) { |
| 91 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 92 | << aos::FlatbufferToJson( |
| 93 | message.value(), |
| 94 | {.multi_line = false, .max_vector_size = 4}) |
| 95 | << ": " |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 96 | << aos::FlatbufferToJson( |
| 97 | channel->schema(), |
| 98 | message.value().message().data()->data(), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 99 | {false, static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 100 | << std::endl; |
| 101 | } else { |
| 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(), |
| 105 | {false, static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 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 | |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 124 | aos::FastStringBuilder builder; |
| 125 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 126 | aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration()); |
| 127 | reader.Register(&event_loop_factory); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 128 | |
| 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 Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 133 | event_loop_factory.MakeEventLoop("printer", node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 134 | printer_event_loop->SkipTimingReport(); |
| 135 | printer_event_loop->SkipAosLog(); |
| 136 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 137 | struct MessageInfo { |
| 138 | std::string node_name; |
| 139 | std::unique_ptr<aos::RawFetcher> fetcher; |
| 140 | }; |
| 141 | std::vector<MessageInfo> messages_before_start; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 142 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 143 | bool found_channel = false; |
| 144 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 145 | printer_event_loop->configuration()->channels(); |
| 146 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 147 | 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 Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 164 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 165 | // Fetch the last message on this channel from before the log start |
| 166 | // time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 167 | if (FLAGS_fetch) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 168 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 169 | printer_event_loop->MakeRawFetcher(channel); |
| 170 | if (fetcher->Fetch()) { |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 171 | MessageInfo message{.node_name = node_name, |
| 172 | .fetcher = std::move(fetcher)}; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 173 | // 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 Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 176 | 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 Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 182 | if (lhs.fetcher->context().monotonic_event_time > |
| 183 | rhs.fetcher->context().monotonic_event_time) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 186 | return lhs.fetcher->channel() < rhs.fetcher->channel(); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 187 | }); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 188 | messages_before_start.insert(it, std::move(message)); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 192 | printer_event_loop->MakeRawWatcher( |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 193 | channel, |
| 194 | [channel, node_name, &builder](const aos::Context &context, |
| 195 | const void * /*message*/) { |
| 196 | PrintMessage(node_name, channel, context, &builder); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 197 | }); |
| 198 | found_channel = true; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (!found_channel) { |
| 203 | LOG(FATAL) << "Could not find any channels"; |
| 204 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 205 | |
| 206 | // Print the messages from before the log start time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 207 | // TODO(austin): Sort between nodes too when it becomes annoying enough. |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 208 | for (const MessageInfo &message : messages_before_start) { |
| 209 | PrintMessage(message.node_name, message.fetcher->channel(), |
Austin Schuh | 746690f | 2020-08-01 16:15:57 -0700 | [diff] [blame] | 210 | message.fetcher->context(), &builder); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 211 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 212 | printer_event_loops.emplace_back(std::move(printer_event_loop)); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 213 | |
| 214 | std::cout << std::endl; |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 215 | std::cout << (node != nullptr ? (node->name()->str() + " ") : "") |
| 216 | << "Log starting at " << reader.realtime_start_time(node) << " (" |
| 217 | << reader.monotonic_start_time(node) << ")"; |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 218 | std::cout << std::endl << std::endl; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 221 | if (FLAGS_fetch) { |
| 222 | // New line to separate fetched messages from non-fetched messages. |
| 223 | std::cout << std::endl; |
| 224 | } |
| 225 | |
| 226 | event_loop_factory.Run(); |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 227 | |
Austin Schuh | 51a9259 | 2020-08-09 13:17:00 -0700 | [diff] [blame] | 228 | reader.Deregister(); |
| 229 | |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 230 | return 0; |
| 231 | } |