file_helper-inl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(const file_event_handlers &event_handlers)
  18. : event_handlers_(event_handlers)
  19. {}
  20. SPDLOG_INLINE file_helper::~file_helper()
  21. {
  22. close();
  23. }
  24. SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
  25. {
  26. close();
  27. filename_ = fname;
  28. auto *mode = SPDLOG_FILENAME_T("ab");
  29. auto *trunc_mode = SPDLOG_FILENAME_T("wb");
  30. if (event_handlers_.before_open)
  31. {
  32. event_handlers_.before_open(filename_);
  33. }
  34. for (int tries = 0; tries < open_tries_; ++tries)
  35. {
  36. // create containing folder if not exists already.
  37. os::create_dir(os::dir_name(fname));
  38. if (truncate)
  39. {
  40. // Truncate by opening-and-closing a tmp file in "wb" mode, always
  41. // opening the actual log-we-write-to in "ab" mode, since that
  42. // interacts more politely with eternal processes that might
  43. // rotate/truncate the file underneath us.
  44. std::FILE *tmp;
  45. if (os::fopen_s(&tmp, fname, trunc_mode))
  46. {
  47. continue;
  48. }
  49. std::fclose(tmp);
  50. }
  51. if (!os::fopen_s(&fd_, fname, mode))
  52. {
  53. if (event_handlers_.after_open)
  54. {
  55. event_handlers_.after_open(filename_, fd_);
  56. }
  57. return;
  58. }
  59. details::os::sleep_for_millis(open_interval_);
  60. }
  61. throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
  62. }
  63. SPDLOG_INLINE void file_helper::reopen(bool truncate)
  64. {
  65. if (filename_.empty())
  66. {
  67. throw_spdlog_ex("Failed re opening file - was not opened before");
  68. }
  69. this->open(filename_, truncate);
  70. }
  71. SPDLOG_INLINE void file_helper::flush()
  72. {
  73. if (std::fflush(fd_) != 0)
  74. {
  75. throw_spdlog_ex("Failed flush to file " + os::filename_to_str(filename_), errno);
  76. }
  77. }
  78. SPDLOG_INLINE void file_helper::sync()
  79. {
  80. if (!os::fsync(fd_))
  81. {
  82. throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno);
  83. }
  84. }
  85. SPDLOG_INLINE void file_helper::close()
  86. {
  87. if (fd_ != nullptr)
  88. {
  89. if (event_handlers_.before_close)
  90. {
  91. event_handlers_.before_close(filename_, fd_);
  92. }
  93. std::fclose(fd_);
  94. fd_ = nullptr;
  95. if (event_handlers_.after_close)
  96. {
  97. event_handlers_.after_close(filename_);
  98. }
  99. }
  100. }
  101. SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf)
  102. {
  103. size_t msg_size = buf.size();
  104. auto data = buf.data();
  105. if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
  106. {
  107. throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
  108. }
  109. }
  110. SPDLOG_INLINE size_t file_helper::size() const
  111. {
  112. if (fd_ == nullptr)
  113. {
  114. throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_));
  115. }
  116. return os::filesize(fd_);
  117. }
  118. SPDLOG_INLINE const filename_t &file_helper::filename() const
  119. {
  120. return filename_;
  121. }
  122. //
  123. // return file path and its extension:
  124. //
  125. // "mylog.txt" => ("mylog", ".txt")
  126. // "mylog" => ("mylog", "")
  127. // "mylog." => ("mylog.", "")
  128. // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt")
  129. //
  130. // the starting dot in filenames is ignored (hidden files):
  131. //
  132. // ".mylog" => (".mylog". "")
  133. // "my_folder/.mylog" => ("my_folder/.mylog", "")
  134. // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
  135. SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension(const filename_t &fname)
  136. {
  137. auto ext_index = fname.rfind('.');
  138. // no valid extension found - return whole path and empty string as
  139. // extension
  140. if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1)
  141. {
  142. return std::make_tuple(fname, filename_t());
  143. }
  144. // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
  145. auto folder_index = fname.find_last_of(details::os::folder_seps_filename);
  146. if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
  147. {
  148. return std::make_tuple(fname, filename_t());
  149. }
  150. // finally - return a valid base and extension tuple
  151. return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
  152. }
  153. } // namespace details
  154. } // namespace spdlog