rotating_file_sink-inl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. # include <spdlog/sinks/rotating_file_sink.h>
  6. #endif
  7. #include <spdlog/common.h>
  8. #include <spdlog/details/file_helper.h>
  9. #include <spdlog/details/null_mutex.h>
  10. #include <spdlog/fmt/fmt.h>
  11. #include <cerrno>
  12. #include <chrono>
  13. #include <ctime>
  14. #include <mutex>
  15. #include <string>
  16. #include <tuple>
  17. namespace spdlog {
  18. namespace sinks {
  19. template<typename Mutex>
  20. SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(
  21. filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open, const file_event_handlers &event_handlers)
  22. : base_filename_(std::move(base_filename))
  23. , max_size_(max_size)
  24. , max_files_(max_files)
  25. , file_helper_{event_handlers}
  26. {
  27. if (max_size == 0)
  28. {
  29. throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
  30. }
  31. if (max_files > 200000)
  32. {
  33. throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
  34. }
  35. file_helper_.open(calc_filename(base_filename_, 0));
  36. current_size_ = file_helper_.size(); // expensive. called only once
  37. if (rotate_on_open && current_size_ > 0)
  38. {
  39. rotate_();
  40. current_size_ = 0;
  41. }
  42. }
  43. // calc filename according to index and file extension if exists.
  44. // e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
  45. template<typename Mutex>
  46. SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename, std::size_t index)
  47. {
  48. if (index == 0u)
  49. {
  50. return filename;
  51. }
  52. filename_t basename, ext;
  53. std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
  54. return fmt_lib::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
  55. }
  56. template<typename Mutex>
  57. SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::filename()
  58. {
  59. std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
  60. return file_helper_.filename();
  61. }
  62. template<typename Mutex>
  63. SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
  64. {
  65. memory_buf_t formatted;
  66. base_sink<Mutex>::formatter_->format(msg, formatted);
  67. auto new_size = current_size_ + formatted.size();
  68. // rotate if the new estimated file size exceeds max size.
  69. // rotate only if the real size > 0 to better deal with full disk (see issue #2261).
  70. // we only check the real size when new_size > max_size_ because it is relatively expensive.
  71. if (new_size > max_size_)
  72. {
  73. file_helper_.flush();
  74. if (file_helper_.size() > 0)
  75. {
  76. rotate_();
  77. new_size = formatted.size();
  78. }
  79. }
  80. file_helper_.write(formatted);
  81. current_size_ = new_size;
  82. }
  83. template<typename Mutex>
  84. SPDLOG_INLINE void rotating_file_sink<Mutex>::flush_()
  85. {
  86. file_helper_.flush();
  87. }
  88. // Rotate files:
  89. // log.txt -> log.1.txt
  90. // log.1.txt -> log.2.txt
  91. // log.2.txt -> log.3.txt
  92. // log.3.txt -> delete
  93. template<typename Mutex>
  94. SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_()
  95. {
  96. using details::os::filename_to_str;
  97. using details::os::path_exists;
  98. file_helper_.close();
  99. for (auto i = max_files_; i > 0; --i)
  100. {
  101. filename_t src = calc_filename(base_filename_, i - 1);
  102. if (!path_exists(src))
  103. {
  104. continue;
  105. }
  106. filename_t target = calc_filename(base_filename_, i);
  107. if (!rename_file_(src, target))
  108. {
  109. // if failed try again after a small delay.
  110. // this is a workaround to a windows issue, where very high rotation
  111. // rates can cause the rename to fail with permission denied (because of antivirus?).
  112. details::os::sleep_for_millis(100);
  113. if (!rename_file_(src, target))
  114. {
  115. file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
  116. current_size_ = 0;
  117. throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
  118. }
  119. }
  120. }
  121. file_helper_.reopen(true);
  122. }
  123. // delete the target if exists, and rename the src file to target
  124. // return true on success, false otherwise.
  125. template<typename Mutex>
  126. SPDLOG_INLINE bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename, const filename_t &target_filename)
  127. {
  128. // try to delete the target file in case it already exists.
  129. (void)details::os::remove(target_filename);
  130. return details::os::rename(src_filename, target_filename) == 0;
  131. }
  132. } // namespace sinks
  133. } // namespace spdlog