rotating_file_sink-inl.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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,
  22. std::size_t max_size,
  23. std::size_t max_files,
  24. bool rotate_on_open,
  25. const file_event_handlers &event_handlers)
  26. : base_filename_(std::move(base_filename)),
  27. max_size_(max_size),
  28. max_files_(max_files),
  29. file_helper_{event_handlers} {
  30. if (max_size == 0) {
  31. throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
  32. }
  33. if (max_files > 200000) {
  34. throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
  35. }
  36. file_helper_.open(calc_filename(base_filename_, 0));
  37. current_size_ = file_helper_.size(); // expensive. called only once
  38. if (rotate_on_open && current_size_ > 0) {
  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,
  47. std::size_t index) {
  48. if (index == 0u) {
  49. return filename;
  50. }
  51. filename_t basename, ext;
  52. std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
  53. return fmt_lib::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
  54. }
  55. template <typename Mutex>
  56. SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::filename() {
  57. std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
  58. return file_helper_.filename();
  59. }
  60. template <typename Mutex>
  61. SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
  62. memory_buf_t formatted;
  63. base_sink<Mutex>::formatter_->format(msg, formatted);
  64. auto new_size = current_size_ + formatted.size();
  65. // rotate if the new estimated file size exceeds max size.
  66. // rotate only if the real size > 0 to better deal with full disk (see issue #2261).
  67. // we only check the real size when new_size > max_size_ because it is relatively expensive.
  68. if (new_size > max_size_) {
  69. file_helper_.flush();
  70. if (file_helper_.size() > 0) {
  71. rotate_();
  72. new_size = formatted.size();
  73. }
  74. }
  75. file_helper_.write(formatted);
  76. current_size_ = new_size;
  77. }
  78. template <typename Mutex>
  79. SPDLOG_INLINE void rotating_file_sink<Mutex>::flush_() {
  80. file_helper_.flush();
  81. }
  82. // Rotate files:
  83. // log.txt -> log.1.txt
  84. // log.1.txt -> log.2.txt
  85. // log.2.txt -> log.3.txt
  86. // log.3.txt -> delete
  87. template <typename Mutex>
  88. SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_() {
  89. using details::os::filename_to_str;
  90. using details::os::path_exists;
  91. file_helper_.close();
  92. for (auto i = max_files_; i > 0; --i) {
  93. filename_t src = calc_filename(base_filename_, i - 1);
  94. if (!path_exists(src)) {
  95. continue;
  96. }
  97. filename_t target = calc_filename(base_filename_, i);
  98. if (!rename_file_(src, target)) {
  99. // if failed try again after a small delay.
  100. // this is a workaround to a windows issue, where very high rotation
  101. // rates can cause the rename to fail with permission denied (because of antivirus?).
  102. details::os::sleep_for_millis(100);
  103. if (!rename_file_(src, target)) {
  104. file_helper_.reopen(
  105. true); // truncate the log file anyway to prevent it to grow beyond its limit!
  106. current_size_ = 0;
  107. throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) +
  108. " to " + filename_to_str(target),
  109. errno);
  110. }
  111. }
  112. }
  113. file_helper_.reopen(true);
  114. }
  115. // delete the target if exists, and rename the src file to target
  116. // return true on success, false otherwise.
  117. template <typename Mutex>
  118. SPDLOG_INLINE bool rotating_file_sink<Mutex>::rename_file_(const filename_t &src_filename,
  119. const filename_t &target_filename) {
  120. // try to delete the target file in case it already exists.
  121. (void)details::os::remove(target_filename);
  122. return details::os::rename(src_filename, target_filename) == 0;
  123. }
  124. } // namespace sinks
  125. } // namespace spdlog