log_sinks.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef RTC_BASE_LOG_SINKS_H_
  11. #define RTC_BASE_LOG_SINKS_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include <string>
  15. #include "rtc_base/constructor_magic.h"
  16. #include "rtc_base/file_rotating_stream.h"
  17. #include "rtc_base/logging.h"
  18. namespace rtc {
  19. // Log sink that uses a FileRotatingStream to write to disk.
  20. // Init() must be called before adding this sink.
  21. class FileRotatingLogSink : public LogSink {
  22. public:
  23. // |num_log_files| must be greater than 1 and |max_log_size| must be greater
  24. // than 0.
  25. FileRotatingLogSink(const std::string& log_dir_path,
  26. const std::string& log_prefix,
  27. size_t max_log_size,
  28. size_t num_log_files);
  29. ~FileRotatingLogSink() override;
  30. // Writes the message to the current file. It will spill over to the next
  31. // file if needed.
  32. void OnLogMessage(const std::string& message) override;
  33. void OnLogMessage(const std::string& message,
  34. LoggingSeverity sev,
  35. const char* tag) override;
  36. // Deletes any existing files in the directory and creates a new log file.
  37. virtual bool Init();
  38. // Disables buffering on the underlying stream.
  39. bool DisableBuffering();
  40. protected:
  41. explicit FileRotatingLogSink(FileRotatingStream* stream);
  42. private:
  43. std::unique_ptr<FileRotatingStream> stream_;
  44. RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingLogSink);
  45. };
  46. // Log sink that uses a CallSessionFileRotatingStream to write to disk.
  47. // Init() must be called before adding this sink.
  48. class CallSessionFileRotatingLogSink : public FileRotatingLogSink {
  49. public:
  50. CallSessionFileRotatingLogSink(const std::string& log_dir_path,
  51. size_t max_total_log_size);
  52. ~CallSessionFileRotatingLogSink() override;
  53. private:
  54. RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingLogSink);
  55. };
  56. } // namespace rtc
  57. #endif // RTC_BASE_LOG_SINKS_H_