/src/abseil-cpp/absl/log/internal/structured_proto.cc
Line  | Count  | Source  | 
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>  | 
35  | 0  |   bool operator()(T value) const { | 
36  | 0  |     return EncodeVarint(field_number, value, &buf);  | 
37  | 0  |   } Unexecuted instantiation: structured_proto.cc:bool absl::log_internal::(anonymous namespace)::VarintEncoderVisitor::operator()<unsigned long>(unsigned long) const Unexecuted instantiation: structured_proto.cc:bool absl::log_internal::(anonymous namespace)::VarintEncoderVisitor::operator()<long>(long) const Unexecuted instantiation: structured_proto.cc:bool absl::log_internal::(anonymous namespace)::VarintEncoderVisitor::operator()<unsigned int>(unsigned int) const Unexecuted instantiation: structured_proto.cc:bool absl::log_internal::(anonymous namespace)::VarintEncoderVisitor::operator()<int>(int) const Unexecuted instantiation: structured_proto.cc:bool absl::log_internal::(anonymous namespace)::VarintEncoderVisitor::operator()<bool>(bool) const  | 
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 { | 
46  | 0  |   bool operator()(uint64_t value) const { | 
47  | 0  |     return Encode64Bit(field_number, value, &buf);  | 
48  | 0  |   }  | 
49  |  |  | 
50  | 0  |   bool operator()(int64_t value) const { | 
51  | 0  |     return Encode64Bit(field_number, value, &buf);  | 
52  | 0  |   }  | 
53  |  |  | 
54  | 0  |   bool operator()(double value) const { | 
55  | 0  |     return EncodeDouble(field_number, value, &buf);  | 
56  | 0  |   }  | 
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 { | 
65  | 0  |   bool operator()(uint32_t value) const { | 
66  | 0  |     return Encode32Bit(field_number, value, &buf);  | 
67  | 0  |   }  | 
68  |  |  | 
69  | 0  |   bool operator()(int32_t value) const { | 
70  | 0  |     return Encode32Bit(field_number, value, &buf);  | 
71  | 0  |   }  | 
72  |  |  | 
73  | 0  |   bool operator()(float value) const { | 
74  | 0  |     return EncodeFloat(field_number, value, &buf);  | 
75  | 0  |   }  | 
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 { | 
83  | 0  |   bool operator()(StructuredProtoField::Varint varint) { | 
84  | 0  |     return absl::visit(VarintEncoderVisitor{field_number, buf}, varint); | 
85  | 0  |   }  | 
86  |  |  | 
87  | 0  |   bool operator()(StructuredProtoField::I64 i64) { | 
88  | 0  |     return absl::visit(I64EncoderVisitor{field_number, buf}, i64); | 
89  | 0  |   }  | 
90  |  |  | 
91  | 0  |   bool operator()(StructuredProtoField::LengthDelimited length_delimited) { | 
92  |  |     // No need for a visitor, since `StructuredProtoField::LengthDelimited` is  | 
93  |  |     // just `absl::Span<const char>`.  | 
94  | 0  |     return EncodeBytes(field_number, length_delimited, &buf);  | 
95  | 0  |   }  | 
96  |  |  | 
97  | 0  |   bool operator()(StructuredProtoField::I32 i32) { | 
98  | 0  |     return absl::visit(I32EncoderVisitor{field_number, buf}, i32); | 
99  | 0  |   }  | 
100  |  |  | 
101  |  |   uint64_t field_number;  | 
102  |  |   absl::Span<char>& buf;  | 
103  |  | };  | 
104  |  |  | 
105  |  | }  // namespace  | 
106  |  |  | 
107  |  | bool EncodeStructuredProtoField(StructuredProtoField field,  | 
108  | 0  |                                 absl::Span<char>& buf) { | 
109  | 0  |   return absl::visit(EncoderVisitor{field.field_number, buf}, field.value); | 
110  | 0  | }  | 
111  |  |  | 
112  |  | }  // namespace log_internal  | 
113  |  |  | 
114  |  | ABSL_NAMESPACE_END  | 
115  |  | }  // namespace absl  |