xref: /aosp_15_r20/external/webrtc/rtc_base/log_sinks.cc (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 #include "rtc_base/log_sinks.h"
12 
13 #include <string.h>
14 
15 #include <cstdio>
16 #include <string>
17 
18 #include "absl/strings/string_view.h"
19 #include "rtc_base/checks.h"
20 
21 namespace rtc {
22 
FileRotatingLogSink(absl::string_view log_dir_path,absl::string_view log_prefix,size_t max_log_size,size_t num_log_files)23 FileRotatingLogSink::FileRotatingLogSink(absl::string_view log_dir_path,
24                                          absl::string_view log_prefix,
25                                          size_t max_log_size,
26                                          size_t num_log_files)
27     : FileRotatingLogSink(new FileRotatingStream(log_dir_path,
28                                                  log_prefix,
29                                                  max_log_size,
30                                                  num_log_files)) {}
31 
FileRotatingLogSink(FileRotatingStream * stream)32 FileRotatingLogSink::FileRotatingLogSink(FileRotatingStream* stream)
33     : stream_(stream) {
34   RTC_DCHECK(stream);
35 }
36 
~FileRotatingLogSink()37 FileRotatingLogSink::~FileRotatingLogSink() {}
38 
OnLogMessage(const std::string & message)39 void FileRotatingLogSink::OnLogMessage(const std::string& message) {
40   OnLogMessage(absl::string_view(message));
41 }
42 
OnLogMessage(absl::string_view message)43 void FileRotatingLogSink::OnLogMessage(absl::string_view message) {
44   if (!stream_->IsOpen()) {
45     std::fprintf(stderr, "Init() must be called before adding this sink.\n");
46     return;
47   }
48   stream_->Write(message.data(), message.size());
49 }
50 
OnLogMessage(const std::string & message,LoggingSeverity sev,const char * tag)51 void FileRotatingLogSink::OnLogMessage(const std::string& message,
52                                        LoggingSeverity sev,
53                                        const char* tag) {
54   OnLogMessage(absl::string_view(message), sev, tag);
55 }
56 
OnLogMessage(absl::string_view message,LoggingSeverity sev,const char * tag)57 void FileRotatingLogSink::OnLogMessage(absl::string_view message,
58                                        LoggingSeverity sev,
59                                        const char* tag) {
60   if (!stream_->IsOpen()) {
61     std::fprintf(stderr, "Init() must be called before adding this sink.\n");
62     return;
63   }
64   stream_->Write(tag, strlen(tag));
65   stream_->Write(": ", 2);
66   stream_->Write(message.data(), message.size());
67 }
68 
Init()69 bool FileRotatingLogSink::Init() {
70   return stream_->Open();
71 }
72 
DisableBuffering()73 bool FileRotatingLogSink::DisableBuffering() {
74   return stream_->DisableBuffering();
75 }
76 
CallSessionFileRotatingLogSink(absl::string_view log_dir_path,size_t max_total_log_size)77 CallSessionFileRotatingLogSink::CallSessionFileRotatingLogSink(
78     absl::string_view log_dir_path,
79     size_t max_total_log_size)
80     : FileRotatingLogSink(
81           new CallSessionFileRotatingStream(log_dir_path, max_total_log_size)) {
82 }
83 
~CallSessionFileRotatingLogSink()84 CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {}
85 
86 }  // namespace rtc
87