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 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 5 | #include "gflags/gflags.h" |
| 6 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 7 | #include "aos/aos_cli_utils.h" |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 8 | #include "aos/configuration.h" |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 9 | #include "aos/init.h" |
| 10 | #include "aos/json_to_flatbuffer.h" |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 12 | DEFINE_int64(max_vector_size, 100, |
Austin Schuh | d393620 | 2020-04-07 20:11:07 -0700 | [diff] [blame] | 13 | "If positive, vectors longer than this will not be printed"); |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 14 | DEFINE_bool(json, false, "If true, print fully valid JSON"); |
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"); |
Dan Ford | 44e0251 | 2022-06-07 23:45:14 -0700 | [diff] [blame] | 19 | DEFINE_bool( |
| 20 | pretty_max, false, |
| 21 | "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] | 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"); |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 28 | DEFINE_int32(timeout, -1, |
| 29 | "The max time in milliseconds to wait for messages before " |
| 30 | "exiting. -1 means forever, 0 means don't wait."); |
Austin Schuh | be6d296 | 2022-11-01 09:24:56 -0700 | [diff] [blame] | 31 | DEFINE_bool(hex, false, |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 32 | "Are integers in the messages printed in hex notation."); |
Brian Silverman | 1bc2e96 | 2020-04-28 15:22:01 -0700 | [diff] [blame] | 33 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 34 | int main(int argc, char **argv) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 35 | gflags::SetUsageMessage( |
| 36 | "Prints messages from arbitrary channels as they are received given a " |
| 37 | "configuration file describing the channels to listen on.\nTypical " |
| 38 | "Usage: aos_dump [--config path_to_config.json] channel_name " |
| 39 | "message_type\nExample Usage: aos_dump --config pingpong_config.json " |
| 40 | "/test aos.examples.Ping"); |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 41 | aos::InitGoogle(&argc, &argv); |
| 42 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 43 | aos::CliUtilInfo cli_info; |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 44 | if (cli_info.Initialize( |
| 45 | &argc, &argv, |
| 46 | [&cli_info](const aos::Channel *channel) { |
| 47 | return aos::configuration::ChannelIsReadableOnNode( |
| 48 | channel, cli_info.event_loop->node()); |
| 49 | }, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 50 | "channel is readeable on node", true)) { |
Tyler Chatow | e6f5bef | 2020-10-31 14:22:04 -0700 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 54 | uint64_t message_count = 0; |
| 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 | |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 59 | aos::Printer printer( |
| 60 | { |
| 61 | .pretty = FLAGS_pretty, |
| 62 | .max_vector_size = static_cast<size_t>(FLAGS_max_vector_size), |
| 63 | .pretty_max = FLAGS_pretty_max, |
| 64 | .print_timestamps = FLAGS_print_timestamps, |
| 65 | .json = FLAGS_json, |
| 66 | .distributed_clock = false, |
Austin Schuh | be6d296 | 2022-11-01 09:24:56 -0700 | [diff] [blame] | 67 | .hex = 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) { |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 72 | if (FLAGS_fetch) { |
| 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()); |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 77 | ++message_count; |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 81 | if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) { |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 85 | if (FLAGS_timeout == 0) { |
| 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) { |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 93 | if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) { |
Austin Schuh | 58922e5 | 2021-04-10 16:03:25 -0700 | [diff] [blame] | 94 | return; |
| 95 | } |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 96 | |
| 97 | printer.PrintMessage(channel, context); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 98 | next_send_time = context.monotonic_event_time + |
| 99 | std::chrono::milliseconds(FLAGS_rate_limit); |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 100 | if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) { |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 101 | cli_info.event_loop->Exit(); |
Austin Schuh | 01d88fc | 2020-09-13 18:48:16 -0700 | [diff] [blame] | 102 | } |
Austin Schuh | 594eef8 | 2020-09-10 18:43:01 -0700 | [diff] [blame] | 103 | } |
Tyler Chatow | fcf16f4 | 2020-07-26 12:41:36 -0700 | [diff] [blame] | 104 | }); |
Brian Silverman | 83ff9c1 | 2020-06-23 16:20:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Austin Schuh | e787c1d | 2022-06-23 10:14:30 -0700 | [diff] [blame] | 107 | if (FLAGS_timeout == 0) { |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | if (FLAGS_timeout > 0) { |
| 112 | aos::TimerHandler *handle = cli_info.event_loop->AddTimer( |
| 113 | [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); }); |
| 114 | |
| 115 | cli_info.event_loop->OnRun( |
| 116 | [handle, event_loop = &cli_info.event_loop.value()]() { |
| 117 | handle->Setup(event_loop->monotonic_now() + |
| 118 | std::chrono::milliseconds(FLAGS_timeout)); |
| 119 | }); |
| 120 | } |
| 121 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 122 | cli_info.event_loop->Run(); |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 123 | |
Tyler Chatow | 5e369a4 | 2019-11-23 11:57:31 -0800 | [diff] [blame] | 124 | return 0; |
| 125 | } |