blob: 573a06c23d787354f10c3b4999fd5a7ce4f8ff45 [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.");
Naman Gupta54047212022-09-23 14:10:30 -070030DEFINE_bool(use_hex, false,
31 "Are integers in the messages printed in hex notation.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070032
Tyler Chatow5e369a42019-11-23 11:57:31 -080033int main(int argc, char **argv) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070034 gflags::SetUsageMessage(
35 "Prints messages from arbitrary channels as they are received given a "
36 "configuration file describing the channels to listen on.\nTypical "
37 "Usage: aos_dump [--config path_to_config.json] channel_name "
38 "message_type\nExample Usage: aos_dump --config pingpong_config.json "
39 "/test aos.examples.Ping");
Tyler Chatow5e369a42019-11-23 11:57:31 -080040 aos::InitGoogle(&argc, &argv);
41
Brian Silvermanea2c95f2021-02-10 18:10:26 -080042 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070043 if (cli_info.Initialize(
44 &argc, &argv,
45 [&cli_info](const aos::Channel *channel) {
46 return aos::configuration::ChannelIsReadableOnNode(
47 channel, cli_info.event_loop->node());
48 },
Austin Schuhba2c8652022-08-17 14:56:08 -070049 "channel is readeable on node", true)) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070050 return 0;
51 }
52
Austin Schuh594eef82020-09-10 18:43:01 -070053 uint64_t message_count = 0;
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 {
60 .pretty = FLAGS_pretty,
61 .max_vector_size = static_cast<size_t>(FLAGS_max_vector_size),
62 .pretty_max = FLAGS_pretty_max,
63 .print_timestamps = FLAGS_print_timestamps,
64 .json = FLAGS_json,
65 .distributed_clock = false,
66 .use_hex = FLAGS_use_hex,
67 },
68 /*flush*/ true);
69
Brian Silvermanea2c95f2021-02-10 18:10:26 -080070 for (const aos::Channel *channel : cli_info.found_channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070071 if (FLAGS_fetch) {
72 const std::unique_ptr<aos::RawFetcher> fetcher =
Brian Silvermanea2c95f2021-02-10 18:10:26 -080073 cli_info.event_loop->MakeRawFetcher(channel);
Brian Silverman83ff9c12020-06-23 16:20:27 -070074 if (fetcher->Fetch()) {
Naman Gupta54047212022-09-23 14:10:30 -070075 printer.PrintMessage(channel, fetcher->context());
Austin Schuh594eef82020-09-10 18:43:01 -070076 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -070077 }
78 }
79
Naman Gupta54047212022-09-23 14:10:30 -070080 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Austin Schuh594eef82020-09-10 18:43:01 -070081 return 0;
82 }
83
Austin Schuhe787c1d2022-06-23 10:14:30 -070084 if (FLAGS_timeout == 0) {
85 continue;
86 }
87
Brian Silvermanea2c95f2021-02-10 18:10:26 -080088 cli_info.event_loop->MakeRawWatcher(
Naman Gupta54047212022-09-23 14:10:30 -070089 channel, [channel, &printer, &cli_info, &next_send_time](
90 const aos::Context &context, const void * /*message*/) {
Austin Schuh01d88fc2020-09-13 18:48:16 -070091 if (context.monotonic_event_time > next_send_time) {
Naman Gupta54047212022-09-23 14:10:30 -070092 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Austin Schuh58922e52021-04-10 16:03:25 -070093 return;
94 }
Naman Gupta54047212022-09-23 14:10:30 -070095
96 printer.PrintMessage(channel, context);
Austin Schuh01d88fc2020-09-13 18:48:16 -070097 next_send_time = context.monotonic_event_time +
98 std::chrono::milliseconds(FLAGS_rate_limit);
Naman Gupta54047212022-09-23 14:10:30 -070099 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800100 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -0700101 }
Austin Schuh594eef82020-09-10 18:43:01 -0700102 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700103 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700104 }
105
Austin Schuhe787c1d2022-06-23 10:14:30 -0700106 if (FLAGS_timeout == 0) {
107 return 0;
108 }
109
110 if (FLAGS_timeout > 0) {
111 aos::TimerHandler *handle = cli_info.event_loop->AddTimer(
112 [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); });
113
114 cli_info.event_loop->OnRun(
115 [handle, event_loop = &cli_info.event_loop.value()]() {
116 handle->Setup(event_loop->monotonic_now() +
117 std::chrono::milliseconds(FLAGS_timeout));
118 });
119 }
120
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800121 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700122
Tyler Chatow5e369a42019-11-23 11:57:31 -0800123 return 0;
124}