诸暨麻将添加redis
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

181 lignes
5.6 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. #include <spdlog/common.h>
  5. #include <spdlog/details/file_helper.h>
  6. #include <spdlog/details/null_mutex.h>
  7. #include <spdlog/fmt/fmt.h>
  8. #include <spdlog/sinks/base_sink.h>
  9. #include <spdlog/details/os.h>
  10. #include <spdlog/details/synchronous_factory.h>
  11. #include <chrono>
  12. #include <cstdio>
  13. #include <ctime>
  14. #include <mutex>
  15. #include <string>
  16. namespace spdlog {
  17. namespace sinks {
  18. /*
  19. * Generator of daily log file names in format basename.YYYY-MM-DD.ext
  20. */
  21. struct daily_filename_calculator
  22. {
  23. // Create filename for the form basename.YYYY-MM-DD
  24. static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
  25. {
  26. filename_t basename, ext;
  27. std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
  28. return fmt::format(
  29. SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext);
  30. }
  31. };
  32. /*
  33. * Rotating file sink based on date.
  34. * If truncate != false , the created file will be truncated.
  35. * If max_files > 0, retain only the last max_files and delete previous.
  36. */
  37. template<typename Mutex, typename FileNameCalc = daily_filename_calculator>
  38. class daily_file_sink final : public base_sink<Mutex>
  39. {
  40. public:
  41. // create daily file sink which rotates on given time
  42. daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0)
  43. : base_filename_(std::move(base_filename))
  44. , rotation_h_(rotation_hour)
  45. , rotation_m_(rotation_minute)
  46. , truncate_(truncate)
  47. , max_files_(max_files)
  48. , filenames_q_()
  49. {
  50. if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
  51. {
  52. SPDLOG_THROW(spdlog_ex("daily_file_sink: Invalid rotation time in ctor"));
  53. }
  54. auto now = log_clock::now();
  55. auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
  56. file_helper_.open(filename, truncate_);
  57. rotation_tp_ = next_rotation_tp_();
  58. if (max_files_ > 0)
  59. {
  60. filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
  61. filenames_q_.push_back(std::move(filename));
  62. }
  63. }
  64. const filename_t &filename() const
  65. {
  66. return file_helper_.filename();
  67. }
  68. protected:
  69. void sink_it_(const details::log_msg &msg) override
  70. {
  71. auto time = msg.time;
  72. bool should_rotate = time >= rotation_tp_;
  73. if (should_rotate)
  74. {
  75. auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
  76. file_helper_.open(filename, truncate_);
  77. rotation_tp_ = next_rotation_tp_();
  78. }
  79. memory_buf_t formatted;
  80. base_sink<Mutex>::formatter_->format(msg, formatted);
  81. file_helper_.write(formatted);
  82. // Do the cleaning only at the end because it might throw on failure.
  83. if (should_rotate && max_files_ > 0)
  84. {
  85. delete_old_();
  86. }
  87. }
  88. void flush_() override
  89. {
  90. file_helper_.flush();
  91. }
  92. private:
  93. tm now_tm(log_clock::time_point tp)
  94. {
  95. time_t tnow = log_clock::to_time_t(tp);
  96. return spdlog::details::os::localtime(tnow);
  97. }
  98. log_clock::time_point next_rotation_tp_()
  99. {
  100. auto now = log_clock::now();
  101. tm date = now_tm(now);
  102. date.tm_hour = rotation_h_;
  103. date.tm_min = rotation_m_;
  104. date.tm_sec = 0;
  105. auto rotation_time = log_clock::from_time_t(std::mktime(&date));
  106. if (rotation_time > now)
  107. {
  108. return rotation_time;
  109. }
  110. return {rotation_time + std::chrono::hours(24)};
  111. }
  112. // Delete the file N rotations ago.
  113. // Throw spdlog_ex on failure to delete the old file.
  114. void delete_old_()
  115. {
  116. using details::os::filename_to_str;
  117. using details::os::remove_if_exists;
  118. filename_t current_file = filename();
  119. if (filenames_q_.full())
  120. {
  121. auto old_filename = std::move(filenames_q_.front());
  122. filenames_q_.pop_front();
  123. bool ok = remove_if_exists(old_filename) == 0;
  124. if (!ok)
  125. {
  126. filenames_q_.push_back(std::move(current_file));
  127. SPDLOG_THROW(spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno));
  128. }
  129. }
  130. filenames_q_.push_back(std::move(current_file));
  131. }
  132. filename_t base_filename_;
  133. int rotation_h_;
  134. int rotation_m_;
  135. log_clock::time_point rotation_tp_;
  136. details::file_helper file_helper_;
  137. bool truncate_;
  138. uint16_t max_files_;
  139. details::circular_q<filename_t> filenames_q_;
  140. };
  141. using daily_file_sink_mt = daily_file_sink<std::mutex>;
  142. using daily_file_sink_st = daily_file_sink<details::null_mutex>;
  143. } // namespace sinks
  144. //
  145. // factory functions
  146. //
  147. template<typename Factory = spdlog::synchronous_factory>
  148. inline std::shared_ptr<logger> daily_logger_mt(
  149. const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
  150. {
  151. return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate);
  152. }
  153. template<typename Factory = spdlog::synchronous_factory>
  154. inline std::shared_ptr<logger> daily_logger_st(
  155. const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
  156. {
  157. return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate);
  158. }
  159. } // namespace spdlog