blob: e2467b3abf435542a2cbf34e879fb466ec5408f3 [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");
Austin Schuh594eef82020-09-10 18:43:01 -070017DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
18DEFINE_uint64(count, 0,
19 "If >0, aos_dump will exit after printing this many messages.");
Austin Schuh01d88fc2020-09-13 18:48:16 -070020DEFINE_int32(rate_limit, 0,
21 "The minimum amount of time to wait in milliseconds before "
22 "sending another message");
Brian Silverman1bc2e962020-04-28 15:22:01 -070023
24namespace {
25
Tyler Chatowfcf16f42020-07-26 12:41:36 -070026void PrintMessage(const aos::Channel *channel, const aos::Context &context,
27 aos::FastStringBuilder *builder) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070028 // Print the flatbuffer out to stdout, both to remove the
29 // unnecessary cruft from glog and to allow the user to readily
30 // redirect just the logged output independent of any debugging
31 // information on stderr.
Tyler Chatowfcf16f42020-07-26 12:41:36 -070032
33 builder->Reset();
Austin Schuha9df9ad2021-06-16 14:49:39 -070034
35 CHECK(flatbuffers::Verify(*channel->schema(),
36 *channel->schema()->root_table(),
37 static_cast<const uint8_t *>(context.data),
38 static_cast<size_t>(context.size)))
39 << ": Corrupted flatbuffer on " << channel->name()->c_str() << " "
40 << channel->type()->c_str();
41
Ravago Jones5cc9df52020-09-02 21:29:58 -070042 aos::FlatbufferToJson(
43 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
44 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070045
Austin Schuh594eef82020-09-10 18:43:01 -070046 if (FLAGS_print_timestamps) {
47 if (context.monotonic_remote_time != context.monotonic_event_time) {
48 std::cout << context.realtime_remote_time << " ("
49 << context.monotonic_remote_time << ") delivered "
50 << context.realtime_event_time << " ("
51 << context.monotonic_event_time << "): " << *builder << '\n';
52 } else {
53 std::cout << context.realtime_event_time << " ("
54 << context.monotonic_event_time << "): " << *builder << '\n';
55 }
Brian Silverman1bc2e962020-04-28 15:22:01 -070056 } else {
Austin Schuh594eef82020-09-10 18:43:01 -070057 std::cout << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070058 }
59}
60
61} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080062
Tyler Chatow5e369a42019-11-23 11:57:31 -080063int main(int argc, char **argv) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070064 gflags::SetUsageMessage(
65 "Prints messages from arbitrary channels as they are received given a "
66 "configuration file describing the channels to listen on.\nTypical "
67 "Usage: aos_dump [--config path_to_config.json] channel_name "
68 "message_type\nExample Usage: aos_dump --config pingpong_config.json "
69 "/test aos.examples.Ping");
Tyler Chatow5e369a42019-11-23 11:57:31 -080070 aos::InitGoogle(&argc, &argv);
71
Brian Silvermanea2c95f2021-02-10 18:10:26 -080072 aos::CliUtilInfo cli_info;
73 if (cli_info.Initialize(&argc, &argv,
74 [&cli_info](const aos::Channel *channel) {
75 return aos::configuration::ChannelIsReadableOnNode(
76 channel, cli_info.event_loop->node());
77 })) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070078 return 0;
79 }
80
Austin Schuh594eef82020-09-10 18:43:01 -070081 uint64_t message_count = 0;
82
Tyler Chatowfcf16f42020-07-26 12:41:36 -070083 aos::FastStringBuilder str_builder;
84
Austin Schuh01d88fc2020-09-13 18:48:16 -070085 aos::monotonic_clock::time_point next_send_time =
86 aos::monotonic_clock::min_time;
Brian Silvermanea2c95f2021-02-10 18:10:26 -080087 for (const aos::Channel *channel : cli_info.found_channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070088 if (FLAGS_fetch) {
89 const std::unique_ptr<aos::RawFetcher> fetcher =
Brian Silvermanea2c95f2021-02-10 18:10:26 -080090 cli_info.event_loop->MakeRawFetcher(channel);
Brian Silverman83ff9c12020-06-23 16:20:27 -070091 if (fetcher->Fetch()) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -070092 PrintMessage(channel, fetcher->context(), &str_builder);
Austin Schuh594eef82020-09-10 18:43:01 -070093 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -070094 }
95 }
96
Austin Schuh594eef82020-09-10 18:43:01 -070097 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
98 return 0;
99 }
100
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800101 cli_info.event_loop->MakeRawWatcher(
Austin Schuh01d88fc2020-09-13 18:48:16 -0700102 channel,
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800103 [channel, &str_builder, &cli_info, &message_count, &next_send_time](
Austin Schuh01d88fc2020-09-13 18:48:16 -0700104 const aos::Context &context, const void * /*message*/) {
105 if (context.monotonic_event_time > next_send_time) {
Austin Schuh58922e52021-04-10 16:03:25 -0700106 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
107 return;
108 }
Austin Schuh01d88fc2020-09-13 18:48:16 -0700109 PrintMessage(channel, context, &str_builder);
Austin Schuh08f27b42020-10-31 14:14:41 -0700110 ++message_count;
Austin Schuh01d88fc2020-09-13 18:48:16 -0700111 next_send_time = context.monotonic_event_time +
112 std::chrono::milliseconds(FLAGS_rate_limit);
113 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800114 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -0700115 }
Austin Schuh594eef82020-09-10 18:43:01 -0700116 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700117 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700118 }
119
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800120 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700121
Tyler Chatow5e369a42019-11-23 11:57:31 -0800122 return 0;
123}