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