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), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame^] | 48 | {false, static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 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), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame^] | 58 | {false, static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 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) << " " |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame^] | 96 | << aos::FlatbufferToJson( |
| 97 | message.value(), |
| 98 | {.multi_line = false, .max_vector_size = 4}) |
| 99 | << ": " |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 100 | << aos::FlatbufferToJson( |
| 101 | channel->schema(), |
| 102 | message.value().message().data()->data(), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame^] | 103 | {false, static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 104 | << std::endl; |
| 105 | } else { |
| 106 | std::cout << aos::configuration::StrippedChannelToString(channel) << " " |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame^] | 107 | << aos::FlatbufferToJson( |
| 108 | message.value(), |
| 109 | {false, static_cast<size_t>(FLAGS_max_vector_size)}) |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 110 | << std::endl; |
| 111 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 112 | } |
| 113 | return 0; |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 116 | if (argc < 2) { |
| 117 | LOG(FATAL) << "Expected at least 1 logfile as an argument."; |
| 118 | } |
| 119 | |
| 120 | std::vector<std::vector<std::string>> logfiles; |
| 121 | |
| 122 | for (int i = 1; i < argc; ++i) { |
| 123 | logfiles.emplace_back(std::vector<std::string>{std::string(argv[i])}); |
| 124 | } |
| 125 | |
| 126 | aos::logger::LogReader reader(logfiles); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 127 | |
| 128 | aos::SimulatedEventLoopFactory event_loop_factory(reader.configuration()); |
| 129 | reader.Register(&event_loop_factory); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 130 | |
| 131 | std::vector<std::unique_ptr<aos::EventLoop>> printer_event_loops; |
| 132 | |
| 133 | for (const aos::Node *node : reader.Nodes()) { |
| 134 | std::unique_ptr<aos::EventLoop> printer_event_loop = |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 135 | event_loop_factory.MakeEventLoop("printer", node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 136 | printer_event_loop->SkipTimingReport(); |
| 137 | printer_event_loop->SkipAosLog(); |
| 138 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 139 | struct MessageInfo { |
| 140 | std::string node_name; |
| 141 | std::unique_ptr<aos::RawFetcher> fetcher; |
| 142 | }; |
| 143 | std::vector<MessageInfo> messages_before_start; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 144 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 145 | bool found_channel = false; |
| 146 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 147 | printer_event_loop->configuration()->channels(); |
| 148 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 149 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 150 | const aos::Channel *channel = channels->Get(i); |
| 151 | const flatbuffers::string_view name = channel->name()->string_view(); |
| 152 | const flatbuffers::string_view type = channel->type()->string_view(); |
| 153 | if (name.find(FLAGS_name) != std::string::npos && |
| 154 | type.find(FLAGS_type) != std::string::npos) { |
| 155 | if (!aos::configuration::ChannelIsReadableOnNode( |
| 156 | channel, printer_event_loop->node())) { |
| 157 | continue; |
| 158 | } |
| 159 | VLOG(1) << "Listening on " << name << " " << type; |
| 160 | |
| 161 | std::string node_name = |
| 162 | node == nullptr ? "" |
| 163 | : std::string(node->name()->string_view()) + " "; |
| 164 | |
| 165 | CHECK_NOTNULL(channel->schema()); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 166 | |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 167 | // Fetch the last message on this channel from before the log start |
| 168 | // time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 169 | if (FLAGS_fetch) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 170 | std::unique_ptr<aos::RawFetcher> fetcher = |
| 171 | printer_event_loop->MakeRawFetcher(channel); |
| 172 | if (fetcher->Fetch()) { |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 173 | MessageInfo message{.node_name = node_name, |
| 174 | .fetcher = std::move(fetcher)}; |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 175 | // Insert it sorted into the vector so we can print in time order |
| 176 | // instead of channel order at the start. |
| 177 | auto it = std::lower_bound( |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 178 | messages_before_start.begin(), messages_before_start.end(), |
| 179 | message, [](const MessageInfo &lhs, const MessageInfo &rhs) { |
| 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 true; |
| 183 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 184 | if (lhs.fetcher->context().monotonic_event_time > |
| 185 | rhs.fetcher->context().monotonic_event_time) { |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 186 | return false; |
| 187 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 188 | return lhs.fetcher->channel() < rhs.fetcher->channel(); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 189 | }); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 190 | messages_before_start.insert(it, std::move(message)); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 194 | printer_event_loop->MakeRawWatcher( |
| 195 | channel, [channel, node_name](const aos::Context &context, |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 196 | const void * /*message*/) { |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 197 | PrintMessage(node_name, channel, context); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 198 | }); |
| 199 | found_channel = true; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (!found_channel) { |
| 204 | LOG(FATAL) << "Could not find any channels"; |
| 205 | } |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 206 | |
| 207 | // Print the messages from before the log start time. |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 208 | // TODO(austin): Sort between nodes too when it becomes annoying enough. |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 209 | for (const MessageInfo &message : messages_before_start) { |
| 210 | PrintMessage(message.node_name, message.fetcher->channel(), |
| 211 | message.fetcher->context()); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 212 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 213 | printer_event_loops.emplace_back(std::move(printer_event_loop)); |
Brian Silverman | 9891b29 | 2020-06-23 16:34:22 -0700 | [diff] [blame] | 214 | |
| 215 | std::cout << std::endl; |
| 216 | std::cout << "Log starting at " << reader.realtime_start_time() << " (" |
| 217 | << reader.monotonic_start_time() << ")"; |
| 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 | |
| 228 | aos::Cleanup(); |
| 229 | return 0; |
| 230 | } |