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