blob: b237e873ee0ada8a1d5b602a22cb82146e4a7c56 [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
23FileRotatingLogSink::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 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 Tofigh6364d082022-03-14 13:32:04 +010051void FileRotatingLogSink::OnLogMessage(absl::string_view message,
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020052 LoggingSeverity sev,
53 const char* tag) {
Niels Möllera9311b62021-03-25 15:29:02 +010054 if (!stream_->IsOpen()) {
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020055 std::fprintf(stderr, "Init() must be called before adding this sink.\n");
56 return;
57 }
Niels Möllera9311b62021-03-25 15:29:02 +010058 stream_->Write(tag, strlen(tag));
59 stream_->Write(": ", 2);
Ali Tofigh6364d082022-03-14 13:32:04 +010060 stream_->Write(message.data(), message.size());
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020061}
62
tkchin93411912015-07-22 12:12:17 -070063bool FileRotatingLogSink::Init() {
64 return stream_->Open();
65}
66
tkchin28bae022015-07-23 12:27:02 -070067bool FileRotatingLogSink::DisableBuffering() {
68 return stream_->DisableBuffering();
69}
70
tkchin93411912015-07-22 12:12:17 -070071CallSessionFileRotatingLogSink::CallSessionFileRotatingLogSink(
72 const std::string& log_dir_path,
73 size_t max_total_log_size)
74 : FileRotatingLogSink(
75 new CallSessionFileRotatingStream(log_dir_path, max_total_log_size)) {
76}
77
Yves Gerey665174f2018-06-19 15:03:05 +020078CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {}
tkchin93411912015-07-22 12:12:17 -070079
80} // namespace rtc