blob: c01bafbdd2280f45988b2927cc75bcb04da4f86d [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/logsinks.h"
tkchin93411912015-07-22 12:12:17 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
Jonas Olsson55378f42018-05-25 10:23:10 +020014#include <cstdio>
tkchin93411912015-07-22 12:12:17 -070015#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "rtc_base/stream.h"
tkchin28bae022015-07-23 12:27:02 -070019
tkchin93411912015-07-22 12:12:17 -070020namespace rtc {
21
22FileRotatingLogSink::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 Gerey665174f2018-06-19 15:03:05 +020029 num_log_files)) {}
tkchin93411912015-07-22 12:12:17 -070030
31FileRotatingLogSink::FileRotatingLogSink(FileRotatingStream* stream)
32 : stream_(stream) {
henrikg91d6ede2015-09-17 00:24:34 -070033 RTC_DCHECK(stream);
tkchin93411912015-07-22 12:12:17 -070034}
35
Yves Gerey665174f2018-06-19 15:03:05 +020036FileRotatingLogSink::~FileRotatingLogSink() {}
tkchin93411912015-07-22 12:12:17 -070037
38void FileRotatingLogSink::OnLogMessage(const std::string& message) {
tkchin28bae022015-07-23 12:27:02 -070039 if (stream_->GetState() != SS_OPEN) {
Jonas Olsson55378f42018-05-25 10:23:10 +020040 std::fprintf(stderr, "Init() must be called before adding this sink.\n");
tkchin93411912015-07-22 12:12:17 -070041 return;
42 }
43 stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr);
44}
45
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020046void 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
tkchin93411912015-07-22 12:12:17 -070058bool FileRotatingLogSink::Init() {
59 return stream_->Open();
60}
61
tkchin28bae022015-07-23 12:27:02 -070062bool FileRotatingLogSink::DisableBuffering() {
63 return stream_->DisableBuffering();
64}
65
tkchin93411912015-07-22 12:12:17 -070066CallSessionFileRotatingLogSink::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 Gerey665174f2018-06-19 15:03:05 +020073CallSessionFileRotatingLogSink::~CallSessionFileRotatingLogSink() {}
tkchin93411912015-07-22 12:12:17 -070074
75} // namespace rtc