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 | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame^] | 11 | DEFINE_int64(max_vector_size, 100, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 12 | "If positive, vectors longer than this will not be printed"); |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame^] | 13 | DEFINE_bool(json, false, "If true, print fully valid JSON"); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 14 | DEFINE_bool(fetch, false, |
| 15 | "If true, fetch the current message on the channel first"); |
Ravago Jones | 5cc9df5 | 2020-09-02 21:29:58 -0700 | [diff] [blame] | 16 | DEFINE_bool(pretty, false, |
| 17 | "If true, pretty print the messages on multiple lines"); |
Dan Ford | 44e0251 | 2022-06-07 23:45:14 -0700 | [diff] [blame] | 18 | DEFINE_bool( |
| 19 | pretty_max, false, |
| 20 | "If true, expand every field to its own line (expands more than -pretty)"); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 21 | DEFINE_bool(print_timestamps, true, "If true, timestamps are printed."); |
| 22 | DEFINE_uint64(count, 0, |
| 23 | "If >0, aos_dump will exit after printing this many messages."); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 24 | DEFINE_int32(rate_limit, 0, |
| 25 | "The minimum amount of time to wait in milliseconds before " |
| 26 | "sending another message"); |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 27 | DEFINE_int32(timeout, -1, |
| 28 | "The max time in milliseconds to wait for messages before " |
| 29 | "exiting. -1 means forever, 0 means don't wait."); |
Brian J Griglak | 043e0e2 | 2022-08-18 12:51:18 -0600 | [diff] [blame] | 30 | DEFINE_bool(use_hex, false, "Are integers in the messages printed in hex notation."); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 31 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 32 | int main(int argc, char **argv) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 33 | gflags::SetUsageMessage( |
| 34 | "Prints messages from arbitrary channels as they are received given a " |
| 35 | "configuration file describing the channels to listen on.\nTypical " |
| 36 | "Usage: aos_dump [--config path_to_config.json] channel_name " |
| 37 | "message_type\nExample Usage: aos_dump --config pingpong_config.json " |
| 38 | "/test aos.examples.Ping"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 39 | aos::InitGoogle(&argc, &argv); |
| 40 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 41 | aos::CliUtilInfo cli_info; |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 42 | if (cli_info.Initialize( |
| 43 | &argc, &argv, |
| 44 | [&cli_info](const aos::Channel *channel) { |
| 45 | return aos::configuration::ChannelIsReadableOnNode( |
| 46 | channel, cli_info.event_loop->node()); |
| 47 | }, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 48 | "channel is readeable on node", true)) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 52 | uint64_t message_count = 0; |
| 53 | |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 54 | aos::FastStringBuilder str_builder; |
| 55 | |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 56 | aos::monotonic_clock::time_point next_send_time = |
| 57 | aos::monotonic_clock::min_time; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame^] | 58 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 59 | for (const aos::Channel *channel : cli_info.found_channels) { |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 60 | if (FLAGS_fetch) { |
| 61 | const std::unique_ptr<aos::RawFetcher> fetcher = |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 62 | cli_info.event_loop->MakeRawFetcher(channel); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 63 | if (fetcher->Fetch()) { |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame^] | 64 | PrintMessage( |
| 65 | channel, fetcher->context(), &str_builder, |
| 66 | { |
| 67 | .pretty = FLAGS_pretty, |
| 68 | .max_vector_size = static_cast<size_t>(FLAGS_max_vector_size), |
| 69 | .pretty_max = FLAGS_pretty_max, |
| 70 | .print_timestamps = FLAGS_print_timestamps, |
| 71 | .json = FLAGS_json, |
| 72 | .distributed_clock = false, |
| 73 | .use_hex = FLAGS_use_hex, |
| 74 | }); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 75 | ++message_count; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 79 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 83 | if (FLAGS_timeout == 0) { |
| 84 | continue; |
| 85 | } |
| 86 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 87 | cli_info.event_loop->MakeRawWatcher( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 88 | channel, |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 89 | [channel, &str_builder, &cli_info, &message_count, &next_send_time]( |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 90 | const aos::Context &context, const void * /*message*/) { |
| 91 | if (context.monotonic_event_time > next_send_time) { |
Austin Schuh | 58922e5 | 2021-04-10 16:03:25 -0700 | [diff] [blame] | 92 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
| 93 | return; |
| 94 | } |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame^] | 95 | PrintMessage(channel, context, &str_builder, |
| 96 | { |
| 97 | .pretty = FLAGS_pretty, |
| 98 | .max_vector_size = |
| 99 | static_cast<size_t>(FLAGS_max_vector_size), |
| 100 | .pretty_max = FLAGS_pretty_max, |
| 101 | .print_timestamps = FLAGS_print_timestamps, |
| 102 | .json = FLAGS_json, |
| 103 | .distributed_clock = false, |
| 104 | .use_hex = FLAGS_use_hex, |
| 105 | }); |
Austin Schuh | 08f27b4 | 2020-10-31 14:14:41 -0700 | [diff] [blame] | 106 | ++message_count; |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 107 | next_send_time = context.monotonic_event_time + |
| 108 | std::chrono::milliseconds(FLAGS_rate_limit); |
| 109 | if (FLAGS_count > 0 && message_count >= FLAGS_count) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 110 | cli_info.event_loop->Exit(); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 111 | } |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 112 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 113 | }); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 116 | if (FLAGS_timeout == 0) { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | if (FLAGS_timeout > 0) { |
| 121 | aos::TimerHandler *handle = cli_info.event_loop->AddTimer( |
| 122 | [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); }); |
| 123 | |
| 124 | cli_info.event_loop->OnRun( |
| 125 | [handle, event_loop = &cli_info.event_loop.value()]() { |
| 126 | handle->Setup(event_loop->monotonic_now() + |
| 127 | std::chrono::milliseconds(FLAGS_timeout)); |
| 128 | }); |
| 129 | } |
| 130 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 131 | cli_info.event_loop->Run(); |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 132 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 133 | return 0; |
| 134 | } |