log_sink.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/log_sink.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header declares the interface class `absl::LogSink`.
  20. #ifndef ABSL_LOG_LOG_SINK_H_
  21. #define ABSL_LOG_LOG_SINK_H_
  22. #include "absl/base/config.h"
  23. #include "absl/log/log_entry.h"
  24. namespace absl {
  25. ABSL_NAMESPACE_BEGIN
  26. // absl::LogSink
  27. //
  28. // `absl::LogSink` is an interface which can be extended to intercept and
  29. // process particular messages (with `LOG.ToSinkOnly()` or
  30. // `LOG.ToSinkAlso()`) or all messages (if registered with
  31. // `absl::AddLogSink`). Implementations must be thread-safe, and should take
  32. // care not to take any locks that might be held by the `LOG` caller.
  33. class LogSink {
  34. public:
  35. virtual ~LogSink() = default;
  36. // LogSink::Send()
  37. //
  38. // `Send` is called synchronously during the log statement.
  39. //
  40. // It is safe to use `LOG` within an implementation of `Send`. `ToSinkOnly`
  41. // and `ToSinkAlso` are safe in general but can be used to create an infinite
  42. // loop if you try.
  43. virtual void Send(const absl::LogEntry& entry) = 0;
  44. // LogSink::Flush()
  45. //
  46. // Sinks that buffer messages should override this method to flush the buffer
  47. // and return.
  48. virtual void Flush() {}
  49. private:
  50. // https://lld.llvm.org/missingkeyfunction.html#missing-key-function
  51. virtual void KeyFunction() const final; // NOLINT(readability/inheritance)
  52. };
  53. ABSL_NAMESPACE_END
  54. } // namespace absl
  55. #endif // ABSL_LOG_LOG_SINK_H_