• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2024 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "absl/log/internal/structured_proto.h"
17 
18 #include <cstdint>
19 
20 #include "absl/base/config.h"
21 #include "absl/log/internal/proto.h"
22 #include "absl/types/span.h"
23 #include "absl/types/variant.h"
24 
25 namespace absl {
26 ABSL_NAMESPACE_BEGIN
27 namespace log_internal {
28 
29 namespace {
30 
31 // Handles protobuf-encoding a type contained inside
32 // `StructuredProtoField::Varint`.
33 struct VarintEncoderVisitor final {
34   template <typename T>
operator ()absl::log_internal::__anon3b784a4f0111::VarintEncoderVisitor35   bool operator()(T value) const {
36     return EncodeVarint(field_number, value, &buf);
37   }
38 
39   uint64_t field_number;
40   absl::Span<char>& buf;
41 };
42 
43 // Handles protobuf-encoding a type contained inside
44 // `StructuredProtoField::I64`.
45 struct I64EncoderVisitor final {
operator ()absl::log_internal::__anon3b784a4f0111::I64EncoderVisitor46   bool operator()(uint64_t value) const {
47     return Encode64Bit(field_number, value, &buf);
48   }
49 
operator ()absl::log_internal::__anon3b784a4f0111::I64EncoderVisitor50   bool operator()(int64_t value) const {
51     return Encode64Bit(field_number, value, &buf);
52   }
53 
operator ()absl::log_internal::__anon3b784a4f0111::I64EncoderVisitor54   bool operator()(double value) const {
55     return EncodeDouble(field_number, value, &buf);
56   }
57 
58   uint64_t field_number;
59   absl::Span<char>& buf;
60 };
61 
62 // Handles protobuf-encoding a type contained inside
63 // `StructuredProtoField::I32`.
64 struct I32EncoderVisitor final {
operator ()absl::log_internal::__anon3b784a4f0111::I32EncoderVisitor65   bool operator()(uint32_t value) const {
66     return Encode32Bit(field_number, value, &buf);
67   }
68 
operator ()absl::log_internal::__anon3b784a4f0111::I32EncoderVisitor69   bool operator()(int32_t value) const {
70     return Encode32Bit(field_number, value, &buf);
71   }
72 
operator ()absl::log_internal::__anon3b784a4f0111::I32EncoderVisitor73   bool operator()(float value) const {
74     return EncodeFloat(field_number, value, &buf);
75   }
76 
77   uint64_t field_number;
78   absl::Span<char>& buf;
79 };
80 
81 // Handles protobuf-encoding a type contained inside `StructuredProtoField`.
82 struct EncoderVisitor final {
operator ()absl::log_internal::__anon3b784a4f0111::EncoderVisitor83   bool operator()(StructuredProtoField::Varint varint) {
84     return absl::visit(VarintEncoderVisitor{field_number, buf}, varint);
85   }
86 
operator ()absl::log_internal::__anon3b784a4f0111::EncoderVisitor87   bool operator()(StructuredProtoField::I64 i64) {
88     return absl::visit(I64EncoderVisitor{field_number, buf}, i64);
89   }
90 
operator ()absl::log_internal::__anon3b784a4f0111::EncoderVisitor91   bool operator()(StructuredProtoField::LengthDelimited length_delimited) {
92     // No need for a visitor, since `StructuredProtoField::LengthDelimited` is
93     // just `absl::Span<const char>`.
94     return EncodeBytes(field_number, length_delimited, &buf);
95   }
96 
operator ()absl::log_internal::__anon3b784a4f0111::EncoderVisitor97   bool operator()(StructuredProtoField::I32 i32) {
98     return absl::visit(I32EncoderVisitor{field_number, buf}, i32);
99   }
100 
101   uint64_t field_number;
102   absl::Span<char>& buf;
103 };
104 
105 }  // namespace
106 
EncodeStructuredProtoField(StructuredProtoField field,absl::Span<char> & buf)107 bool EncodeStructuredProtoField(StructuredProtoField field,
108                                 absl::Span<char>& buf) {
109   return absl::visit(EncoderVisitor{field.field_number, buf}, field.value);
110 }
111 
112 }  // namespace log_internal
113 
114 ABSL_NAMESPACE_END
115 }  // namespace absl
116