blob: 980f94f2aa1f98a776224e2ae6b9c8146467c2b9 [file] [log] [blame]
Brian Silvermanea2c95f2021-02-10 18:10:26 -08001#ifndef AOS_AOS_CLI_UTILS_H_
2#define AOS_AOS_CLI_UTILS_H_
3
Austin Schuh99f7c6a2024-06-25 22:07:44 -07004#include "absl/flags/flag.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07005
Brian Silvermanea2c95f2021-02-10 18:10:26 -08006#include "aos/configuration.h"
7#include "aos/events/shm_event_loop.h"
Austin Schuh893d7f42022-09-16 15:01:35 -07008#include "aos/events/simulated_event_loop.h"
Brian Silvermanea2c95f2021-02-10 18:10:26 -08009
10namespace aos {
11
Austin Schuh893d7f42022-09-16 15:01:35 -070012struct PrintOptions {
13 // Format the JSON with the pretty option.
14 bool pretty;
15 // Max vector size to skip expanding.
16 size_t max_vector_size;
17 // Put everything on a separate line instead of keeping small messages
18 // together.
19 bool pretty_max;
20 // Print the timestamps.
21 bool print_timestamps;
22 // Make everything JSON compliant.
23 bool json;
24 // Print the distributed clock.
25 bool distributed_clock;
26 // Print numbers out in hex.
Austin Schuhbe6d2962022-11-01 09:24:56 -070027 bool hex;
Austin Schuh893d7f42022-09-16 15:01:35 -070028};
29
30// Print the flatbuffer out to stdout, both to remove the unnecessary cruft from
31// glog and to allow the user to readily redirect just the logged output
32// independent of any debugging information on stderr.
33void PrintMessage(const std::string_view node_name,
34 aos::NodeEventLoopFactory *node_factory,
35 const aos::Channel *channel, const aos::Context &context,
36 aos::FastStringBuilder *builder, PrintOptions options);
37
38void PrintMessage(const aos::Channel *channel, const aos::Context &context,
39 aos::FastStringBuilder *builder, PrintOptions options);
40
Naman Gupta54047212022-09-23 14:10:30 -070041// RAII class to manage printing out sequences of messages, and to print them
42// all in a JSON array properly.
43class Printer {
44 public:
45 Printer(PrintOptions options, bool flush);
46 ~Printer();
47
48 // Number of messages that have been printed.
49 uint64_t message_count() const { return message_count_; }
50
51 // Prints a message.
52 void PrintMessage(const std::string_view node_name,
53 aos::NodeEventLoopFactory *node_factory,
54 const aos::Channel *channel, const aos::Context &context);
55 void PrintMessage(const aos::Channel *channel, const aos::Context &context);
56
57 private:
58 // Builder to make printing fast
59 aos::FastStringBuilder str_builder_;
60
61 // Number of messages
62 uint64_t message_count_ = 0;
63
64 // Options for printing.
65 const PrintOptions options_;
66
67 // If true, use std::endl to flush stdout, otherwise write a "\n"
68 const bool flush_;
69};
70
Brian Silvermanea2c95f2021-02-10 18:10:26 -080071// The information needed by the main function of a CLI tool.
72struct CliUtilInfo {
73 // If this returns true, main should return immediately with 0.
74 // If this returns false, the other fields will be filled out appropriately.
75 // event_loop will be filled out before channel_filter is called.
76 bool Initialize(int *argc, char ***argv,
Austin Schuh59f3b0f2021-07-31 20:50:40 -070077 std::function<bool(const aos::Channel *)> channel_filter,
Austin Schuhba2c8652022-08-17 14:56:08 -070078 std::string_view channel_filter_description,
Austin Schuh59f3b0f2021-07-31 20:50:40 -070079 bool expect_args);
Brian Silvermanea2c95f2021-02-10 18:10:26 -080080
81 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config;
82 std::optional<aos::ShmEventLoop> event_loop;
83 std::vector<const aos::Channel *> found_channels;
84
85 private:
86 // Generate eval command to populate autocomplete responses. Eval escapes
87 // spaces so channels are paired with their types. If a complete channel name
88 // is found, only autocompletes the type to avoid repeating arguments. Returns
89 // no autocomplete suggestions if a channel and type is found with the current
90 // arguments.
91 void Autocomplete(std::string_view channel_name,
92 std::string_view message_type,
93 std::function<bool(const aos::Channel *)> channel_filter);
94
95 void ShiftArgs(int *argc, char ***argv) {
96 for (int i = 1; i + 1 < *argc; ++i) {
97 (*argv)[i] = (*argv)[i + 1];
98 }
99 --*argc;
100 }
101};
102
103} // namespace aos
104
105#endif // AOS_AOS_CLI_UTILS_H_