blob: a3019b97865de01689a327c6bf1e447a508c81c5 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/stream.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) {
tkchin28bae022015-07-23 12:27:02 -070040 if (stream_->GetState() != SS_OPEN) {
Jonas Olsson55378f42018-05-25 10:23:10 +020041 std::fprintf(stderr, "Init() must be called before adding this sink.\n");
tkchin93411912015-07-22 12:12:17 -070042 return;
43 }
44 stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr);
45}
46
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020047void 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
tkchin93411912015-07-22 12:12:17 -070059bool FileRotatingLogSink::Init() {
60 return stream_->Open();
61}
62
tkchin28bae022015-07-23 12:27:02 -070063bool FileRotatingLogSink::DisableBuffering() {
64 return stream_->DisableBuffering();
65}
66
tkchin93411912015-07-22 12:12:17 -070067CallSessionFileRotatingLogSink::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 Gerey665174f2018-06-19 15:03:05 +020074CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {}
tkchin93411912015-07-22 12:12:17 -070075
76} // namespace rtc