blob: ad50b0bfe4e485281252bc06e5532697e9b10555 [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
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "gflags/gflags.h"
6
Brian Silvermanea2c95f2021-02-10 18:10:26 -08007#include "aos/aos_cli_utils.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08008#include "aos/configuration.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08009#include "aos/init.h"
10#include "aos/json_to_flatbuffer.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080011
Austin Schuh893d7f42022-09-16 15:01:35 -070012DEFINE_int64(max_vector_size, 100,
Austin Schuhd3936202020-04-07 20:11:07 -070013 "If positive, vectors longer than this will not be printed");
Austin Schuh893d7f42022-09-16 15:01:35 -070014DEFINE_bool(json, false, "If true, print fully valid JSON");
Brian Silverman1bc2e962020-04-28 15:22:01 -070015DEFINE_bool(fetch, false,
16 "If true, fetch the current message on the channel first");
Ravago Jones5cc9df52020-09-02 21:29:58 -070017DEFINE_bool(pretty, false,
18 "If true, pretty print the messages on multiple lines");
Dan Ford44e02512022-06-07 23:45:14 -070019DEFINE_bool(
20 pretty_max, false,
21 "If true, expand every field to its own line (expands more than -pretty)");
Austin Schuh594eef82020-09-10 18:43:01 -070022DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
23DEFINE_uint64(count, 0,
24 "If >0, aos_dump will exit after printing this many messages.");
Austin Schuh01d88fc2020-09-13 18:48:16 -070025DEFINE_int32(rate_limit, 0,
26 "The minimum amount of time to wait in milliseconds before "
27 "sending another message");
Austin Schuhe787c1d2022-06-23 10:14:30 -070028DEFINE_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 Schuhbe6d2962022-11-01 09:24:56 -070031DEFINE_bool(hex, false,
Naman Gupta54047212022-09-23 14:10:30 -070032 "Are integers in the messages printed in hex notation.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070033
Tyler Chatow5e369a42019-11-23 11:57:31 -080034int main(int argc, char **argv) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070035 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 Chatow5e369a42019-11-23 11:57:31 -080041 aos::InitGoogle(&argc, &argv);
42
Brian Silvermanea2c95f2021-02-10 18:10:26 -080043 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070044 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 Schuhba2c8652022-08-17 14:56:08 -070050 "channel is readeable on node", true)) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070051 return 0;
52 }
53
Austin Schuh594eef82020-09-10 18:43:01 -070054 uint64_t message_count = 0;
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
Naman Gupta54047212022-09-23 14:10:30 -070059 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 Schuhbe6d2962022-11-01 09:24:56 -070067 .hex = 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) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070072 if (FLAGS_fetch) {
73 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());
Austin Schuh594eef82020-09-10 18:43:01 -070077 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -070078 }
79 }
80
Naman Gupta54047212022-09-23 14:10:30 -070081 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Austin Schuh594eef82020-09-10 18:43:01 -070082 return 0;
83 }
84
Austin Schuhe787c1d2022-06-23 10:14:30 -070085 if (FLAGS_timeout == 0) {
86 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) {
Naman Gupta54047212022-09-23 14:10:30 -070093 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Austin Schuh58922e52021-04-10 16:03:25 -070094 return;
95 }
Naman Gupta54047212022-09-23 14:10:30 -070096
97 printer.PrintMessage(channel, context);
Austin Schuh01d88fc2020-09-13 18:48:16 -070098 next_send_time = context.monotonic_event_time +
99 std::chrono::milliseconds(FLAGS_rate_limit);
Naman Gupta54047212022-09-23 14:10:30 -0700100 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800101 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -0700102 }
Austin Schuh594eef82020-09-10 18:43:01 -0700103 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700104 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700105 }
106
Austin Schuhe787c1d2022-06-23 10:14:30 -0700107 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 Silvermanea2c95f2021-02-10 18:10:26 -0800122 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700123
Tyler Chatow5e369a42019-11-23 11:57:31 -0800124 return 0;
125}