structured.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #ifndef ABSL_LOG_INTERNAL_STRUCTURED_H_
  19. #define ABSL_LOG_INTERNAL_STRUCTURED_H_
  20. #include <ostream>
  21. #include "absl/base/config.h"
  22. #include "absl/log/internal/log_message.h"
  23. #include "absl/strings/string_view.h"
  24. namespace absl {
  25. ABSL_NAMESPACE_BEGIN
  26. namespace log_internal {
  27. class ABSL_MUST_USE_RESULT AsLiteralImpl final {
  28. public:
  29. explicit AsLiteralImpl(absl::string_view str) : str_(str) {}
  30. AsLiteralImpl(const AsLiteralImpl&) = default;
  31. AsLiteralImpl& operator=(const AsLiteralImpl&) = default;
  32. private:
  33. absl::string_view str_;
  34. friend std::ostream& operator<<(std::ostream& os, AsLiteralImpl as_literal) {
  35. return os << as_literal.str_;
  36. }
  37. void AddToMessage(log_internal::LogMessage& m) {
  38. m.CopyToEncodedBuffer<log_internal::LogMessage::StringType::kLiteral>(str_);
  39. }
  40. friend log_internal::LogMessage& operator<<(log_internal::LogMessage& m,
  41. AsLiteralImpl as_literal) {
  42. as_literal.AddToMessage(m);
  43. return m;
  44. }
  45. };
  46. } // namespace log_internal
  47. ABSL_NAMESPACE_END
  48. } // namespace absl
  49. #endif // ABSL_LOG_INTERNAL_STRUCTURED_H_