blob: 1aee018c56d028849f6117c3c209dc4868dd075d [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");
15
16namespace {
17
18void PrintMessage(const aos::Channel *channel, const aos::Context &context) {
19 // Print the flatbuffer out to stdout, both to remove the
20 // unnecessary cruft from glog and to allow the user to readily
21 // redirect just the logged output independent of any debugging
22 // information on stderr.
23 if (context.monotonic_remote_time != context.monotonic_event_time) {
24 std::cout << context.realtime_remote_time << " ("
25 << context.monotonic_remote_time << ") delivered "
26 << context.realtime_event_time << " ("
27 << context.monotonic_event_time << "): "
28 << aos::FlatbufferToJson(
29 channel->schema(),
30 static_cast<const uint8_t *>(context.data),
31 FLAGS_max_vector_size)
32 << '\n';
33 } else {
34 std::cout << context.realtime_event_time << " ("
35 << context.monotonic_event_time << "): "
36 << aos::FlatbufferToJson(
37 channel->schema(),
38 static_cast<const uint8_t *>(context.data),
39 FLAGS_max_vector_size)
40 << '\n';
41 }
42}
43
44} // namespace
Austin Schuh15649d62019-12-28 16:36:38 -080045
Tyler Chatow5e369a42019-11-23 11:57:31 -080046int main(int argc, char **argv) {
47 aos::InitGoogle(&argc, &argv);
48
49 std::string channel_name;
50 std::string message_type;
51 if (argc > 1) {
52 channel_name = argv[1];
53 }
54 if (argc > 2) {
55 message_type = argv[2];
56 }
57
58 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
59 aos::configuration::ReadConfig(FLAGS_config);
60
61 const aos::Configuration *config_msg = &config.message();
62 ::aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080063 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080064 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080065
66 if (argc == 1) {
67 std::cout << "Channels:\n";
68 for (const aos::Channel *channel : *config_msg->channels()) {
69 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
70 << '\n';
71 }
72 return 0;
73 }
74
75 int found_channels = 0;
76 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
77 config_msg->channels();
78 for (const aos::Channel *channel : *channels) {
79 if (channel->name()->c_str() == channel_name &&
80 channel->type()->str().find(message_type) != std::string::npos) {
Brian Silverman1bc2e962020-04-28 15:22:01 -070081 if (FLAGS_fetch) {
82 const std::unique_ptr<aos::RawFetcher> fetcher =
83 event_loop.MakeRawFetcher(channel);
84 if (fetcher->Fetch()) {
85 PrintMessage(channel, fetcher->context());
86 }
87 }
88
89 event_loop.MakeRawWatcher(channel, [channel](const aos::Context &context,
90 const void * /*message*/) {
91 PrintMessage(channel, context);
92 });
93
Tyler Chatow5e369a42019-11-23 11:57:31 -080094 found_channels++;
95 }
96 }
97
98 if (found_channels == 0) {
99 LOG(FATAL) << "Could not find any channels with the given name and type.";
100 } else if (found_channels > 1 && message_type.size() != 0) {
101 LOG(FATAL) << "Multiple channels found with same type";
102 }
103
104 event_loop.Run();
105 ::aos::Cleanup();
106 return 0;
107}