tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/log_sinks.h" |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | 55378f4 | 2018-05-25 08:23:10 | [diff] [blame] | 14 | #include <cstdio> |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 18 | #include "rtc_base/stream.h" |
tkchin | 28bae02 | 2015-07-23 19:27:02 | [diff] [blame] | 19 | |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 20 | namespace rtc { |
| 21 | |
| 22 | FileRotatingLogSink::FileRotatingLogSink(const std::string& log_dir_path, |
| 23 | const std::string& log_prefix, |
| 24 | size_t max_log_size, |
| 25 | size_t num_log_files) |
| 26 | : FileRotatingLogSink(new FileRotatingStream(log_dir_path, |
| 27 | log_prefix, |
| 28 | max_log_size, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 29 | num_log_files)) {} |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 30 | |
| 31 | FileRotatingLogSink::FileRotatingLogSink(FileRotatingStream* stream) |
| 32 | : stream_(stream) { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 33 | RTC_DCHECK(stream); |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 34 | } |
| 35 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 36 | FileRotatingLogSink::~FileRotatingLogSink() {} |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 37 | |
| 38 | void FileRotatingLogSink::OnLogMessage(const std::string& message) { |
tkchin | 28bae02 | 2015-07-23 19:27:02 | [diff] [blame] | 39 | if (stream_->GetState() != SS_OPEN) { |
Jonas Olsson | 55378f4 | 2018-05-25 08:23:10 | [diff] [blame] | 40 | std::fprintf(stderr, "Init() must be called before adding this sink.\n"); |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr); |
| 44 | } |
| 45 | |
Paulina Hensman | f1e3cb4 | 2018-06-20 12:07:05 | [diff] [blame] | 46 | void FileRotatingLogSink::OnLogMessage(const std::string& message, |
| 47 | LoggingSeverity sev, |
| 48 | const char* tag) { |
| 49 | if (stream_->GetState() != SS_OPEN) { |
| 50 | std::fprintf(stderr, "Init() must be called before adding this sink.\n"); |
| 51 | return; |
| 52 | } |
| 53 | stream_->WriteAll(tag, strlen(tag), nullptr, nullptr); |
| 54 | stream_->WriteAll(": ", 2, nullptr, nullptr); |
| 55 | stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr); |
| 56 | } |
| 57 | |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 58 | bool FileRotatingLogSink::Init() { |
| 59 | return stream_->Open(); |
| 60 | } |
| 61 | |
tkchin | 28bae02 | 2015-07-23 19:27:02 | [diff] [blame] | 62 | bool FileRotatingLogSink::DisableBuffering() { |
| 63 | return stream_->DisableBuffering(); |
| 64 | } |
| 65 | |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 66 | CallSessionFileRotatingLogSink::CallSessionFileRotatingLogSink( |
| 67 | const std::string& log_dir_path, |
| 68 | size_t max_total_log_size) |
| 69 | : FileRotatingLogSink( |
| 70 | new CallSessionFileRotatingStream(log_dir_path, max_total_log_size)) { |
| 71 | } |
| 72 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 73 | CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {} |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 74 | |
| 75 | } // namespace rtc |