blob: 055700feb5a4678c660137334f2882075a993340 [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");
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();
Ravago Jones5cc9df52020-09-02 21:29:58 -070034 aos::FlatbufferToJson(
35 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
36 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070037
Austin Schuh594eef82020-09-10 18:43:01 -070038 if (FLAGS_print_timestamps) {
39 if (context.monotonic_remote_time != context.monotonic_event_time) {
40 std::cout << context.realtime_remote_time << " ("
41 << context.monotonic_remote_time << ") delivered "
42 << context.realtime_event_time << " ("
43 << context.monotonic_event_time << "): " << *builder << '\n';
44 } else {
45 std::cout << context.realtime_event_time << " ("
46 << context.monotonic_event_time << "): " << *builder << '\n';
47 }
Brian Silverman1bc2e962020-04-28 15:22:01 -070048 } else {
Austin Schuh594eef82020-09-10 18:43:01 -070049 std::cout << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070050 }
51}
52
53} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080054
Tyler Chatow5e369a42019-11-23 11:57:31 -080055int main(int argc, char **argv) {
56 aos::InitGoogle(&argc, &argv);
57
58 std::string channel_name;
59 std::string message_type;
60 if (argc > 1) {
61 channel_name = argv[1];
62 }
63 if (argc > 2) {
64 message_type = argv[2];
65 }
66
67 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
68 aos::configuration::ReadConfig(FLAGS_config);
69
70 const aos::Configuration *config_msg = &config.message();
Austin Schuh594eef82020-09-10 18:43:01 -070071 aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080072 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080073 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080074
75 if (argc == 1) {
76 std::cout << "Channels:\n";
77 for (const aos::Channel *channel : *config_msg->channels()) {
78 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
79 << '\n';
80 }
81 return 0;
82 }
83
Brian Silverman83ff9c12020-06-23 16:20:27 -070084 std::vector<const aos::Channel *> found_channels;
Tyler Chatow5e369a42019-11-23 11:57:31 -080085 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
86 config_msg->channels();
Brian Silverman83ff9c12020-06-23 16:20:27 -070087 bool found_exact = false;
Tyler Chatow5e369a42019-11-23 11:57:31 -080088 for (const aos::Channel *channel : *channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070089 if (channel->name()->c_str() != channel_name) {
90 continue;
Tyler Chatow5e369a42019-11-23 11:57:31 -080091 }
Brian Silverman83ff9c12020-06-23 16:20:27 -070092 if (channel->type()->string_view() == message_type) {
93 if (!found_exact) {
94 found_channels.clear();
95 found_exact = true;
96 }
97 } else if (!found_exact && channel->type()->string_view().find(
98 message_type) != std::string_view::npos) {
99 } else {
100 continue;
101 }
102 found_channels.push_back(channel);
Tyler Chatow5e369a42019-11-23 11:57:31 -0800103 }
104
Brian Silverman83ff9c12020-06-23 16:20:27 -0700105 if (found_channels.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -0800106 LOG(FATAL) << "Could not find any channels with the given name and type.";
Brian Silverman83ff9c12020-06-23 16:20:27 -0700107 } else if (found_channels.size() > 1 && !message_type.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -0800108 LOG(FATAL) << "Multiple channels found with same type";
109 }
110
Austin Schuh594eef82020-09-10 18:43:01 -0700111 uint64_t message_count = 0;
112
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700113 aos::FastStringBuilder str_builder;
114
Austin Schuh01d88fc2020-09-13 18:48:16 -0700115 aos::monotonic_clock::time_point next_send_time =
116 aos::monotonic_clock::min_time;
Brian Silverman83ff9c12020-06-23 16:20:27 -0700117 for (const aos::Channel *channel : found_channels) {
118 if (FLAGS_fetch) {
119 const std::unique_ptr<aos::RawFetcher> fetcher =
120 event_loop.MakeRawFetcher(channel);
121 if (fetcher->Fetch()) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700122 PrintMessage(channel, fetcher->context(), &str_builder);
Austin Schuh594eef82020-09-10 18:43:01 -0700123 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -0700124 }
125 }
126
Austin Schuh594eef82020-09-10 18:43:01 -0700127 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
128 return 0;
129 }
130
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700131 event_loop.MakeRawWatcher(
Austin Schuh01d88fc2020-09-13 18:48:16 -0700132 channel,
133 [channel, &str_builder, &event_loop, &message_count, &next_send_time](
134 const aos::Context &context, const void * /*message*/) {
135 if (context.monotonic_event_time > next_send_time) {
136 PrintMessage(channel, context, &str_builder);
137 next_send_time = context.monotonic_event_time +
138 std::chrono::milliseconds(FLAGS_rate_limit);
139 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
140 event_loop.Exit();
141 }
Austin Schuh594eef82020-09-10 18:43:01 -0700142 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700143 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700144 }
145
Tyler Chatow5e369a42019-11-23 11:57:31 -0800146 event_loop.Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700147
Tyler Chatow5e369a42019-11-23 11:57:31 -0800148 return 0;
149}