xref: /aosp_15_r20/external/webrtc/rtc_base/log_sinks.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef RTC_BASE_LOG_SINKS_H_
12 #define RTC_BASE_LOG_SINKS_H_
13 
14 #include <stddef.h>
15 
16 #include <memory>
17 #include <string>
18 
19 #include "absl/strings/string_view.h"
20 #include "rtc_base/file_rotating_stream.h"
21 #include "rtc_base/logging.h"
22 
23 namespace rtc {
24 
25 // Log sink that uses a FileRotatingStream to write to disk.
26 // Init() must be called before adding this sink.
27 class FileRotatingLogSink : public LogSink {
28  public:
29   // `num_log_files` must be greater than 1 and `max_log_size` must be greater
30   // than 0.
31   FileRotatingLogSink(absl::string_view log_dir_path,
32                       absl::string_view log_prefix,
33                       size_t max_log_size,
34                       size_t num_log_files);
35   ~FileRotatingLogSink() override;
36 
37   FileRotatingLogSink(const FileRotatingLogSink&) = delete;
38   FileRotatingLogSink& operator=(const FileRotatingLogSink&) = delete;
39 
40   // Writes the message to the current file. It will spill over to the next
41   // file if needed.
42   void OnLogMessage(const std::string& message) override;
43   void OnLogMessage(absl::string_view message) override;
44   void OnLogMessage(const std::string& message,
45                     LoggingSeverity sev,
46                     const char* tag) override;
47   void OnLogMessage(absl::string_view message,
48                     LoggingSeverity sev,
49                     const char* tag) override;
50 
51   // Deletes any existing files in the directory and creates a new log file.
52   virtual bool Init();
53 
54   // Disables buffering on the underlying stream.
55   bool DisableBuffering();
56 
57  protected:
58   explicit FileRotatingLogSink(FileRotatingStream* stream);
59 
60  private:
61   std::unique_ptr<FileRotatingStream> stream_;
62 };
63 
64 // Log sink that uses a CallSessionFileRotatingStream to write to disk.
65 // Init() must be called before adding this sink.
66 class CallSessionFileRotatingLogSink : public FileRotatingLogSink {
67  public:
68   CallSessionFileRotatingLogSink(absl::string_view log_dir_path,
69                                  size_t max_total_log_size);
70   ~CallSessionFileRotatingLogSink() override;
71 
72   CallSessionFileRotatingLogSink(const CallSessionFileRotatingLogSink&) =
73       delete;
74   CallSessionFileRotatingLogSink& operator=(
75       const CallSessionFileRotatingLogSink&) = delete;
76 };
77 
78 }  // namespace rtc
79 
80 #endif  // RTC_BASE_LOG_SINKS_H_
81