blob: a0cca5de6adbd09638ae5a2969905ad779e513a8 [file] [log] [blame]
Tyler Chatow5e369a42019-11-23 11:57:31 -08001#include <iostream>
2#include <map>
3
4#include "aos/configuration.h"
5#include "aos/events/shm_event_loop.h"
6#include "aos/init.h"
7#include "aos/json_to_flatbuffer.h"
8#include "gflags/gflags.h"
9
10DEFINE_string(config, "./config.json", "File path of aos configuration");
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");
Brian Silverman42402e52020-09-22 22:21:13 -070017DEFINE_bool(all, false,
18 "If true, print out the channels for all nodes, not just the "
19 "channels which are visible on this node.");
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");
Brian Silverman1bc2e962020-04-28 15:22:01 -070026
27namespace {
28
Tyler Chatowfcf16f42020-07-26 12:41:36 -070029void PrintMessage(const aos::Channel *channel, const aos::Context &context,
30 aos::FastStringBuilder *builder) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070031 // Print the flatbuffer out to stdout, both to remove the
32 // unnecessary cruft from glog and to allow the user to readily
33 // redirect just the logged output independent of any debugging
34 // information on stderr.
Tyler Chatowfcf16f42020-07-26 12:41:36 -070035
36 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070037 aos::FlatbufferToJson(
38 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
39 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070040
Austin Schuh594eef82020-09-10 18:43:01 -070041 if (FLAGS_print_timestamps) {
42 if (context.monotonic_remote_time != context.monotonic_event_time) {
43 std::cout << context.realtime_remote_time << " ("
44 << context.monotonic_remote_time << ") delivered "
45 << context.realtime_event_time << " ("
46 << context.monotonic_event_time << "): " << *builder << '\n';
47 } else {
48 std::cout << context.realtime_event_time << " ("
49 << context.monotonic_event_time << "): " << *builder << '\n';
50 }
Brian Silverman1bc2e962020-04-28 15:22:01 -070051 } else {
Austin Schuh594eef82020-09-10 18:43:01 -070052 std::cout << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070053 }
54}
55
56} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080057
Tyler Chatow5e369a42019-11-23 11:57:31 -080058int main(int argc, char **argv) {
59 aos::InitGoogle(&argc, &argv);
60
61 std::string channel_name;
62 std::string message_type;
63 if (argc > 1) {
64 channel_name = argv[1];
65 }
66 if (argc > 2) {
67 message_type = argv[2];
68 }
69
70 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
71 aos::configuration::ReadConfig(FLAGS_config);
72
73 const aos::Configuration *config_msg = &config.message();
Austin Schuh594eef82020-09-10 18:43:01 -070074 aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080075 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080076 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080077
78 if (argc == 1) {
79 std::cout << "Channels:\n";
80 for (const aos::Channel *channel : *config_msg->channels()) {
Brian Silverman42402e52020-09-22 22:21:13 -070081 if (FLAGS_all || aos::configuration::ChannelIsReadableOnNode(
82 channel, event_loop.node())) {
83 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
84 << '\n';
85 }
Tyler Chatow5e369a42019-11-23 11:57:31 -080086 }
87 return 0;
88 }
89
Brian Silverman83ff9c12020-06-23 16:20:27 -070090 std::vector<const aos::Channel *> found_channels;
Tyler Chatow5e369a42019-11-23 11:57:31 -080091 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
92 config_msg->channels();
Brian Silverman83ff9c12020-06-23 16:20:27 -070093 bool found_exact = false;
Tyler Chatow5e369a42019-11-23 11:57:31 -080094 for (const aos::Channel *channel : *channels) {
Brian Silverman42402e52020-09-22 22:21:13 -070095 if (!FLAGS_all && !aos::configuration::ChannelIsReadableOnNode(
96 channel, event_loop.node())) {
97 continue;
98 }
Brian Silverman83ff9c12020-06-23 16:20:27 -070099 if (channel->name()->c_str() != channel_name) {
100 continue;
Tyler Chatow5e369a42019-11-23 11:57:31 -0800101 }
Brian Silverman83ff9c12020-06-23 16:20:27 -0700102 if (channel->type()->string_view() == message_type) {
103 if (!found_exact) {
104 found_channels.clear();
105 found_exact = true;
106 }
107 } else if (!found_exact && channel->type()->string_view().find(
108 message_type) != std::string_view::npos) {
109 } else {
110 continue;
111 }
112 found_channels.push_back(channel);
Tyler Chatow5e369a42019-11-23 11:57:31 -0800113 }
114
Brian Silverman83ff9c12020-06-23 16:20:27 -0700115 if (found_channels.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -0800116 LOG(FATAL) << "Could not find any channels with the given name and type.";
Brian Silverman83ff9c12020-06-23 16:20:27 -0700117 } else if (found_channels.size() > 1 && !message_type.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -0800118 LOG(FATAL) << "Multiple channels found with same type";
119 }
120
Austin Schuh594eef82020-09-10 18:43:01 -0700121 uint64_t message_count = 0;
122
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700123 aos::FastStringBuilder str_builder;
124
Austin Schuh01d88fc2020-09-13 18:48:16 -0700125 aos::monotonic_clock::time_point next_send_time =
126 aos::monotonic_clock::min_time;
Brian Silverman83ff9c12020-06-23 16:20:27 -0700127 for (const aos::Channel *channel : found_channels) {
128 if (FLAGS_fetch) {
129 const std::unique_ptr<aos::RawFetcher> fetcher =
130 event_loop.MakeRawFetcher(channel);
131 if (fetcher->Fetch()) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700132 PrintMessage(channel, fetcher->context(), &str_builder);
Austin Schuh594eef82020-09-10 18:43:01 -0700133 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -0700134 }
135 }
136
Austin Schuh594eef82020-09-10 18:43:01 -0700137 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
138 return 0;
139 }
140
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700141 event_loop.MakeRawWatcher(
Austin Schuh01d88fc2020-09-13 18:48:16 -0700142 channel,
143 [channel, &str_builder, &event_loop, &message_count, &next_send_time](
144 const aos::Context &context, const void * /*message*/) {
145 if (context.monotonic_event_time > next_send_time) {
146 PrintMessage(channel, context, &str_builder);
147 next_send_time = context.monotonic_event_time +
148 std::chrono::milliseconds(FLAGS_rate_limit);
149 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
150 event_loop.Exit();
151 }
Austin Schuh594eef82020-09-10 18:43:01 -0700152 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700153 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700154 }
155
Tyler Chatow5e369a42019-11-23 11:57:31 -0800156 event_loop.Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700157
Tyler Chatow5e369a42019-11-23 11:57:31 -0800158 return 0;
159}