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> |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 4 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 5 | #include "aos/aos_cli_utils.h" |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 6 | #include "aos/configuration.h" |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 7 | #include "aos/init.h" |
| 8 | #include "aos/json_to_flatbuffer.h" |
| 9 | #include "gflags/gflags.h" |
| 10 | |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 11 | DEFINE_int32(max_vector_size, 100, |
| 12 | "If positive, vectors longer than this will not be printed"); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 13 | DEFINE_bool(fetch, false, |
| 14 | "If true, fetch the current message on the channel first"); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 15 | DEFINE_bool(pretty, false, |
| 16 | "If true, pretty print the messages on multiple lines"); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 17 | DEFINE_bool(print_timestamps, true, "If true, timestamps are printed."); |
| 18 | DEFINE_uint64(count, 0, |
| 19 | "If >0, aos_dump will exit after printing this many messages."); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 20 | DEFINE_int32(rate_limit, 0, |
| 21 | "The minimum amount of time to wait in milliseconds before " |
| 22 | "sending another message"); |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 23 | DEFINE_int32(timeout, -1, |
| 24 | "The max time in milliseconds to wait for messages before " |
| 25 | "exiting. -1 means forever, 0 means don't wait."); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 29 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 30 | aos::FastStringBuilder *builder) { |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 31 | // Print the flatbuffer out to stdout, both to remove the |
| 32 | // unnecessary cruft from glog and to allow the user to readily |
| 33 | // redirect just the logged output independent of any debugging |
| 34 | // information on stderr. |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 35 | |
| 36 | builder->Reset(); |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 37 | |
| 38 | CHECK(flatbuffers::Verify(*channel->schema(), |
| 39 | *channel->schema()->root_table(), |
| 40 | static_cast<const uint8_t *>(context.data), |
| 41 | static_cast<size_t>(context.size))) |
| 42 | << ": Corrupted flatbuffer on " << channel->name()->c_str() << " " |
| 43 | << channel->type()->c_str(); |
| 44 | |
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)}); |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 48 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 49 | if (FLAGS_print_timestamps) { |
| 50 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 51 | std::cout << context.realtime_remote_time << " (" |
| 52 | << context.monotonic_remote_time << ") delivered " |
| 53 | << context.realtime_event_time << " (" |
| 54 | << context.monotonic_event_time << "): " << *builder << '\n'; |
| 55 | } else { |
| 56 | std::cout << context.realtime_event_time << " (" |
| 57 | << context.monotonic_event_time << "): " << *builder << '\n'; |
| 58 | } |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 59 | } else { |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 60 | std::cout << *builder << '\n'; |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | } // namespace |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 65 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 66 | int main(int argc, char **argv) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 67 | gflags::SetUsageMessage( |
| 68 | "Prints messages from arbitrary channels as they are received given a " |
| 69 | "configuration file describing the channels to listen on.\nTypical " |
| 70 | "Usage: aos_dump [--config path_to_config.json] channel_name " |
| 71 | "message_type\nExample Usage: aos_dump --config pingpong_config.json " |
| 72 | "/test aos.examples.Ping"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 73 | aos::InitGoogle(&argc, &argv); |
| 74 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 75 | aos::CliUtilInfo cli_info; |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 76 | if (cli_info.Initialize( |
| 77 | &argc, &argv, |
| 78 | [&cli_info](const aos::Channel *channel) { |
| 79 | return aos::configuration::ChannelIsReadableOnNode( |
| 80 | channel, cli_info.event_loop->node()); |
| 81 | }, |
| 82 | true)) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 86 | uint64_t message_count = 0; |
| 87 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 88 | aos::FastStringBuilder str_builder; |
| 89 | |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 90 | aos::monotonic_clock::time_point next_send_time = |
| 91 | aos::monotonic_clock::min_time; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 92 | for (const aos::Channel *channel : cli_info.found_channels) { |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 93 | if (FLAGS_fetch) { |
| 94 | const std::unique_ptr<aos::RawFetcher> fetcher = |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 95 | cli_info.event_loop->MakeRawFetcher(channel); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 96 | if (fetcher->Fetch()) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 97 | PrintMessage(channel, fetcher->context(), &str_builder); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 98 | ++message_count; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 102 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 103 | return 0; |
| 104 | } |
| 105 | |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 106 | if (FLAGS_timeout == 0) { |
| 107 | continue; |
| 108 | } |
| 109 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 110 | cli_info.event_loop->MakeRawWatcher( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 111 | channel, |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 112 | [channel, &str_builder, &cli_info, &message_count, &next_send_time]( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 113 | const aos::Context &context, const void * /*message*/) { |
| 114 | if (context.monotonic_event_time > next_send_time) { |
Austin Schuh | 58922e5 | 2021-04-10 16:03:25 -0700 | [diff] [blame] | 115 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 116 | return; |
| 117 | } |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 118 | PrintMessage(channel, context, &str_builder); |
Austin Schuh | 08f27b4 | 2020-10-31 14:14:41 -0700 | [diff] [blame] | 119 | ++message_count; |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 120 | next_send_time = context.monotonic_event_time + |
| 121 | std::chrono::milliseconds(FLAGS_rate_limit); |
| 122 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 123 | cli_info.event_loop->Exit(); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 124 | } |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 125 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 126 | }); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 129 | if (FLAGS_timeout == 0) { |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | if (FLAGS_timeout > 0) { |
| 134 | aos::TimerHandler *handle = cli_info.event_loop->AddTimer( |
| 135 | [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); }); |
| 136 | |
| 137 | cli_info.event_loop->OnRun( |
| 138 | [handle, event_loop = &cli_info.event_loop.value()]() { |
| 139 | handle->Setup(event_loop->monotonic_now() + |
| 140 | std::chrono::milliseconds(FLAGS_timeout)); |
| 141 | }); |
| 142 | } |
| 143 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 144 | cli_info.event_loop->Run(); |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 145 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 146 | return 0; |
| 147 | } |