structured.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/structured.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header declares APIs supporting structured logging, allowing log
  20. // statements to be more easily parsed, especially by automated processes.
  21. //
  22. // When structured logging is in use, data streamed into a `LOG` statement are
  23. // encoded as `Value` fields in a `logging.proto.Event` protocol buffer message.
  24. // The individual data are exposed programmatically to `LogSink`s and to the
  25. // user via some log reading tools which are able to query the structured data
  26. // more usefully than would be possible if each message was a single opaque
  27. // string. These helpers allow user code to add additional structure to the
  28. // data they stream.
  29. #ifndef ABSL_LOG_STRUCTURED_H_
  30. #define ABSL_LOG_STRUCTURED_H_
  31. #include <ostream>
  32. #include "absl/base/attributes.h"
  33. #include "absl/base/config.h"
  34. #include "absl/log/internal/structured.h"
  35. #include "absl/strings/string_view.h"
  36. namespace absl {
  37. ABSL_NAMESPACE_BEGIN
  38. // LogAsLiteral()
  39. //
  40. // Annotates its argument as a string literal so that structured logging
  41. // captures it as a `literal` field instead of a `str` field (the default).
  42. // This does not affect the text representation, only the structure.
  43. //
  44. // Streaming `LogAsLiteral(s)` into a `std::ostream` behaves just like streaming
  45. // `s` directly.
  46. //
  47. // Using `LogAsLiteral()` is occasionally appropriate and useful when proxying
  48. // data logged from another system or another language. For example:
  49. //
  50. // void Logger::LogString(absl::string_view str, absl::LogSeverity severity,
  51. // const char *file, int line) {
  52. // LOG(LEVEL(severity)).AtLocation(file, line) << str;
  53. // }
  54. // void Logger::LogStringLiteral(absl::string_view str,
  55. // absl::LogSeverity severity, const char *file,
  56. // int line) {
  57. // LOG(LEVEL(severity)).AtLocation(file, line) << absl::LogAsLiteral(str);
  58. // }
  59. //
  60. // `LogAsLiteral` should only be used as a streaming operand and not, for
  61. // example, as a local variable initializer.
  62. inline log_internal::AsLiteralImpl LogAsLiteral(
  63. absl::string_view s ABSL_ATTRIBUTE_LIFETIME_BOUND) {
  64. return log_internal::AsLiteralImpl(s);
  65. }
  66. ABSL_NAMESPACE_END
  67. } // namespace absl
  68. #endif // ABSL_LOG_STRUCTURED_H_