Line data Source code
1 : #pragma once 2 : 3 : #include <memory> 4 : #include <string> 5 : 6 : #include "envoy/access_log/access_log.h" 7 : #include "envoy/common/pure.h" 8 : #include "envoy/config/typed_config.h" 9 : #include "envoy/server/factory_context.h" 10 : #include "envoy/stream_info/stream_info.h" 11 : 12 : namespace Envoy { 13 : namespace Formatter { 14 : 15 : /** 16 : * Template interface for multiple protocols/modules formatters. 17 : */ 18 : template <class FormatterContext> class FormatterBase { 19 : public: 20 983 : virtual ~FormatterBase() = default; 21 : 22 : /** 23 : * Return a formatted substitution line. 24 : * @param context supplies the formatter context. 25 : * @param stream_info supplies the stream info. 26 : * @return std::string string containing the complete formatted substitution line. 27 : */ 28 : virtual std::string formatWithContext(const FormatterContext& context, 29 : const StreamInfo::StreamInfo& stream_info) const PURE; 30 : }; 31 : 32 : template <class FormatterContext> 33 : using FormatterBasePtr = std::unique_ptr<FormatterBase<FormatterContext>>; 34 : 35 : /** 36 : * Template interface for multiple protocols/modules formatter providers. 37 : */ 38 : template <class FormatterContext> class FormatterProviderBase { 39 : public: 40 7170 : virtual ~FormatterProviderBase() = default; 41 : 42 : /** 43 : * Format the value with the given context and stream info. 44 : * @param context supplies the formatter context. 45 : * @param stream_info supplies the stream info. 46 : * @return absl::optional<std::string> optional string containing a single value extracted from 47 : * the given context and stream info. 48 : */ 49 : virtual absl::optional<std::string> 50 : formatWithContext(const FormatterContext& context, 51 : const StreamInfo::StreamInfo& stream_info) const PURE; 52 : 53 : /** 54 : * Format the value with the given context and stream info. 55 : * @param context supplies the formatter context. 56 : * @param stream_info supplies the stream info. 57 : * @return ProtobufWkt::Value containing a single value extracted from the given 58 : * context and stream info. 59 : */ 60 : virtual ProtobufWkt::Value 61 : formatValueWithContext(const FormatterContext& context, 62 : const StreamInfo::StreamInfo& stream_info) const PURE; 63 : }; 64 : 65 : template <class FormatterContext> 66 : using FormatterProviderBasePtr = std::unique_ptr<FormatterProviderBase<FormatterContext>>; 67 : 68 : template <class FormatterContext> class CommandParserBase { 69 : public: 70 0 : virtual ~CommandParserBase() = default; 71 : 72 : /** 73 : * Return a FormatterProviderBasePtr if command arg and max_length are correct for the formatter 74 : * provider associated with command. 75 : * @param command command name. 76 : * @param command_arg command specific argument. Empty if no argument is provided. 77 : * @param max_length length to which the output produced by FormatterProvider 78 : * should be truncated to (optional). 79 : * 80 : * @return FormattterProviderPtr substitution provider for the parsed command. 81 : */ 82 : virtual FormatterProviderBasePtr<FormatterContext> 83 : parse(const std::string& command, const std::string& command_arg, 84 : absl::optional<size_t>& max_length) const PURE; 85 : }; 86 : 87 : template <class FormatterContext> 88 : using CommandParserBasePtr = std::unique_ptr<CommandParserBase<FormatterContext>>; 89 : 90 : template <class FormatterContext> 91 : using CommandParsersBase = std::vector<CommandParserBasePtr<FormatterContext>>; 92 : 93 : template <class FormatterContext> class CommandParserFactoryBase : public Config::TypedFactory { 94 : public: 95 : /** 96 : * Creates a particular CommandParser implementation. 97 : * 98 : * @param config supplies the configuration for the command parser. 99 : * @param context supplies the factory context. 100 : * @return CommandParserPtr the CommandParser which will be used in 101 : * SubstitutionFormatParser::parse() when evaluating an access log format string. 102 : */ 103 : virtual CommandParserBasePtr<FormatterContext> 104 : createCommandParserFromProto(const Protobuf::Message& config, 105 : Server::Configuration::GenericFactoryContext& context) PURE; 106 : 107 : std::string category() const override { 108 : return fmt::format("envoy.{}.formatters", FormatterContext::category()); 109 : } 110 : }; 111 : 112 : template <class FormatterContext> class BuiltInCommandParsersBase { 113 : public: 114 57 : static void addCommandParser(CommandParserBasePtr<FormatterContext> parser) { 115 57 : mutableCommandParsers().push_back(std::move(parser)); 116 57 : } 117 : 118 3998 : static const CommandParsersBase<FormatterContext>& commandParsers() { 119 3998 : return mutableCommandParsers(); 120 3998 : } 121 : 122 : private: 123 4055 : static CommandParsersBase<FormatterContext>& mutableCommandParsers() { 124 4055 : MUTABLE_CONSTRUCT_ON_FIRST_USE(CommandParsersBase<FormatterContext>); 125 4055 : } 126 : }; 127 : 128 : template <class FormatterContext> struct BuiltInCommandPaserRegister { 129 57 : BuiltInCommandPaserRegister(CommandParserBasePtr<FormatterContext> parser) { 130 57 : BuiltInCommandParsersBase<FormatterContext>::addCommandParser(std::move(parser)); 131 57 : } 132 : }; 133 : 134 : #define REGISTER_BUILT_IN_COMMAND_PARSER(context, parser) \ 135 : static BuiltInCommandPaserRegister<context> register_##context##_##parser{ \ 136 : std::make_unique<parser>()}; 137 : 138 : } // namespace Formatter 139 : } // namespace Envoy