blob: ac82b95054579c9c275a87fb22b7208f575fd462 [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 Silverman1bc2e962020-04-28 15:22:01 -070017
18namespace {
19
Tyler Chatowfcf16f42020-07-26 12:41:36 -070020void PrintMessage(const aos::Channel *channel, const aos::Context &context,
21 aos::FastStringBuilder *builder) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070022 // Print the flatbuffer out to stdout, both to remove the
23 // unnecessary cruft from glog and to allow the user to readily
24 // redirect just the logged output independent of any debugging
25 // information on stderr.
Tyler Chatowfcf16f42020-07-26 12:41:36 -070026
27 builder->Reset();
Ravago Jones5cc9df52020-09-02 21:29:58 -070028 aos::FlatbufferToJson(
29 builder, channel->schema(), static_cast<const uint8_t *>(context.data),
30 {FLAGS_pretty, static_cast<size_t>(FLAGS_max_vector_size)});
Tyler Chatowfcf16f42020-07-26 12:41:36 -070031
Brian Silverman1bc2e962020-04-28 15:22:01 -070032 if (context.monotonic_remote_time != context.monotonic_event_time) {
33 std::cout << context.realtime_remote_time << " ("
34 << context.monotonic_remote_time << ") delivered "
35 << context.realtime_event_time << " ("
Tyler Chatowfcf16f42020-07-26 12:41:36 -070036 << context.monotonic_event_time << "): " << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070037 } else {
38 std::cout << context.realtime_event_time << " ("
Tyler Chatowfcf16f42020-07-26 12:41:36 -070039 << context.monotonic_event_time << "): " << *builder << '\n';
Brian Silverman1bc2e962020-04-28 15:22:01 -070040 }
41}
42
43} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080044
Tyler Chatow5e369a42019-11-23 11:57:31 -080045int main(int argc, char **argv) {
46 aos::InitGoogle(&argc, &argv);
47
48 std::string channel_name;
49 std::string message_type;
50 if (argc > 1) {
51 channel_name = argv[1];
52 }
53 if (argc > 2) {
54 message_type = argv[2];
55 }
56
57 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
58 aos::configuration::ReadConfig(FLAGS_config);
59
60 const aos::Configuration *config_msg = &config.message();
61 ::aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080062 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080063 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080064
65 if (argc == 1) {
66 std::cout << "Channels:\n";
67 for (const aos::Channel *channel : *config_msg->channels()) {
68 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
69 << '\n';
70 }
71 return 0;
72 }
73
Brian Silverman83ff9c12020-06-23 16:20:27 -070074 std::vector<const aos::Channel *> found_channels;
Tyler Chatow5e369a42019-11-23 11:57:31 -080075 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
76 config_msg->channels();
Brian Silverman83ff9c12020-06-23 16:20:27 -070077 bool found_exact = false;
Tyler Chatow5e369a42019-11-23 11:57:31 -080078 for (const aos::Channel *channel : *channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070079 if (channel->name()->c_str() != channel_name) {
80 continue;
Tyler Chatow5e369a42019-11-23 11:57:31 -080081 }
Brian Silverman83ff9c12020-06-23 16:20:27 -070082 if (channel->type()->string_view() == message_type) {
83 if (!found_exact) {
84 found_channels.clear();
85 found_exact = true;
86 }
87 } else if (!found_exact && channel->type()->string_view().find(
88 message_type) != std::string_view::npos) {
89 } else {
90 continue;
91 }
92 found_channels.push_back(channel);
Tyler Chatow5e369a42019-11-23 11:57:31 -080093 }
94
Brian Silverman83ff9c12020-06-23 16:20:27 -070095 if (found_channels.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -080096 LOG(FATAL) << "Could not find any channels with the given name and type.";
Brian Silverman83ff9c12020-06-23 16:20:27 -070097 } else if (found_channels.size() > 1 && !message_type.empty()) {
Tyler Chatow5e369a42019-11-23 11:57:31 -080098 LOG(FATAL) << "Multiple channels found with same type";
99 }
100
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700101 aos::FastStringBuilder str_builder;
102
Brian Silverman83ff9c12020-06-23 16:20:27 -0700103 for (const aos::Channel *channel : found_channels) {
104 if (FLAGS_fetch) {
105 const std::unique_ptr<aos::RawFetcher> fetcher =
106 event_loop.MakeRawFetcher(channel);
107 if (fetcher->Fetch()) {
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700108 PrintMessage(channel, fetcher->context(), &str_builder);
Brian Silverman83ff9c12020-06-23 16:20:27 -0700109 }
110 }
111
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700112 event_loop.MakeRawWatcher(
113 channel, [channel, &str_builder](const aos::Context &context,
114 const void * /*message*/) {
115 PrintMessage(channel, context, &str_builder);
116 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700117 }
118
Tyler Chatow5e369a42019-11-23 11:57:31 -0800119 event_loop.Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700120
Tyler Chatow5e369a42019-11-23 11:57:31 -0800121 return 0;
122}