blob: ecf4add8a40339889f714184eb3befe75eaad5b1 [file] [log] [blame]
Tyler Chatowe6f5bef2020-10-31 14:22:04 -07001#include <unistd.h>
2
Tyler Chatow5e369a42019-11-23 11:57:31 -08003#include <iostream>
Tyler Chatow5e369a42019-11-23 11:57:31 -08004
Austin Schuh99f7c6a2024-06-25 22:07:44 -07005#include "absl/flags/flag.h"
6#include "absl/flags/usage.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007
Brian Silvermanea2c95f2021-02-10 18:10:26 -08008#include "aos/aos_cli_utils.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08009#include "aos/configuration.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080010#include "aos/init.h"
11#include "aos/json_to_flatbuffer.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080012
Austin Schuh99f7c6a2024-06-25 22:07:44 -070013ABSL_FLAG(int64_t, max_vector_size, 100,
14 "If positive, vectors longer than this will not be printed");
15ABSL_FLAG(bool, json, false, "If true, print fully valid JSON");
16ABSL_FLAG(bool, fetch, false,
17 "If true, fetch the current message on the channel first");
18ABSL_FLAG(bool, pretty, false,
19 "If true, pretty print the messages on multiple lines");
20ABSL_FLAG(
21 bool, pretty_max, false,
Dan Ford44e02512022-06-07 23:45:14 -070022 "If true, expand every field to its own line (expands more than -pretty)");
Austin Schuh99f7c6a2024-06-25 22:07:44 -070023ABSL_FLAG(bool, print_timestamps, true, "If true, timestamps are printed.");
24ABSL_FLAG(uint64_t, count, 0,
25 "If >0, aos_dump will exit after printing this many messages.");
26ABSL_FLAG(int32_t, rate_limit, 0,
27 "The minimum amount of time to wait in milliseconds before "
28 "sending another message");
29ABSL_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.");
32ABSL_FLAG(bool, hex, false,
33 "Are integers in the messages printed in hex notation.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070034
Tyler Chatow5e369a42019-11-23 11:57:31 -080035int main(int argc, char **argv) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070036 absl::SetProgramUsageMessage(
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070037 "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 Chatow5e369a42019-11-23 11:57:31 -080042 aos::InitGoogle(&argc, &argv);
43
Brian Silvermanea2c95f2021-02-10 18:10:26 -080044 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070045 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 Schuhba2c8652022-08-17 14:56:08 -070051 "channel is readeable on node", true)) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070052 return 0;
53 }
54
Austin Schuh01d88fc2020-09-13 18:48:16 -070055 aos::monotonic_clock::time_point next_send_time =
56 aos::monotonic_clock::min_time;
Austin Schuh893d7f42022-09-16 15:01:35 -070057
Naman Gupta54047212022-09-23 14:10:30 -070058 aos::Printer printer(
59 {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070060 .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 Gupta54047212022-09-23 14:10:30 -070066 .distributed_clock = false,
Austin Schuh99f7c6a2024-06-25 22:07:44 -070067 .hex = absl::GetFlag(FLAGS_hex),
Naman Gupta54047212022-09-23 14:10:30 -070068 },
69 /*flush*/ true);
70
Brian Silvermanea2c95f2021-02-10 18:10:26 -080071 for (const aos::Channel *channel : cli_info.found_channels) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070072 if (absl::GetFlag(FLAGS_fetch)) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070073 const std::unique_ptr<aos::RawFetcher> fetcher =
Brian Silvermanea2c95f2021-02-10 18:10:26 -080074 cli_info.event_loop->MakeRawFetcher(channel);
Brian Silverman83ff9c12020-06-23 16:20:27 -070075 if (fetcher->Fetch()) {
Naman Gupta54047212022-09-23 14:10:30 -070076 printer.PrintMessage(channel, fetcher->context());
Brian Silverman83ff9c12020-06-23 16:20:27 -070077 }
78 }
79
Austin Schuh99f7c6a2024-06-25 22:07:44 -070080 if (absl::GetFlag(FLAGS_count) > 0 &&
81 printer.message_count() >= absl::GetFlag(FLAGS_count)) {
Austin Schuh594eef82020-09-10 18:43:01 -070082 return 0;
83 }
84
Austin Schuh99f7c6a2024-06-25 22:07:44 -070085 if (absl::GetFlag(FLAGS_timeout) == 0) {
Austin Schuhe787c1d2022-06-23 10:14:30 -070086 continue;
87 }
88
Brian Silvermanea2c95f2021-02-10 18:10:26 -080089 cli_info.event_loop->MakeRawWatcher(
Naman Gupta54047212022-09-23 14:10:30 -070090 channel, [channel, &printer, &cli_info, &next_send_time](
91 const aos::Context &context, const void * /*message*/) {
Austin Schuh01d88fc2020-09-13 18:48:16 -070092 if (context.monotonic_event_time > next_send_time) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070093 if (absl::GetFlag(FLAGS_count) > 0 &&
94 printer.message_count() >= absl::GetFlag(FLAGS_count)) {
Austin Schuh58922e52021-04-10 16:03:25 -070095 return;
96 }
Naman Gupta54047212022-09-23 14:10:30 -070097
98 printer.PrintMessage(channel, context);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070099 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 Silvermanea2c95f2021-02-10 18:10:26 -0800104 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -0700105 }
Austin Schuh594eef82020-09-10 18:43:01 -0700106 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700107 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700108 }
109
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700110 if (absl::GetFlag(FLAGS_timeout) == 0) {
Austin Schuhe787c1d2022-06-23 10:14:30 -0700111 return 0;
112 }
113
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700114 if (absl::GetFlag(FLAGS_timeout) > 0) {
Austin Schuhe787c1d2022-06-23 10:14:30 -0700115 aos::TimerHandler *handle = cli_info.event_loop->AddTimer(
116 [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); });
117
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700118 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 Schuhe787c1d2022-06-23 10:14:30 -0700123 }
124
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800125 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700126
Tyler Chatow5e369a42019-11-23 11:57:31 -0800127 return 0;
128}