blob: 86f4305a4c8f0145962656d23b1138dcf4adb8e2 [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 Schuhd3936202020-04-07 20:11:07 -070011DEFINE_int32(max_vector_size, 100,
12 "If positive, vectors longer than this will not be printed");
Brian Silverman1bc2e962020-04-28 15:22:01 -070013DEFINE_bool(fetch, false,
14 "If true, fetch the current message on the channel first");
Ravago Jones5cc9df52020-09-02 21:29:58 -070015DEFINE_bool(pretty, false,
16 "If true, pretty print the messages on multiple lines");
Dan Ford44e02512022-06-07 23:45:14 -070017DEFINE_bool(
18 pretty_max, false,
19 "If true, expand every field to its own line (expands more than -pretty)");
Austin Schuh594eef82020-09-10 18:43:01 -070020DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
21DEFINE_uint64(count, 0,
22 "If >0, aos_dump will exit after printing this many messages.");
Austin Schuh01d88fc2020-09-13 18:48:16 -070023DEFINE_int32(rate_limit, 0,
24 "The minimum amount of time to wait in milliseconds before "
25 "sending another message");
Austin Schuhe787c1d2022-06-23 10:14:30 -070026DEFINE_int32(timeout, -1,
27 "The max time in milliseconds to wait for messages before "
28 "exiting. -1 means forever, 0 means don't wait.");
Brian J Griglak043e0e22022-08-18 12:51:18 -060029DEFINE_bool(use_hex, false, "Are integers in the messages printed in hex notation.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070030
31namespace {
32
Tyler Chatowfcf16f42020-07-26 12:41:36 -070033void PrintMessage(const aos::Channel *channel, const aos::Context &context,
34 aos::FastStringBuilder *builder) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070035 // Print the flatbuffer out to stdout, both to remove the
36 // unnecessary cruft from glog and to allow the user to readily
37 // redirect just the logged output independent of any debugging
38 // information on stderr.
Tyler Chatowfcf16f42020-07-26 12:41:36 -070039
40 builder->Reset();
Austin Schuha9df9ad2021-06-16 14:49:39 -070041
42 CHECK(flatbuffers::Verify(*channel->schema(),
43 *channel->schema()->root_table(),
44 static_cast<const uint8_t *>(context.data),
45 static_cast<size_t>(context.size)))
46 << ": Corrupted flatbuffer on " << channel->name()->c_str() << " "
47 << channel->type()->c_str();
48
Ravago Jones5cc9df52020-09-02 21:29:58 -070049 aos::FlatbufferToJson(
50 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
Dan Ford44e02512022-06-07 23:45:14 -070051 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size),
Brian J Griglak043e0e22022-08-18 12:51:18 -060052 FLAGS_pretty_max, FLAGS_use_hex});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070053
Austin Schuh594eef82020-09-10 18:43:01 -070054 if (FLAGS_print_timestamps) {
55 if (context.monotonic_remote_time != context.monotonic_event_time) {
56 std::cout << context.realtime_remote_time << " ("
57 << context.monotonic_remote_time << ") delivered "
58 << context.realtime_event_time << " ("
59 << context.monotonic_event_time << "): " << *builder << '\n';
60 } else {
61 std::cout << context.realtime_event_time << " ("
62 << context.monotonic_event_time << "): " << *builder << '\n';
63 }
Brian Silverman1bc2e962020-04-28 15:22:01 -070064 } else {
Austin Schuh594eef82020-09-10 18:43:01 -070065 std::cout << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070066 }
67}
68
69} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080070
Tyler Chatow5e369a42019-11-23 11:57:31 -080071int main(int argc, char **argv) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070072 gflags::SetUsageMessage(
73 "Prints messages from arbitrary channels as they are received given a "
74 "configuration file describing the channels to listen on.\nTypical "
75 "Usage: aos_dump [--config path_to_config.json] channel_name "
76 "message_type\nExample Usage: aos_dump --config pingpong_config.json "
77 "/test aos.examples.Ping");
Tyler Chatow5e369a42019-11-23 11:57:31 -080078 aos::InitGoogle(&argc, &argv);
79
Brian Silvermanea2c95f2021-02-10 18:10:26 -080080 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070081 if (cli_info.Initialize(
82 &argc, &argv,
83 [&cli_info](const aos::Channel *channel) {
84 return aos::configuration::ChannelIsReadableOnNode(
85 channel, cli_info.event_loop->node());
86 },
Austin Schuhba2c8652022-08-17 14:56:08 -070087 "channel is readeable on node", true)) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070088 return 0;
89 }
90
Austin Schuh594eef82020-09-10 18:43:01 -070091 uint64_t message_count = 0;
92
Tyler Chatowfcf16f42020-07-26 12:41:36 -070093 aos::FastStringBuilder str_builder;
94
Austin Schuh01d88fc2020-09-13 18:48:16 -070095 aos::monotonic_clock::time_point next_send_time =
96 aos::monotonic_clock::min_time;
Brian Silvermanea2c95f2021-02-10 18:10:26 -080097 for (const aos::Channel *channel : cli_info.found_channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070098 if (FLAGS_fetch) {
99 const std::unique_ptr<aos::RawFetcher> fetcher =
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800100 cli_info.event_loop->MakeRawFetcher(channel);
Brian Silverman83ff9c12020-06-23 16:20:27 -0700101 if (fetcher->Fetch()) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700102 PrintMessage(channel, fetcher->context(), &str_builder);
Austin Schuh594eef82020-09-10 18:43:01 -0700103 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -0700104 }
105 }
106
Austin Schuh594eef82020-09-10 18:43:01 -0700107 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
108 return 0;
109 }
110
Austin Schuhe787c1d2022-06-23 10:14:30 -0700111 if (FLAGS_timeout == 0) {
112 continue;
113 }
114
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800115 cli_info.event_loop->MakeRawWatcher(
Austin Schuh01d88fc2020-09-13 18:48:16 -0700116 channel,
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800117 [channel, &str_builder, &cli_info, &message_count, &next_send_time](
Austin Schuh01d88fc2020-09-13 18:48:16 -0700118 const aos::Context &context, const void * /*message*/) {
119 if (context.monotonic_event_time > next_send_time) {
Austin Schuh58922e52021-04-10 16:03:25 -0700120 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
121 return;
122 }
Austin Schuh01d88fc2020-09-13 18:48:16 -0700123 PrintMessage(channel, context, &str_builder);
Austin Schuh08f27b42020-10-31 14:14:41 -0700124 ++message_count;
Austin Schuh01d88fc2020-09-13 18:48:16 -0700125 next_send_time = context.monotonic_event_time +
126 std::chrono::milliseconds(FLAGS_rate_limit);
127 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800128 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -0700129 }
Austin Schuh594eef82020-09-10 18:43:01 -0700130 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700131 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700132 }
133
Austin Schuhe787c1d2022-06-23 10:14:30 -0700134 if (FLAGS_timeout == 0) {
135 return 0;
136 }
137
138 if (FLAGS_timeout > 0) {
139 aos::TimerHandler *handle = cli_info.event_loop->AddTimer(
140 [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); });
141
142 cli_info.event_loop->OnRun(
143 [handle, event_loop = &cli_info.event_loop.value()]() {
144 handle->Setup(event_loop->monotonic_now() +
145 std::chrono::milliseconds(FLAGS_timeout));
146 });
147 }
148
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800149 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700150
Tyler Chatow5e369a42019-11-23 11:57:31 -0800151 return 0;
152}