诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 lines
3.3 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/details/file_helper.h>
  6. #endif
  7. #include <spdlog/details/os.h>
  8. #include <spdlog/common.h>
  9. #include <cerrno>
  10. #include <chrono>
  11. #include <cstdio>
  12. #include <string>
  13. #include <thread>
  14. #include <tuple>
  15. namespace spdlog {
  16. namespace details {
  17. SPDLOG_INLINE file_helper::~file_helper()
  18. {
  19. close();
  20. }
  21. SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
  22. {
  23. close();
  24. filename_ = fname;
  25. auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
  26. for (int tries = 0; tries < open_tries_; ++tries)
  27. {
  28. // create containing folder if not exists already.
  29. os::create_dir(os::dir_name(fname));
  30. if (!os::fopen_s(&fd_, fname, mode))
  31. {
  32. return;
  33. }
  34. details::os::sleep_for_millis(open_interval_);
  35. }
  36. SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno));
  37. }
  38. SPDLOG_INLINE void file_helper::reopen(bool truncate)
  39. {
  40. if (filename_.empty())
  41. {
  42. SPDLOG_THROW(spdlog_ex("Failed re opening file - was not opened before"));
  43. }
  44. this->open(filename_, truncate);
  45. }
  46. SPDLOG_INLINE void file_helper::flush()
  47. {
  48. std::fflush(fd_);
  49. }
  50. SPDLOG_INLINE void file_helper::close()
  51. {
  52. if (fd_ != nullptr)
  53. {
  54. std::fclose(fd_);
  55. fd_ = nullptr;
  56. }
  57. }
  58. SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf)
  59. {
  60. size_t msg_size = buf.size();
  61. auto data = buf.data();
  62. if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
  63. {
  64. SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno));
  65. }
  66. }
  67. SPDLOG_INLINE size_t file_helper::size() const
  68. {
  69. if (fd_ == nullptr)
  70. {
  71. SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_)));
  72. }
  73. return os::filesize(fd_);
  74. }
  75. SPDLOG_INLINE const filename_t &file_helper::filename() const
  76. {
  77. return filename_;
  78. }
  79. //
  80. // return file path and its extension:
  81. //
  82. // "mylog.txt" => ("mylog", ".txt")
  83. // "mylog" => ("mylog", "")
  84. // "mylog." => ("mylog.", "")
  85. // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt")
  86. //
  87. // the starting dot in filenames is ignored (hidden files):
  88. //
  89. // ".mylog" => (".mylog". "")
  90. // "my_folder/.mylog" => ("my_folder/.mylog", "")
  91. // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
  92. SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension(const filename_t &fname)
  93. {
  94. auto ext_index = fname.rfind('.');
  95. // no valid extension found - return whole path and empty string as
  96. // extension
  97. if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1)
  98. {
  99. return std::make_tuple(fname, filename_t());
  100. }
  101. // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
  102. auto folder_index = fname.rfind(details::os::folder_sep);
  103. if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
  104. {
  105. return std::make_tuple(fname, filename_t());
  106. }
  107. // finally - return a valid base and extension tuple
  108. return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
  109. }
  110. } // namespace details
  111. } // namespace spdlog