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"); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 26 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 27 | aos::FastStringBuilder *builder) { |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 28 | // Print the flatbuffer out to stdout, both to remove the |
| 29 | // unnecessary cruft from glog and to allow the user to readily |
| 30 | // redirect just the logged output independent of any debugging |
| 31 | // information on stderr. |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 32 | |
| 33 | builder->Reset(); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 34 | aos::FlatbufferToJson( |
| 35 | builder, channel->schema(), static_cast<const uint8_t *>(context.data), |
| 36 | {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)}); |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 37 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 38 | if (FLAGS_print_timestamps) { |
| 39 | if (context.monotonic_remote_time != context.monotonic_event_time) { |
| 40 | std::cout << context.realtime_remote_time << " (" |
| 41 | << context.monotonic_remote_time << ") delivered " |
| 42 | << context.realtime_event_time << " (" |
| 43 | << context.monotonic_event_time << "): " << *builder << '\n'; |
| 44 | } else { |
| 45 | std::cout << context.realtime_event_time << " (" |
| 46 | << context.monotonic_event_time << "): " << *builder << '\n'; |
| 47 | } |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 48 | } else { |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 49 | std::cout << *builder << '\n'; |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | } // namespace |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 54 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 55 | int main(int argc, char **argv) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 56 | gflags::SetUsageMessage( |
| 57 | "Prints messages from arbitrary channels as they are received given a " |
| 58 | "configuration file describing the channels to listen on.\nTypical " |
| 59 | "Usage: aos_dump [--config path_to_config.json] channel_name " |
| 60 | "message_type\nExample Usage: aos_dump --config pingpong_config.json " |
| 61 | "/test aos.examples.Ping"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 62 | aos::InitGoogle(&argc, &argv); |
| 63 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 64 | aos::CliUtilInfo cli_info; |
| 65 | if (cli_info.Initialize(&argc, &argv, |
| 66 | [&cli_info](const aos::Channel *channel) { |
| 67 | return aos::configuration::ChannelIsReadableOnNode( |
| 68 | channel, cli_info.event_loop->node()); |
| 69 | })) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 73 | uint64_t message_count = 0; |
| 74 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 75 | aos::FastStringBuilder str_builder; |
| 76 | |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 77 | aos::monotonic_clock::time_point next_send_time = |
| 78 | aos::monotonic_clock::min_time; |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 79 | for (const aos::Channel *channel : cli_info.found_channels) { |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 80 | if (FLAGS_fetch) { |
| 81 | const std::unique_ptr<aos::RawFetcher> fetcher = |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 82 | cli_info.event_loop->MakeRawFetcher(channel); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 83 | if (fetcher->Fetch()) { |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 84 | PrintMessage(channel, fetcher->context(), &str_builder); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 85 | ++message_count; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 89 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 90 | return 0; |
| 91 | } |
| 92 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 93 | cli_info.event_loop->MakeRawWatcher( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 94 | channel, |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 95 | [channel, &str_builder, &cli_info, &message_count, &next_send_time]( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 96 | const aos::Context &context, const void * /*message*/) { |
| 97 | if (context.monotonic_event_time > next_send_time) { |
Austin Schuh | 58922e5 | 2021-04-10 16:03:25 -0700 | [diff] [blame^] | 98 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 99 | return; |
| 100 | } |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 101 | PrintMessage(channel, context, &str_builder); |
Austin Schuh | 08f27b4 | 2020-10-31 14:14:41 -0700 | [diff] [blame] | 102 | ++message_count; |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 103 | next_send_time = context.monotonic_event_time + |
| 104 | std::chrono::milliseconds(FLAGS_rate_limit); |
| 105 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 106 | cli_info.event_loop->Exit(); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 107 | } |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 108 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 109 | }); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 112 | cli_info.event_loop->Run(); |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 113 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 114 | return 0; |
| 115 | } |