Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 3 | #include <iostream> |
| 4 | #include <map> |
| 5 | |
| 6 | #include "aos/configuration.h" |
| 7 | #include "aos/events/shm_event_loop.h" |
| 8 | #include "aos/init.h" |
| 9 | #include "aos/json_to_flatbuffer.h" |
| 10 | #include "gflags/gflags.h" |
| 11 | |
| 12 | DEFINE_string(config, "./config.json", "File path of aos configuration"); |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 13 | DEFINE_int32(max_vector_size, 100, |
| 14 | "If positive, vectors longer than this will not be printed"); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 15 | DEFINE_bool(fetch, false, |
| 16 | "If true, fetch the current message on the channel first"); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 17 | DEFINE_bool(pretty, false, |
| 18 | "If true, pretty print the messages on multiple lines"); |
Brian Silverman | 42402e5 | 2020-09-22 22:21:13 -0700 | [diff] [blame] | 19 | DEFINE_bool(all, false, |
| 20 | "If true, print out the channels for all nodes, not just the " |
| 21 | "channels which are visible on this node."); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 22 | DEFINE_bool(print_timestamps, true, "If true, timestamps are printed."); |
| 23 | DEFINE_uint64(count, 0, |
| 24 | "If >0, aos_dump will exit after printing this many messages."); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 25 | DEFINE_int32(rate_limit, 0, |
| 26 | "The minimum amount of time to wait in milliseconds before " |
| 27 | "sending another message"); |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 28 | DEFINE_bool( |
| 29 | _bash_autocomplete, false, |
| 30 | "Internal use: Outputs channel list for use with autocomplete script."); |
| 31 | DEFINE_string(_bash_autocomplete_word, "", |
Tyler Chatow | b8fa290 | 2020-10-31 14:58:58 -0700 | [diff] [blame^] | 32 | "Intenal use: Current word being autocompleted"); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 36 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 37 | aos::FastStringBuilder *builder) { |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 38 | // Print the flatbuffer out to stdout, both to remove the |
| 39 | // unnecessary cruft from glog and to allow the user to readily |
| 40 | // redirect just the logged output independent of any debugging |
| 41 | // information on stderr. |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 42 | |
| 43 | builder->Reset(); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 44 | aos::FlatbufferToJson( |
| 45 | builder, channel->schema(), static_cast<const uint8_t *>(context.data), |
| 46 | {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)}); |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 47 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 48 | if (FLAGS_print_timestamps) { |
| 49 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 50 | std::cout << context.realtime_remote_time << " (" |
| 51 | << context.monotonic_remote_time << ") delivered " |
| 52 | << context.realtime_event_time << " (" |
| 53 | << context.monotonic_event_time << "): " << *builder << '\n'; |
| 54 | } else { |
| 55 | std::cout << context.realtime_event_time << " (" |
| 56 | << context.monotonic_event_time << "): " << *builder << '\n'; |
| 57 | } |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 58 | } else { |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 59 | std::cout << *builder << '\n'; |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 63 | // Generate eval command to populate autocomplete responses. Eval escapes spaces |
| 64 | // so channels are paired with their types. If a complete channel name is found, |
| 65 | // only autocompletes the type to avoid repeating arguments. Returns no |
| 66 | // autocomplete suggestions if a channel and type is found with the current |
| 67 | // arguments. |
| 68 | void Autocomplete(const aos::Configuration *config_msg, |
| 69 | const aos::ShmEventLoop &event_loop, |
| 70 | std::string_view channel_name, |
| 71 | std::string_view message_type) { |
| 72 | const bool unique_match = |
Tyler Chatow | b8fa290 | 2020-10-31 14:58:58 -0700 | [diff] [blame^] | 73 | std::count_if(config_msg->channels()->begin(), |
| 74 | config_msg->channels()->end(), |
| 75 | [channel_name, message_type](const aos::Channel *channel) { |
| 76 | return channel->name()->string_view() == channel_name && |
| 77 | channel->type()->string_view() == message_type; |
| 78 | }) == 1; |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 79 | |
Tyler Chatow | b8fa290 | 2020-10-31 14:58:58 -0700 | [diff] [blame^] | 80 | const bool editing_message = |
| 81 | !channel_name.empty() && FLAGS__bash_autocomplete_word == message_type; |
| 82 | const bool editing_channel = |
| 83 | !editing_message && FLAGS__bash_autocomplete_word == channel_name; |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 84 | |
| 85 | std::cout << "COMPREPLY=("; |
| 86 | |
| 87 | // If we have a unique match, don't provide any suggestions. Otherwise, check |
| 88 | // that were're editing one of the two positional arguments. |
| 89 | if (!unique_match && (editing_message || editing_channel)) { |
| 90 | for (const aos::Channel *channel : *config_msg->channels()) { |
| 91 | if (FLAGS_all || aos::configuration::ChannelIsReadableOnNode( |
| 92 | channel, event_loop.node())) { |
| 93 | // Suggest only message types if the message type argument is being |
| 94 | // entered. |
| 95 | if (editing_message) { |
| 96 | // Then, filter for only channel names that match exactly and types |
| 97 | // that begin with message_type. |
| 98 | if (channel->name()->string_view() == channel_name && |
| 99 | channel->type()->string_view().find(message_type) == 0) { |
| 100 | std::cout << '\'' << channel->type()->c_str() << "' "; |
| 101 | } |
| 102 | } else if (channel->name()->string_view().find(channel_name) == 0) { |
| 103 | // If the message type empty, then return full autocomplete. |
| 104 | // Otherwise, since the message type is poulated yet not being edited, |
| 105 | // the user must be editing the channel name alone, in which case only |
| 106 | // suggest channel names, not pairs. |
| 107 | if (message_type.empty()) { |
| 108 | std::cout << '\'' << channel->name()->c_str() << ' ' |
| 109 | << channel->type()->c_str() << "' "; |
| 110 | } else { |
| 111 | std::cout << '\'' << channel->name()->c_str() << "' "; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | std::cout << ')'; |
| 118 | } |
| 119 | |
| 120 | bool EndsWith(std::string_view str, std::string_view ending) { |
| 121 | const std::size_t offset = str.size() - ending.size(); |
Tyler Chatow | b8fa290 | 2020-10-31 14:58:58 -0700 | [diff] [blame^] | 122 | return str.size() >= ending.size() && |
| 123 | std::equal(str.begin() + offset, str.end(), ending.begin(), |
| 124 | ending.end()); |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 127 | } // namespace |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 128 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 129 | int main(int argc, char **argv) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 130 | gflags::SetUsageMessage( |
| 131 | "Prints messages from arbitrary channels as they are received given a " |
| 132 | "configuration file describing the channels to listen on.\nTypical " |
| 133 | "Usage: aos_dump [--config path_to_config.json] channel_name " |
| 134 | "message_type\nExample Usage: aos_dump --config pingpong_config.json " |
| 135 | "/test aos.examples.Ping"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 136 | aos::InitGoogle(&argc, &argv); |
| 137 | |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 138 | // Don't generate failure output if the config doesn't exist while attempting |
| 139 | // to autocomplete. |
| 140 | if (struct stat file_stat; |
| 141 | FLAGS__bash_autocomplete && |
| 142 | (!(EndsWith(FLAGS_config, ".json") || EndsWith(FLAGS_config, ".bfbs")) || |
| 143 | stat(FLAGS_config.c_str(), &file_stat) != 0 || |
| 144 | (file_stat.st_mode & S_IFMT) != S_IFREG)) { |
| 145 | std::cout << "COMPREPLY=()"; |
| 146 | return 0; |
| 147 | } |
| 148 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 149 | std::string channel_name; |
| 150 | std::string message_type; |
| 151 | if (argc > 1) { |
| 152 | channel_name = argv[1]; |
| 153 | } |
| 154 | if (argc > 2) { |
| 155 | message_type = argv[2]; |
| 156 | } |
| 157 | |
| 158 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 159 | aos::configuration::ReadConfig(FLAGS_config); |
| 160 | |
| 161 | const aos::Configuration *config_msg = &config.message(); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 162 | aos::ShmEventLoop event_loop(config_msg); |
James Kuszmaul | 5c22e08 | 2019-12-14 20:43:07 -0800 | [diff] [blame] | 163 | event_loop.SkipTimingReport(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 164 | event_loop.SkipAosLog(); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 165 | |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 166 | if (FLAGS__bash_autocomplete) { |
| 167 | Autocomplete(config_msg, event_loop, channel_name, message_type); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 171 | if (argc == 1) { |
| 172 | std::cout << "Channels:\n"; |
| 173 | for (const aos::Channel *channel : *config_msg->channels()) { |
Brian Silverman | 42402e5 | 2020-09-22 22:21:13 -0700 | [diff] [blame] | 174 | if (FLAGS_all || aos::configuration::ChannelIsReadableOnNode( |
| 175 | channel, event_loop.node())) { |
| 176 | std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str() |
| 177 | << '\n'; |
| 178 | } |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 183 | std::vector<const aos::Channel *> found_channels; |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 184 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 185 | config_msg->channels(); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 186 | bool found_exact = false; |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 187 | for (const aos::Channel *channel : *channels) { |
Brian Silverman | 42402e5 | 2020-09-22 22:21:13 -0700 | [diff] [blame] | 188 | if (!FLAGS_all && !aos::configuration::ChannelIsReadableOnNode( |
| 189 | channel, event_loop.node())) { |
| 190 | continue; |
| 191 | } |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 192 | if (channel->name()->c_str() != channel_name) { |
| 193 | continue; |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 194 | } |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 195 | if (channel->type()->string_view() == message_type) { |
| 196 | if (!found_exact) { |
| 197 | found_channels.clear(); |
| 198 | found_exact = true; |
| 199 | } |
| 200 | } else if (!found_exact && channel->type()->string_view().find( |
| 201 | message_type) != std::string_view::npos) { |
| 202 | } else { |
| 203 | continue; |
| 204 | } |
| 205 | found_channels.push_back(channel); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 208 | if (found_channels.empty()) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 209 | LOG(FATAL) << "Could not find any channels with the given name and type."; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 210 | } else if (found_channels.size() > 1 && !message_type.empty()) { |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 211 | LOG(FATAL) << "Multiple channels found with same type"; |
| 212 | } |
| 213 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 214 | uint64_t message_count = 0; |
| 215 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 216 | aos::FastStringBuilder str_builder; |
| 217 | |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 218 | aos::monotonic_clock::time_point next_send_time = |
| 219 | aos::monotonic_clock::min_time; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 220 | for (const aos::Channel *channel : found_channels) { |
| 221 | if (FLAGS_fetch) { |
| 222 | const std::unique_ptr<aos::RawFetcher> fetcher = |
| 223 | event_loop.MakeRawFetcher(channel); |
| 224 | if (fetcher->Fetch()) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 225 | PrintMessage(channel, fetcher->context(), &str_builder); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 226 | ++message_count; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 230 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 231 | return 0; |
| 232 | } |
| 233 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 234 | event_loop.MakeRawWatcher( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 235 | channel, |
| 236 | [channel, &str_builder, &event_loop, &message_count, &next_send_time]( |
| 237 | const aos::Context &context, const void * /*message*/) { |
| 238 | if (context.monotonic_event_time > next_send_time) { |
| 239 | PrintMessage(channel, context, &str_builder); |
| 240 | next_send_time = context.monotonic_event_time + |
| 241 | std::chrono::milliseconds(FLAGS_rate_limit); |
| 242 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 243 | event_loop.Exit(); |
| 244 | } |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 245 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 246 | }); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 249 | event_loop.Run(); |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 250 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 251 | return 0; |
| 252 | } |