xref: /aosp_15_r20/external/cronet/third_party/abseil-cpp/absl/log/log_sink.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
21 #ifndef ABSL_LOG_LOG_SINK_H_
22 #define ABSL_LOG_LOG_SINK_H_
23 
24 #include "absl/base/config.h"
25 #include "absl/log/log_entry.h"
26 
27 namespace absl {
28 ABSL_NAMESPACE_BEGIN
29 
30 // absl::LogSink
31 //
32 // `absl::LogSink` is an interface which can be extended to intercept and
33 // process particular messages (with `LOG.ToSinkOnly()` or
34 // `LOG.ToSinkAlso()`) or all messages (if registered with
35 // `absl::AddLogSink`).  Implementations must not take any locks that might be
36 // held by the `LOG` caller.
37 class LogSink {
38  public:
39   virtual ~LogSink() = default;
40 
41   // LogSink::Send()
42   //
43   // `Send` is called synchronously during the log statement.  `Send` must be
44   // thread-safe.
45   //
46   // It is safe to use `LOG` within an implementation of `Send`.  `ToSinkOnly`
47   // and `ToSinkAlso` are safe in general but can be used to create an infinite
48   // loop if you try.
49   virtual void Send(const absl::LogEntry& entry) = 0;
50 
51   // LogSink::Flush()
52   //
53   // Sinks that buffer messages should override this method to flush the buffer
54   // and return.  `Flush` must be thread-safe.
Flush()55   virtual void Flush() {}
56 
57  protected:
58   LogSink() = default;
59   // Implementations may be copyable and/or movable.
60   LogSink(const LogSink&) = default;
61   LogSink& operator=(const LogSink&) = default;
62 
63  private:
64   // https://lld.llvm.org/missingkeyfunction.html#missing-key-function
65   virtual void KeyFunction() const final;  // NOLINT(readability/inheritance)
66 };
67 
68 ABSL_NAMESPACE_END
69 }  // namespace absl
70 
71 #endif  // ABSL_LOG_LOG_SINK_H_
72