tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [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 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/log_sinks.h" |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 15 | #include <cstdio> |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 19 | #include "rtc_base/stream.h" |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 20 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 21 | namespace rtc { |
| 22 | |
| 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, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | num_log_files)) {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 31 | |
| 32 | FileRotatingLogSink::FileRotatingLogSink(FileRotatingStream* stream) |
| 33 | : stream_(stream) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 34 | RTC_DCHECK(stream); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | FileRotatingLogSink::~FileRotatingLogSink() {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 38 | |
| 39 | void FileRotatingLogSink::OnLogMessage(const std::string& message) { |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 40 | if (stream_->GetState() != SS_OPEN) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 41 | std::fprintf(stderr, "Init() must be called before adding this sink.\n"); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr); |
| 45 | } |
| 46 | |
Paulina Hensman | f1e3cb4 | 2018-06-20 14:07:05 +0200 | [diff] [blame] | 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 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 59 | bool FileRotatingLogSink::Init() { |
| 60 | return stream_->Open(); |
| 61 | } |
| 62 | |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 63 | bool FileRotatingLogSink::DisableBuffering() { |
| 64 | return stream_->DisableBuffering(); |
| 65 | } |
| 66 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 74 | CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 75 | |
| 76 | } // namespace rtc |