1
#pragma once
2

            
3
#include <bitset>
4
#include <functional>
5
#include <list>
6
#include <regex>
7
#include <string>
8
#include <vector>
9

            
10
#include "envoy/common/time.h"
11
#include "envoy/config/core/v3/base.pb.h"
12
#include "envoy/formatter/substitution_formatter.h"
13
#include "envoy/stream_info/stream_info.h"
14

            
15
#include "source/common/common/utility.h"
16
#include "source/common/formatter/http_formatter_context.h"
17
#include "source/common/json/json_loader.h"
18
#include "source/common/json/json_streamer.h"
19
#include "source/common/json/json_utility.h"
20

            
21
#include "absl/types/optional.h"
22
#include "re2/re2.h"
23

            
24
namespace Envoy {
25
namespace Formatter {
26

            
27
/**
28
 * FormatterProvider for string literals. It ignores headers and stream info and returns string by
29
 * which it was initialized.
30
 */
31
class PlainStringFormatter : public FormatterProvider {
32
public:
33
311634
  PlainStringFormatter(absl::string_view str) { str_.set_string_value(str); }
34

            
35
  // FormatterProvider
36
1165102
  absl::optional<std::string> format(const Context&, const StreamInfo::StreamInfo&) const override {
37
1165102
    return str_.string_value();
38
1165102
  }
39
11
  Protobuf::Value formatValue(const Context&, const StreamInfo::StreamInfo&) const override {
40
11
    return str_;
41
11
  }
42

            
43
private:
44
  Protobuf::Value str_;
45
};
46

            
47
/**
48
 * FormatterProvider for numbers.
49
 */
50
class PlainNumberFormatter : public FormatterProvider {
51
public:
52
1
  PlainNumberFormatter(double num) { num_.set_number_value(num); }
53

            
54
  // FormatterProvider
55
1
  absl::optional<std::string> format(const Context&, const StreamInfo::StreamInfo&) const override {
56
1
    std::string str = absl::StrFormat("%g", num_.number_value());
57
1
    return str;
58
1
  }
59
1
  Protobuf::Value formatValue(const Context&, const StreamInfo::StreamInfo&) const override {
60
1
    return num_;
61
1
  }
62

            
63
private:
64
  Protobuf::Value num_;
65
};
66

            
67
/**
68
 * Access log format parser.
69
 */
70
class SubstitutionFormatParser {
71
public:
72
  static absl::StatusOr<std::vector<FormatterProviderPtr>>
73
  parse(absl::string_view format, const std::vector<CommandParserPtr>& command_parsers = {});
74
};
75

            
76
inline constexpr absl::string_view DefaultUnspecifiedValueStringView = "-";
77

            
78
/**
79
 * Composite formatter implementation.
80
 */
81
class FormatterImpl : public Formatter {
82
public:
83
  using CommandParsers = std::vector<CommandParserPtr>;
84

            
85
  static absl::StatusOr<std::unique_ptr<FormatterImpl>>
86
  create(absl::string_view format, bool omit_empty_values = false,
87
         const CommandParsers& command_parsers = {});
88

            
89
  // Formatter
90
  std::string format(const Context& context,
91
                     const StreamInfo::StreamInfo& stream_info) const override;
92

            
93
protected:
94
  FormatterImpl(absl::Status& creation_status, absl::string_view format,
95
                bool omit_empty_values = false, const CommandParsers& command_parsers = {})
96
25278
      : omit_empty_values_(omit_empty_values) {
97
25278
    auto providers_or_error = SubstitutionFormatParser::parse(format, command_parsers);
98
25278
    SET_AND_RETURN_IF_NOT_OK(providers_or_error.status(), creation_status);
99
25223
    providers_ = std::move(*providers_or_error);
100
25223
  }
101

            
102
private:
103
  const bool omit_empty_values_;
104
  std::vector<FormatterProviderPtr> providers_;
105
};
106

            
107
class JsonFormatterImpl : public Formatter {
108
public:
109
  using CommandParsers = std::vector<CommandParserPtr>;
110
  using Formatter = FormatterProviderPtr;
111
  using Formatters = std::vector<Formatter>;
112

            
113
  JsonFormatterImpl(const Protobuf::Struct& struct_format, bool omit_empty_values,
114
                    const CommandParsers& commands = {});
115

            
116
  // Formatter
117
  std::string format(const Context& context, const StreamInfo::StreamInfo& info) const override;
118

            
119
private:
120
  const bool omit_empty_values_;
121
  using ParsedFormatElement = absl::variant<std::string, Formatters>;
122
  std::vector<ParsedFormatElement> parsed_elements_;
123
};
124

            
125
} // namespace Formatter
126
} // namespace Envoy