forked from frc971/971-Robot-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaos_dump.cc
124 lines (106 loc) · 4.08 KB
/
aos_dump.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <unistd.h>
#include <iostream>
#include "aos/aos_cli_utils.h"
#include "aos/configuration.h"
#include "aos/init.h"
#include "aos/json_to_flatbuffer.h"
#include "gflags/gflags.h"
DEFINE_int64(max_vector_size, 100,
"If positive, vectors longer than this will not be printed");
DEFINE_bool(json, false, "If true, print fully valid JSON");
DEFINE_bool(fetch, false,
"If true, fetch the current message on the channel first");
DEFINE_bool(pretty, false,
"If true, pretty print the messages on multiple lines");
DEFINE_bool(
pretty_max, false,
"If true, expand every field to its own line (expands more than -pretty)");
DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
DEFINE_uint64(count, 0,
"If >0, aos_dump will exit after printing this many messages.");
DEFINE_int32(rate_limit, 0,
"The minimum amount of time to wait in milliseconds before "
"sending another message");
DEFINE_int32(timeout, -1,
"The max time in milliseconds to wait for messages before "
"exiting. -1 means forever, 0 means don't wait.");
DEFINE_bool(hex, false,
"Are integers in the messages printed in hex notation.");
int main(int argc, char **argv) {
gflags::SetUsageMessage(
"Prints messages from arbitrary channels as they are received given a "
"configuration file describing the channels to listen on.\nTypical "
"Usage: aos_dump [--config path_to_config.json] channel_name "
"message_type\nExample Usage: aos_dump --config pingpong_config.json "
"/test aos.examples.Ping");
aos::InitGoogle(&argc, &argv);
aos::CliUtilInfo cli_info;
if (cli_info.Initialize(
&argc, &argv,
[&cli_info](const aos::Channel *channel) {
return aos::configuration::ChannelIsReadableOnNode(
channel, cli_info.event_loop->node());
},
"channel is readeable on node", true)) {
return 0;
}
uint64_t message_count = 0;
aos::monotonic_clock::time_point next_send_time =
aos::monotonic_clock::min_time;
aos::Printer printer(
{
.pretty = FLAGS_pretty,
.max_vector_size = static_cast<size_t>(FLAGS_max_vector_size),
.pretty_max = FLAGS_pretty_max,
.print_timestamps = FLAGS_print_timestamps,
.json = FLAGS_json,
.distributed_clock = false,
.hex = FLAGS_hex,
},
/*flush*/ true);
for (const aos::Channel *channel : cli_info.found_channels) {
if (FLAGS_fetch) {
const std::unique_ptr<aos::RawFetcher> fetcher =
cli_info.event_loop->MakeRawFetcher(channel);
if (fetcher->Fetch()) {
printer.PrintMessage(channel, fetcher->context());
++message_count;
}
}
if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
return 0;
}
if (FLAGS_timeout == 0) {
continue;
}
cli_info.event_loop->MakeRawWatcher(
channel, [channel, &printer, &cli_info, &next_send_time](
const aos::Context &context, const void * /*message*/) {
if (context.monotonic_event_time > next_send_time) {
if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
return;
}
printer.PrintMessage(channel, context);
next_send_time = context.monotonic_event_time +
std::chrono::milliseconds(FLAGS_rate_limit);
if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
cli_info.event_loop->Exit();
}
}
});
}
if (FLAGS_timeout == 0) {
return 0;
}
if (FLAGS_timeout > 0) {
aos::TimerHandler *handle = cli_info.event_loop->AddTimer(
[event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); });
cli_info.event_loop->OnRun(
[handle, event_loop = &cli_info.event_loop.value()]() {
handle->Setup(event_loop->monotonic_now() +
std::chrono::milliseconds(FLAGS_timeout));
});
}
cli_info.event_loop->Run();
return 0;
}