blob: 9f3d01fd14523095b2dda5fcc79012866b315fb0 [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
Brian Silvermanea2c95f2021-02-10 18:10:26 -08005#include "aos/aos_cli_utils.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08006#include "aos/configuration.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08007#include "aos/init.h"
8#include "aos/json_to_flatbuffer.h"
9#include "gflags/gflags.h"
10
Austin Schuh893d7f42022-09-16 15:01:35 -070011DEFINE_int64(max_vector_size, 100,
Austin Schuhd3936202020-04-07 20:11:07 -070012 "If positive, vectors longer than this will not be printed");
Austin Schuh893d7f42022-09-16 15:01:35 -070013DEFINE_bool(json, false, "If true, print fully valid JSON");
Brian Silverman1bc2e962020-04-28 15:22:01 -070014DEFINE_bool(fetch, false,
15 "If true, fetch the current message on the channel first");
Ravago Jones5cc9df52020-09-02 21:29:58 -070016DEFINE_bool(pretty, false,
17 "If true, pretty print the messages on multiple lines");
Dan Ford44e02512022-06-07 23:45:14 -070018DEFINE_bool(
19 pretty_max, false,
20 "If true, expand every field to its own line (expands more than -pretty)");
Austin Schuh594eef82020-09-10 18:43:01 -070021DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
22DEFINE_uint64(count, 0,
23 "If >0, aos_dump will exit after printing this many messages.");
Austin Schuh01d88fc2020-09-13 18:48:16 -070024DEFINE_int32(rate_limit, 0,
25 "The minimum amount of time to wait in milliseconds before "
26 "sending another message");
Austin Schuhe787c1d2022-06-23 10:14:30 -070027DEFINE_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 Griglak043e0e22022-08-18 12:51:18 -060030DEFINE_bool(use_hex, false, "Are integers in the messages printed in hex notation.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070031
Tyler Chatow5e369a42019-11-23 11:57:31 -080032int main(int argc, char **argv) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070033 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 Chatow5e369a42019-11-23 11:57:31 -080039 aos::InitGoogle(&argc, &argv);
40
Brian Silvermanea2c95f2021-02-10 18:10:26 -080041 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070042 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 Schuhba2c8652022-08-17 14:56:08 -070048 "channel is readeable on node", true)) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070049 return 0;
50 }
51
Austin Schuh594eef82020-09-10 18:43:01 -070052 uint64_t message_count = 0;
53
Tyler Chatowfcf16f42020-07-26 12:41:36 -070054 aos::FastStringBuilder str_builder;
55
Austin Schuh01d88fc2020-09-13 18:48:16 -070056 aos::monotonic_clock::time_point next_send_time =
57 aos::monotonic_clock::min_time;
Austin Schuh893d7f42022-09-16 15:01:35 -070058
Brian Silvermanea2c95f2021-02-10 18:10:26 -080059 for (const aos::Channel *channel : cli_info.found_channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070060 if (FLAGS_fetch) {
61 const std::unique_ptr<aos::RawFetcher> fetcher =
Brian Silvermanea2c95f2021-02-10 18:10:26 -080062 cli_info.event_loop->MakeRawFetcher(channel);
Brian Silverman83ff9c12020-06-23 16:20:27 -070063 if (fetcher->Fetch()) {
Austin Schuh893d7f42022-09-16 15:01:35 -070064 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 Schuh594eef82020-09-10 18:43:01 -070075 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -070076 }
77 }
78
Austin Schuh594eef82020-09-10 18:43:01 -070079 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
80 return 0;
81 }
82
Austin Schuhe787c1d2022-06-23 10:14:30 -070083 if (FLAGS_timeout == 0) {
84 continue;
85 }
86
Brian Silvermanea2c95f2021-02-10 18:10:26 -080087 cli_info.event_loop->MakeRawWatcher(
Austin Schuh01d88fc2020-09-13 18:48:16 -070088 channel,
Brian Silvermanea2c95f2021-02-10 18:10:26 -080089 [channel, &str_builder, &cli_info, &message_count, &next_send_time](
Austin Schuh01d88fc2020-09-13 18:48:16 -070090 const aos::Context &context, const void * /*message*/) {
91 if (context.monotonic_event_time > next_send_time) {
Austin Schuh58922e52021-04-10 16:03:25 -070092 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
93 return;
94 }
Austin Schuh893d7f42022-09-16 15:01:35 -070095 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 Schuh08f27b42020-10-31 14:14:41 -0700106 ++message_count;
Austin Schuh01d88fc2020-09-13 18:48:16 -0700107 next_send_time = context.monotonic_event_time +
108 std::chrono::milliseconds(FLAGS_rate_limit);
109 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800110 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -0700111 }
Austin Schuh594eef82020-09-10 18:43:01 -0700112 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700113 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700114 }
115
Austin Schuhe787c1d2022-06-23 10:14:30 -0700116 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 Silvermanea2c95f2021-02-10 18:10:26 -0800131 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700132
Tyler Chatow5e369a42019-11-23 11:57:31 -0800133 return 0;
134}