blob: fd33eca47e433c75e67a03fc6b2d01ea4cdc17b5 [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");
Austin Schuh15649d62019-12-28 16:36:38 -080013
Tyler Chatow5e369a42019-11-23 11:57:31 -080014int main(int argc, char **argv) {
15 aos::InitGoogle(&argc, &argv);
16
17 std::string channel_name;
18 std::string message_type;
19 if (argc > 1) {
20 channel_name = argv[1];
21 }
22 if (argc > 2) {
23 message_type = argv[2];
24 }
25
26 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
27 aos::configuration::ReadConfig(FLAGS_config);
28
29 const aos::Configuration *config_msg = &config.message();
30 ::aos::ShmEventLoop event_loop(config_msg);
James Kuszmaul5c22e082019-12-14 20:43:07 -080031 event_loop.SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080032 event_loop.SkipAosLog();
Tyler Chatow5e369a42019-11-23 11:57:31 -080033
34 if (argc == 1) {
35 std::cout << "Channels:\n";
36 for (const aos::Channel *channel : *config_msg->channels()) {
37 std::cout << channel->name()->c_str() << ' ' << channel->type()->c_str()
38 << '\n';
39 }
40 return 0;
41 }
42
43 int found_channels = 0;
44 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
45 config_msg->channels();
46 for (const aos::Channel *channel : *channels) {
47 if (channel->name()->c_str() == channel_name &&
48 channel->type()->str().find(message_type) != std::string::npos) {
49 event_loop.MakeRawWatcher(
Austin Schuh15649d62019-12-28 16:36:38 -080050 channel, [channel](const aos::Context &context, const void *message) {
51 // Print the flatbuffer out to stdout, both to remove the
52 // unnecessary cruft from glog and to allow the user to readily
53 // redirect just the logged output independent of any debugging
54 // information on stderr.
55 if (context.monotonic_remote_time != context.monotonic_event_time) {
56 std::cout << context.realtime_remote_time << " ("
57 << context.monotonic_remote_time << ") delivered "
58 << context.realtime_event_time << " ("
59 << context.monotonic_event_time << "): "
60 << aos::FlatbufferToJson(
61 channel->schema(),
Austin Schuhd3936202020-04-07 20:11:07 -070062 static_cast<const uint8_t *>(message),
63 FLAGS_max_vector_size)
Austin Schuh15649d62019-12-28 16:36:38 -080064 << '\n';
65 } else {
66 std::cout << context.realtime_event_time << " ("
67 << context.monotonic_event_time << "): "
68 << aos::FlatbufferToJson(
69 channel->schema(),
Austin Schuhd3936202020-04-07 20:11:07 -070070 static_cast<const uint8_t *>(message),
71 FLAGS_max_vector_size)
Austin Schuh15649d62019-12-28 16:36:38 -080072 << '\n';
73 }
Tyler Chatow5e369a42019-11-23 11:57:31 -080074 });
75 found_channels++;
76 }
77 }
78
79 if (found_channels == 0) {
80 LOG(FATAL) << "Could not find any channels with the given name and type.";
81 } else if (found_channels > 1 && message_type.size() != 0) {
82 LOG(FATAL) << "Multiple channels found with same type";
83 }
84
85 event_loop.Run();
86 ::aos::Cleanup();
87 return 0;
88}