log_sink.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 not take any locks that might be
  32. // 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. `Send` must be
  39. // thread-safe.
  40. //
  41. // It is safe to use `LOG` within an implementation of `Send`. `ToSinkOnly`
  42. // and `ToSinkAlso` are safe in general but can be used to create an infinite
  43. // loop if you try.
  44. virtual void Send(const absl::LogEntry& entry) = 0;
  45. // LogSink::Flush()
  46. //
  47. // Sinks that buffer messages should override this method to flush the buffer
  48. // and return. `Flush` must be thread-safe.
  49. virtual void Flush() {}
  50. protected:
  51. LogSink() = default;
  52. // Implementations may be copyable and/or movable.
  53. LogSink(const LogSink&) = default;
  54. LogSink& operator=(const LogSink&) = default;
  55. private:
  56. // https://lld.llvm.org/missingkeyfunction.html#missing-key-function
  57. virtual void KeyFunction() const final; // NOLINT(readability/inheritance)
  58. };
  59. ABSL_NAMESPACE_END
  60. } // namespace absl
  61. #endif // ABSL_LOG_LOG_SINK_H_