xref: /aosp_15_r20/external/abseil-cpp/absl/log/internal/structured.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 // Copyright 2022 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: log/internal/structured.h
17 // -----------------------------------------------------------------------------
18 
19 #ifndef ABSL_LOG_INTERNAL_STRUCTURED_H_
20 #define ABSL_LOG_INTERNAL_STRUCTURED_H_
21 
22 #include <ostream>
23 
24 #include "absl/base/config.h"
25 #include "absl/log/internal/log_message.h"
26 #include "absl/strings/string_view.h"
27 
28 namespace absl {
29 ABSL_NAMESPACE_BEGIN
30 namespace log_internal {
31 
32 class ABSL_MUST_USE_RESULT AsLiteralImpl final {
33  public:
AsLiteralImpl(absl::string_view str)34   explicit AsLiteralImpl(absl::string_view str) : str_(str) {}
35   AsLiteralImpl(const AsLiteralImpl&) = default;
36   AsLiteralImpl& operator=(const AsLiteralImpl&) = default;
37 
38  private:
39   absl::string_view str_;
40 
41   friend std::ostream& operator<<(std::ostream& os, AsLiteralImpl as_literal) {
42     return os << as_literal.str_;
43   }
AddToMessage(log_internal::LogMessage & m)44   void AddToMessage(log_internal::LogMessage& m) {
45     m.CopyToEncodedBuffer<log_internal::LogMessage::StringType::kLiteral>(str_);
46   }
47   friend log_internal::LogMessage& operator<<(log_internal::LogMessage& m,
48                                               AsLiteralImpl as_literal) {
49     as_literal.AddToMessage(m);
50     return m;
51   }
52 };
53 
54 }  // namespace log_internal
55 ABSL_NAMESPACE_END
56 }  // namespace absl
57 
58 #endif  // ABSL_LOG_INTERNAL_STRUCTURED_H_
59