rotating_file_sink.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/sinks/base_sink.h>
  5. #include <spdlog/details/file_helper.h>
  6. #include <spdlog/details/null_mutex.h>
  7. #include <spdlog/details/synchronous_factory.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. {
  19. public:
  20. rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false,
  21. const file_event_handlers &event_handlers = {});
  22. static filename_t calc_filename(const filename_t &filename, std::size_t index);
  23. filename_t filename();
  24. protected:
  25. void sink_it_(const details::log_msg &msg) override;
  26. void flush_() override;
  27. private:
  28. // Rotate files:
  29. // log.txt -> log.1.txt
  30. // log.1.txt -> log.2.txt
  31. // log.2.txt -> log.3.txt
  32. // log.3.txt -> delete
  33. void rotate_();
  34. // delete the target if exists, and rename the src file to target
  35. // return true on success, false otherwise.
  36. bool rename_file_(const filename_t &src_filename, const filename_t &target_filename);
  37. filename_t base_filename_;
  38. std::size_t max_size_;
  39. std::size_t max_files_;
  40. std::size_t current_size_;
  41. details::file_helper file_helper_;
  42. };
  43. using rotating_file_sink_mt = rotating_file_sink<std::mutex>;
  44. using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
  45. } // namespace sinks
  46. //
  47. // factory functions
  48. //
  49. template<typename Factory = spdlog::synchronous_factory>
  50. inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name, const filename_t &filename, size_t max_file_size,
  51. size_t max_files, bool rotate_on_open = false, const file_event_handlers &event_handlers = {})
  52. {
  53. return Factory::template create<sinks::rotating_file_sink_mt>(
  54. logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
  55. }
  56. template<typename Factory = spdlog::synchronous_factory>
  57. inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name, const filename_t &filename, size_t max_file_size,
  58. size_t max_files, bool rotate_on_open = false, const file_event_handlers &event_handlers = {})
  59. {
  60. return Factory::template create<sinks::rotating_file_sink_st>(
  61. logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
  62. }
  63. } // namespace spdlog
  64. #ifdef SPDLOG_HEADER_ONLY
  65. # include "rotating_file_sink-inl.h"
  66. #endif