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 "rtc_base/checks.h"
19 #include "rtc_base/stream.h"
20 
21 namespace rtc {
22 
FileRotatingLogSink(const std::string & log_dir_path,const std::string & log_prefix,size_t max_log_size,size_t num_log_files)23 FileRotatingLogSink::FileRotatingLogSink(const std::string& log_dir_path,
24                                          const std::string& 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   if (stream_->GetState() != SS_OPEN) {
41     std::fprintf(stderr, "Init() must be called before adding this sink.\n");
42     return;
43   }
44   stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr);
45 }
46 
OnLogMessage(const std::string & message,LoggingSeverity sev,const char * tag)47 void FileRotatingLogSink::OnLogMessage(const std::string& message,
48                                        LoggingSeverity sev,
49                                        const char* tag) {
50   if (stream_->GetState() != SS_OPEN) {
51     std::fprintf(stderr, "Init() must be called before adding this sink.\n");
52     return;
53   }
54   stream_->WriteAll(tag, strlen(tag), nullptr, nullptr);
55   stream_->WriteAll(": ", 2, nullptr, nullptr);
56   stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr);
57 }
58 
Init()59 bool FileRotatingLogSink::Init() {
60   return stream_->Open();
61 }
62 
DisableBuffering()63 bool FileRotatingLogSink::DisableBuffering() {
64   return stream_->DisableBuffering();
65 }
66 
CallSessionFileRotatingLogSink(const std::string & log_dir_path,size_t max_total_log_size)67 CallSessionFileRotatingLogSink::CallSessionFileRotatingLogSink(
68     const std::string& log_dir_path,
69     size_t max_total_log_size)
70     : FileRotatingLogSink(
71           new CallSessionFileRotatingStream(log_dir_path, max_total_log_size)) {
72 }
73 
~CallSessionFileRotatingLogSink()74 CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {}
75 
76 }  // namespace rtc
77