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