rotating_file_sink.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <spdlog/details/file_helper.h>
  5. #include <spdlog/details/null_mutex.h>
  6. #include <spdlog/details/synchronous_factory.h>
  7. #include <spdlog/sinks/base_sink.h>
  8. #include <chrono>
  9. #include <mutex>
  10. #include <string>
  11. namespace spdlog {
  12. namespace sinks {
  13. //
  14. // Rotating file sink based on size
  15. //
  16. template <typename Mutex>
  17. class rotating_file_sink final : public base_sink<Mutex> {
  18. public:
  19. rotating_file_sink(filename_t base_filename,
  20. std::size_t max_size,
  21. std::size_t max_files,
  22. bool rotate_on_open = false,
  23. const file_event_handlers &event_handlers = {});
  24. static filename_t calc_filename(const filename_t &filename, std::size_t index);
  25. filename_t filename();
  26. protected:
  27. void sink_it_(const details::log_msg &msg) override;
  28. void flush_() override;
  29. private:
  30. // Rotate files:
  31. // log.txt -> log.1.txt
  32. // log.1.txt -> log.2.txt
  33. // log.2.txt -> log.3.txt
  34. // log.3.txt -> delete
  35. void rotate_();
  36. // delete the target if exists, and rename the src file to target
  37. // return true on success, false otherwise.
  38. bool rename_file_(const filename_t &src_filename, const filename_t &target_filename);
  39. filename_t base_filename_;
  40. std::size_t max_size_;
  41. std::size_t max_files_;
  42. std::size_t current_size_;
  43. details::file_helper file_helper_;
  44. };
  45. using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
  46. using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
  47. } // namespace sinks
  48. //
  49. // factory functions
  50. //
  51. template <typename Factory = spdlog::synchronous_factory>
  52. inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name,
  53. const filename_t &filename,
  54. size_t max_file_size,
  55. size_t max_files,
  56. bool rotate_on_open = false,
  57. const file_event_handlers &event_handlers = {}) {
  58. return Factory::template create<sinks::rotating_file_sink_mt>(
  59. logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
  60. }
  61. template <typename Factory = spdlog::synchronous_factory>
  62. inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name,
  63. const filename_t &filename,
  64. size_t max_file_size,
  65. size_t max_files,
  66. bool rotate_on_open = false,
  67. const file_event_handlers &event_handlers = {}) {
  68. return Factory::template create<sinks::rotating_file_sink_st>(
  69. logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
  70. }
  71. } // namespace spdlog
  72. #ifdef SPDLOG_HEADER_ONLY
  73. #include "rotating_file_sink-inl.h"
  74. #endif