tkchin | 9341191 | 2015-07-22 19:12:17 | [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 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_LOG_SINKS_H_ |
| 12 | #define RTC_BASE_LOG_SINKS_H_ |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <string> |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 18 | |
Ali Tofigh | 6364d08 | 2022-03-14 12:32:04 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/file_rotating_stream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
tkchin | 9341191 | 2015-07-22 19:12:17 | [diff] [blame] | 22 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 23 | namespace rtc { |
| 24 | |
| 25 | // Log sink that uses a FileRotatingStream to write to disk. |
| 26 | // Init() must be called before adding this sink. |
| 27 | class FileRotatingLogSink : public LogSink { |
| 28 | public: |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 29 | // `num_log_files` must be greater than 1 and `max_log_size` must be greater |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 30 | // than 0. |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 31 | FileRotatingLogSink(absl::string_view log_dir_path, |
| 32 | absl::string_view log_prefix, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | size_t max_log_size, |
| 34 | size_t num_log_files); |
| 35 | ~FileRotatingLogSink() override; |
| 36 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 37 | FileRotatingLogSink(const FileRotatingLogSink&) = delete; |
| 38 | FileRotatingLogSink& operator=(const FileRotatingLogSink&) = delete; |
| 39 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 40 | // Writes the message to the current file. It will spill over to the next |
| 41 | // file if needed. |
| 42 | void OnLogMessage(const std::string& message) override; |
Ali Tofigh | 6364d08 | 2022-03-14 12:32:04 | [diff] [blame] | 43 | void OnLogMessage(absl::string_view message) override; |
Ali Tofigh | 8fcc79b | 2022-04-29 20:39:20 | [diff] [blame] | 44 | void OnLogMessage(const std::string& message, |
| 45 | LoggingSeverity sev, |
| 46 | const char* tag) override; |
Ali Tofigh | 6364d08 | 2022-03-14 12:32:04 | [diff] [blame] | 47 | void OnLogMessage(absl::string_view message, |
Paulina Hensman | f1e3cb4 | 2018-06-20 12:07:05 | [diff] [blame] | 48 | LoggingSeverity sev, |
| 49 | const char* tag) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 50 | |
| 51 | // Deletes any existing files in the directory and creates a new log file. |
| 52 | virtual bool Init(); |
| 53 | |
| 54 | // Disables buffering on the underlying stream. |
| 55 | bool DisableBuffering(); |
| 56 | |
| 57 | protected: |
| 58 | explicit FileRotatingLogSink(FileRotatingStream* stream); |
| 59 | |
| 60 | private: |
| 61 | std::unique_ptr<FileRotatingStream> stream_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // Log sink that uses a CallSessionFileRotatingStream to write to disk. |
| 65 | // Init() must be called before adding this sink. |
| 66 | class CallSessionFileRotatingLogSink : public FileRotatingLogSink { |
| 67 | public: |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 68 | CallSessionFileRotatingLogSink(absl::string_view log_dir_path, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 69 | size_t max_total_log_size); |
| 70 | ~CallSessionFileRotatingLogSink() override; |
| 71 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 72 | CallSessionFileRotatingLogSink(const CallSessionFileRotatingLogSink&) = |
| 73 | delete; |
| 74 | CallSessionFileRotatingLogSink& operator=( |
| 75 | const CallSessionFileRotatingLogSink&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace rtc |
| 79 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 80 | #endif // RTC_BASE_LOG_SINKS_H_ |