blob: f511948ed32cacd6da4b694651fe54dec73a5b03 [file] [log] [blame]
tkchin93411912015-07-22 12:12:17 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/log_sinks.h"
tkchin93411912015-07-22 12:12:17 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Jonas Olsson55378f42018-05-25 10:23:10 +020015#include <cstdio>
tkchin93411912015-07-22 12:12:17 -070016#include <string>
17
Ali Tofigh6364d082022-03-14 13:32:04 +010018#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
tkchin28bae022015-07-23 12:27:02 -070020
tkchin93411912015-07-22 12:12:17 -070021namespace rtc {
22
Ali Tofigh2ab914c2022-04-13 12:55:15 +020023FileRotatingLogSink::FileRotatingLogSink(absl::string_view log_dir_path,
24 absl::string_view log_prefix,
tkchin93411912015-07-22 12:12:17 -070025 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 Gerey665174f2018-06-19 15:03:05 +020030 num_log_files)) {}
tkchin93411912015-07-22 12:12:17 -070031
32FileRotatingLogSink::FileRotatingLogSink(FileRotatingStream* stream)
33 : stream_(stream) {
henrikg91d6ede2015-09-17 00:24:34 -070034 RTC_DCHECK(stream);
tkchin93411912015-07-22 12:12:17 -070035}
36
Yves Gerey665174f2018-06-19 15:03:05 +020037FileRotatingLogSink::~FileRotatingLogSink() {}
tkchin93411912015-07-22 12:12:17 -070038
39void FileRotatingLogSink::OnLogMessage(const std::string& message) {
Ali Tofigh6364d082022-03-14 13:32:04 +010040 OnLogMessage(absl::string_view(message));
41}
42
43void FileRotatingLogSink::OnLogMessage(absl::string_view message) {
Niels Möllera9311b62021-03-25 15:29:02 +010044 if (!stream_->IsOpen()) {
Jonas Olsson55378f42018-05-25 10:23:10 +020045 std::fprintf(stderr, "Init() must be called before adding this sink.\n");
tkchin93411912015-07-22 12:12:17 -070046 return;
47 }
Ali Tofigh6364d082022-03-14 13:32:04 +010048 stream_->Write(message.data(), message.size());
tkchin93411912015-07-22 12:12:17 -070049}
50
Ali Tofigh8fcc79b2022-04-29 22:39:20 +020051void FileRotatingLogSink::OnLogMessage(const std::string& message,
52 LoggingSeverity sev,
53 const char* tag) {
54 OnLogMessage(absl::string_view(message), sev, tag);
55}
56
Ali Tofigh6364d082022-03-14 13:32:04 +010057void FileRotatingLogSink::OnLogMessage(absl::string_view message,
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020058 LoggingSeverity sev,
59 const char* tag) {
Niels Möllera9311b62021-03-25 15:29:02 +010060 if (!stream_->IsOpen()) {
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020061 std::fprintf(stderr, "Init() must be called before adding this sink.\n");
62 return;
63 }
Niels Möllera9311b62021-03-25 15:29:02 +010064 stream_->Write(tag, strlen(tag));
65 stream_->Write(": ", 2);
Ali Tofigh6364d082022-03-14 13:32:04 +010066 stream_->Write(message.data(), message.size());
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020067}
68
tkchin93411912015-07-22 12:12:17 -070069bool FileRotatingLogSink::Init() {
70 return stream_->Open();
71}
72
tkchin28bae022015-07-23 12:27:02 -070073bool FileRotatingLogSink::DisableBuffering() {
74 return stream_->DisableBuffering();
75}
76
tkchin93411912015-07-22 12:12:17 -070077CallSessionFileRotatingLogSink::CallSessionFileRotatingLogSink(
Ali Tofigh2ab914c2022-04-13 12:55:15 +020078 absl::string_view log_dir_path,
tkchin93411912015-07-22 12:12:17 -070079 size_t max_total_log_size)
80 : FileRotatingLogSink(
81 new CallSessionFileRotatingStream(log_dir_path, max_total_log_size)) {
82}
83
Yves Gerey665174f2018-06-19 15:03:05 +020084CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {}
tkchin93411912015-07-22 12:12:17 -070085
86} // namespace rtc