blob: 18d2ce1e47153aaad0b851e78368f3d3673622c7 [file] [log] [blame]
Tyler Chatowe6f5bef2020-10-31 14:22:04 -07001#include <unistd.h>
2
Tyler Chatow5e369a42019-11-23 11:57:31 -08003#include <iostream>
Tyler Chatow5e369a42019-11-23 11:57:31 -08004
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "gflags/gflags.h"
6
Brian Silvermanea2c95f2021-02-10 18:10:26 -08007#include "aos/aos_cli_utils.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08008#include "aos/configuration.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -08009#include "aos/init.h"
10#include "aos/json_to_flatbuffer.h"
Tyler Chatow5e369a42019-11-23 11:57:31 -080011
Austin Schuh893d7f42022-09-16 15:01:35 -070012DEFINE_int64(max_vector_size, 100,
Austin Schuhd3936202020-04-07 20:11:07 -070013 "If positive, vectors longer than this will not be printed");
Austin Schuh893d7f42022-09-16 15:01:35 -070014DEFINE_bool(json, false, "If true, print fully valid JSON");
Brian Silverman1bc2e962020-04-28 15:22:01 -070015DEFINE_bool(fetch, false,
16 "If true, fetch the current message on the channel first");
Ravago Jones5cc9df52020-09-02 21:29:58 -070017DEFINE_bool(pretty, false,
18 "If true, pretty print the messages on multiple lines");
Dan Ford44e02512022-06-07 23:45:14 -070019DEFINE_bool(
20 pretty_max, false,
21 "If true, expand every field to its own line (expands more than -pretty)");
Austin Schuh594eef82020-09-10 18:43:01 -070022DEFINE_bool(print_timestamps, true, "If true, timestamps are printed.");
23DEFINE_uint64(count, 0,
24 "If >0, aos_dump will exit after printing this many messages.");
Austin Schuh01d88fc2020-09-13 18:48:16 -070025DEFINE_int32(rate_limit, 0,
26 "The minimum amount of time to wait in milliseconds before "
27 "sending another message");
Austin Schuhe787c1d2022-06-23 10:14:30 -070028DEFINE_int32(timeout, -1,
29 "The max time in milliseconds to wait for messages before "
30 "exiting. -1 means forever, 0 means don't wait.");
Austin Schuhbe6d2962022-11-01 09:24:56 -070031DEFINE_bool(hex, false,
Naman Gupta54047212022-09-23 14:10:30 -070032 "Are integers in the messages printed in hex notation.");
Brian Silverman1bc2e962020-04-28 15:22:01 -070033
Tyler Chatow5e369a42019-11-23 11:57:31 -080034int main(int argc, char **argv) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070035 gflags::SetUsageMessage(
36 "Prints messages from arbitrary channels as they are received given a "
37 "configuration file describing the channels to listen on.\nTypical "
38 "Usage: aos_dump [--config path_to_config.json] channel_name "
39 "message_type\nExample Usage: aos_dump --config pingpong_config.json "
40 "/test aos.examples.Ping");
Tyler Chatow5e369a42019-11-23 11:57:31 -080041 aos::InitGoogle(&argc, &argv);
42
Brian Silvermanea2c95f2021-02-10 18:10:26 -080043 aos::CliUtilInfo cli_info;
Austin Schuh59f3b0f2021-07-31 20:50:40 -070044 if (cli_info.Initialize(
45 &argc, &argv,
46 [&cli_info](const aos::Channel *channel) {
47 return aos::configuration::ChannelIsReadableOnNode(
48 channel, cli_info.event_loop->node());
49 },
Austin Schuhba2c8652022-08-17 14:56:08 -070050 "channel is readeable on node", true)) {
Tyler Chatowe6f5bef2020-10-31 14:22:04 -070051 return 0;
52 }
53
Austin Schuh01d88fc2020-09-13 18:48:16 -070054 aos::monotonic_clock::time_point next_send_time =
55 aos::monotonic_clock::min_time;
Austin Schuh893d7f42022-09-16 15:01:35 -070056
Naman Gupta54047212022-09-23 14:10:30 -070057 aos::Printer printer(
58 {
59 .pretty = FLAGS_pretty,
60 .max_vector_size = static_cast<size_t>(FLAGS_max_vector_size),
61 .pretty_max = FLAGS_pretty_max,
62 .print_timestamps = FLAGS_print_timestamps,
63 .json = FLAGS_json,
64 .distributed_clock = false,
Austin Schuhbe6d2962022-11-01 09:24:56 -070065 .hex = FLAGS_hex,
Naman Gupta54047212022-09-23 14:10:30 -070066 },
67 /*flush*/ true);
68
Brian Silvermanea2c95f2021-02-10 18:10:26 -080069 for (const aos::Channel *channel : cli_info.found_channels) {
Brian Silverman83ff9c12020-06-23 16:20:27 -070070 if (FLAGS_fetch) {
71 const std::unique_ptr<aos::RawFetcher> fetcher =
Brian Silvermanea2c95f2021-02-10 18:10:26 -080072 cli_info.event_loop->MakeRawFetcher(channel);
Brian Silverman83ff9c12020-06-23 16:20:27 -070073 if (fetcher->Fetch()) {
Naman Gupta54047212022-09-23 14:10:30 -070074 printer.PrintMessage(channel, fetcher->context());
Brian Silverman83ff9c12020-06-23 16:20:27 -070075 }
76 }
77
Naman Gupta54047212022-09-23 14:10:30 -070078 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Austin Schuh594eef82020-09-10 18:43:01 -070079 return 0;
80 }
81
Austin Schuhe787c1d2022-06-23 10:14:30 -070082 if (FLAGS_timeout == 0) {
83 continue;
84 }
85
Brian Silvermanea2c95f2021-02-10 18:10:26 -080086 cli_info.event_loop->MakeRawWatcher(
Naman Gupta54047212022-09-23 14:10:30 -070087 channel, [channel, &printer, &cli_info, &next_send_time](
88 const aos::Context &context, const void * /*message*/) {
Austin Schuh01d88fc2020-09-13 18:48:16 -070089 if (context.monotonic_event_time > next_send_time) {
Naman Gupta54047212022-09-23 14:10:30 -070090 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Austin Schuh58922e52021-04-10 16:03:25 -070091 return;
92 }
Naman Gupta54047212022-09-23 14:10:30 -070093
94 printer.PrintMessage(channel, context);
Austin Schuh01d88fc2020-09-13 18:48:16 -070095 next_send_time = context.monotonic_event_time +
96 std::chrono::milliseconds(FLAGS_rate_limit);
Naman Gupta54047212022-09-23 14:10:30 -070097 if (FLAGS_count > 0 && printer.message_count() >= FLAGS_count) {
Brian Silvermanea2c95f2021-02-10 18:10:26 -080098 cli_info.event_loop->Exit();
Austin Schuh01d88fc2020-09-13 18:48:16 -070099 }
Austin Schuh594eef82020-09-10 18:43:01 -0700100 }
Tyler Chatowfcf16f42020-07-26 12:41:36 -0700101 });
Brian Silverman83ff9c12020-06-23 16:20:27 -0700102 }
103
Austin Schuhe787c1d2022-06-23 10:14:30 -0700104 if (FLAGS_timeout == 0) {
105 return 0;
106 }
107
108 if (FLAGS_timeout > 0) {
109 aos::TimerHandler *handle = cli_info.event_loop->AddTimer(
110 [event_loop = &cli_info.event_loop.value()]() { event_loop->Exit(); });
111
112 cli_info.event_loop->OnRun(
113 [handle, event_loop = &cli_info.event_loop.value()]() {
Philipp Schradera6712522023-07-05 20:25:11 -0700114 handle->Schedule(event_loop->monotonic_now() +
115 std::chrono::milliseconds(FLAGS_timeout));
Austin Schuhe787c1d2022-06-23 10:14:30 -0700116 });
117 }
118
Brian Silvermanea2c95f2021-02-10 18:10:26 -0800119 cli_info.event_loop->Run();
Austin Schuhae87e312020-08-01 16:15:01 -0700120
Tyler Chatow5e369a42019-11-23 11:57:31 -0800121 return 0;
122}