诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

132 Zeilen
3.9 KiB

  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)
  22. : base_filename_(std::move(base_filename))
  23. , max_size_(max_size)
  24. , max_files_(max_files)
  25. {
  26. file_helper_.open(calc_filename(base_filename_, 0));
  27. current_size_ = file_helper_.size(); // expensive. called only once
  28. if (rotate_on_open && current_size_ > 0)
  29. {
  30. rotate_();
  31. }
  32. }
  33. // calc filename according to index and file extension if exists.
  34. // e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
  35. template<typename Mutex>
  36. SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename, std::size_t index)
  37. {
  38. if (index == 0u)
  39. {
  40. return filename;
  41. }
  42. filename_t basename, ext;
  43. std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
  44. return fmt::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
  45. }
  46. template<typename Mutex>
  47. SPDLOG_INLINE const filename_t &rotating_file_sink<Mutex>::filename() const
  48. {
  49. return file_helper_.filename();
  50. }
  51. template<typename Mutex>
  52. SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg)
  53. {
  54. memory_buf_t formatted;
  55. base_sink<Mutex>::formatter_->format(msg, formatted);
  56. current_size_ += formatted.size();
  57. if (current_size_ > max_size_)
  58. {
  59. rotate_();
  60. current_size_ = formatted.size();
  61. }
  62. file_helper_.write(formatted);
  63. }
  64. template<typename Mutex>
  65. SPDLOG_INLINE void rotating_file_sink<Mutex>::flush_()
  66. {
  67. file_helper_.flush();
  68. }
  69. // Rotate files:
  70. // log.txt -> log.1.txt
  71. // log.1.txt -> log.2.txt
  72. // log.2.txt -> log.3.txt
  73. // log.3.txt -> delete
  74. template<typename Mutex>
  75. SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_()
  76. {
  77. using details::os::filename_to_str;
  78. using details::os::path_exists;
  79. file_helper_.close();
  80. for (auto i = max_files_; i > 0; --i)
  81. {
  82. filename_t src = calc_filename(base_filename_, i - 1);
  83. if (!path_exists(src))
  84. {
  85. continue;
  86. }
  87. filename_t target = calc_filename(base_filename_, i);
  88. if (!rename_file(src, target))
  89. {
  90. // if failed try again after a small delay.
  91. // this is a workaround to a windows issue, where very high rotation
  92. // rates can cause the rename to fail with permission denied (because of antivirus?).
  93. details::os::sleep_for_millis(100);
  94. if (!rename_file(src, target))
  95. {
  96. file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
  97. current_size_ = 0;
  98. SPDLOG_THROW(
  99. spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno));
  100. }
  101. }
  102. }
  103. file_helper_.reopen(true);
  104. }
  105. // delete the target if exists, and rename the src file to target
  106. // return true on success, false otherwise.
  107. template<typename Mutex>
  108. SPDLOG_INLINE bool rotating_file_sink<Mutex>::rename_file(const filename_t &src_filename, const filename_t &target_filename)
  109. {
  110. // try to delete the target file in case it already exists.
  111. (void)details::os::remove(target_filename);
  112. return details::os::rename(src_filename, target_filename) == 0;
  113. }
  114. } // namespace sinks
  115. } // namespace spdlog