blob: ef61f6ba5c2bcdd631f0b029fe02fe36784d7e2d [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.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070020
21namespace {
22
Tyler Chatowfcf16f42020-07-26 12:41:36 -070023void PrintMessage(const aos::Channel *channel, const aos::Context &context,
24 aos::FastStringBuilder *builder) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070025 // Print the flatbuffer out to stdout, both to remove the
26 // unnecessary cruft from glog and to allow the user to readily
27 // redirect just the logged output independent of any debugging
28 // information on stderr.
Tyler Chatowfcf16f42020-07-26 12:41:36 -070029
30 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070031 aos::FlatbufferToJson(
32 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
33 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070034
Austin Schuh594eef82020-09-10 18:43:01 -070035 if (FLAGS_print_timestamps) {
36 if (context.monotonic_remote_time != context.monotonic_event_time) {
37 std::cout << context.realtime_remote_time << " ("
38 << context.monotonic_remote_time << ") delivered "
39 << context.realtime_event_time << " ("
40 << context.monotonic_event_time << "): " << *builder << '\n';
41 } else {
42 std::cout << context.realtime_event_time << " ("
43 << context.monotonic_event_time << "): " << *builder << '\n';
44 }
Brian Silverman1bc2e962020-04-28 15:22:01 -070045 } else {
Austin Schuh594eef82020-09-10 18:43:01 -070046 std::cout << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070047 }
48}
49
50} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080051
Tyler Chatow5e369a42019-11-23 11:57:31 -080052int main(int argc, char **argv) {
53 aos::InitGoogle(&argc, &argv);
54
55 std::string channel_name;
56 std::string message_type;
57 if (argc > 1) {
58 channel_name = argv[1];
59 }
60 if (argc > 2) {
61 message_type = argv[2];
62 }
63
64 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
65 aos::configuration::ReadConfig(FLAGS_config);
66
67 const aos::Configuration *config_msg = &config.message();
Austin Schuh594eef82020-09-10 18:43:01 -070068 aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080069 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080070 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080071
72 if (argc == 1) {
73 std::cout << "Channels:\n";
74 for (const aos::Channel *channel : *config_msg->channels()) {
75 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
76 << '\n';
77 }
78 return 0;
79 }
80
Brian Silverman83ff9c12020-06-23 16:20:27 -070081 std::vector<const aos::Channel *> found_channels;
Tyler Chatow5e369a42019-11-23 11:57:31 -080082 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
83 config_msg->channels();
Brian Silverman83ff9c12020-06-23 16:20:27 -070084 bool found_exact = false;
Tyler Chatow5e369a42019-11-23 11:57:31 -080085 for (const aos::Channel *channel : *channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070086 if (channel->name()->c_str() != channel_name) {
87 continue;
Tyler Chatow5e369a42019-11-23 11:57:31 -080088 }
Brian Silverman83ff9c12020-06-23 16:20:27 -070089 if (channel->type()->string_view() == message_type) {
90 if (!found_exact) {
91 found_channels.clear();
92 found_exact = true;
93 }
94 } else if (!found_exact && channel->type()->string_view().find(
95 message_type) != std::string_view::npos) {
96 } else {
97 continue;
98 }
99 found_channels.push_back(channel);
Tyler Chatow5e369a42019-11-23 11:57:31 -0800100 }
101
Brian Silverman83ff9c12020-06-23 16:20:27 -0700102 if (found_channels.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -0800103 LOG(FATAL) << "Could not find any channels with the given name and type.";
Brian Silverman83ff9c12020-06-23 16:20:27 -0700104 } else if (found_channels.size() > 1 && !message_type.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -0800105 LOG(FATAL) << "Multiple channels found with same type";
106 }
107
Austin Schuh594eef82020-09-10 18:43:01 -0700108 uint64_t message_count = 0;
109
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700110 aos::FastStringBuilder str_builder;
111
Brian Silverman83ff9c12020-06-23 16:20:27 -0700112 for (const aos::Channel *channel : found_channels) {
113 if (FLAGS_fetch) {
114 const std::unique_ptr<aos::RawFetcher> fetcher =
115 event_loop.MakeRawFetcher(channel);
116 if (fetcher->Fetch()) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700117 PrintMessage(channel, fetcher->context(), &str_builder);
Austin Schuh594eef82020-09-10 18:43:01 -0700118 ++message_count;
Brian Silverman83ff9c12020-06-23 16:20:27 -0700119 }
120 }
121
Austin Schuh594eef82020-09-10 18:43:01 -0700122 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
123 return 0;
124 }
125
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700126 event_loop.MakeRawWatcher(
Austin Schuh594eef82020-09-10 18:43:01 -0700127 channel, [channel, &str_builder, &event_loop, &message_count](
128 const aos::Context &context, const void * /*message*/) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700129 PrintMessage(channel, context, &str_builder);
Austin Schuh594eef82020-09-10 18:43:01 -0700130 ++message_count;
131 if (FLAGS_count > 0 && message_count >= FLAGS_count) {
132 event_loop.Exit();
133 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700134 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700135 }
136
Tyler Chatow5e369a42019-11-23 11:57:31 -0800137 event_loop.Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700138
Tyler Chatow5e369a42019-11-23 11:57:31 -0800139 return 0;
140}