Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 1 | #ifndef AOS_AOS_CLI_UTILS_H_ |
| 2 | #define AOS_AOS_CLI_UTILS_H_ |
| 3 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | #include "gflags/gflags.h" |
| 5 | |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 6 | #include "aos/configuration.h" |
| 7 | #include "aos/events/shm_event_loop.h" |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 8 | #include "aos/events/simulated_event_loop.h" |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 12 | struct 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 Schuh | be6d296 | 2022-11-01 09:24:56 -0700 | [diff] [blame] | 27 | bool hex; |
Austin Schuh | 893d7f4 | 2022-09-16 15:01:35 -0700 | [diff] [blame] | 28 | }; |
| 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. |
| 33 | void 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 | |
| 38 | void PrintMessage(const aos::Channel *channel, const aos::Context &context, |
| 39 | aos::FastStringBuilder *builder, PrintOptions options); |
| 40 | |
Naman Gupta | 5404721 | 2022-09-23 14:10:30 -0700 | [diff] [blame] | 41 | // RAII class to manage printing out sequences of messages, and to print them |
| 42 | // all in a JSON array properly. |
| 43 | class 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 Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 71 | // The information needed by the main function of a CLI tool. |
| 72 | struct 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 Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 77 | std::function<bool(const aos::Channel *)> channel_filter, |
Austin Schuh | ba2c865 | 2022-08-17 14:56:08 -0700 | [diff] [blame] | 78 | std::string_view channel_filter_description, |
Austin Schuh | 59f3b0f | 2021-07-31 20:50:40 -0700 | [diff] [blame] | 79 | bool expect_args); |
Brian Silverman | ea2c95f | 2021-02-10 18:10:26 -0800 | [diff] [blame] | 80 | |
| 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_ |