Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

file_helper.h 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. #pragma once
  6. // Helper class for file sink
  7. // When failing to open a file, retry several times(5) with small delay between the tries(10 ms)
  8. // Can be set to auto flush on every line
  9. // Throw spdlog_ex exception on errors
  10. #include <spdlog/details/os.h>
  11. #include <spdlog/details/log_msg.h>
  12. #include <chrono>
  13. #include <cstdio>
  14. #include <string>
  15. #include <thread>
  16. #include <cerrno>
  17. namespace spdlog
  18. {
  19. namespace details
  20. {
  21. class file_helper
  22. {
  23. public:
  24. const int open_tries = 5;
  25. const int open_interval = 10;
  26. explicit file_helper() :
  27. _fd(nullptr)
  28. {}
  29. file_helper(const file_helper&) = delete;
  30. file_helper& operator=(const file_helper&) = delete;
  31. ~file_helper()
  32. {
  33. close();
  34. }
  35. void open(const filename_t& fname, bool truncate = false)
  36. {
  37. close();
  38. auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
  39. _filename = fname;
  40. for (int tries = 0; tries < open_tries; ++tries)
  41. {
  42. if (!os::fopen_s(&_fd, fname, mode))
  43. return;
  44. std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
  45. }
  46. throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno);
  47. }
  48. void reopen(bool truncate)
  49. {
  50. if (_filename.empty())
  51. throw spdlog_ex("Failed re opening file - was not opened before");
  52. open(_filename, truncate);
  53. }
  54. void flush()
  55. {
  56. std::fflush(_fd);
  57. }
  58. void close()
  59. {
  60. if (_fd)
  61. {
  62. std::fclose(_fd);
  63. _fd = nullptr;
  64. }
  65. }
  66. void write(const log_msg& msg)
  67. {
  68. size_t msg_size = msg.formatted.size();
  69. auto data = msg.formatted.data();
  70. if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
  71. throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
  72. }
  73. size_t size()
  74. {
  75. if (!_fd)
  76. throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
  77. return os::filesize(_fd);
  78. }
  79. const filename_t& filename() const
  80. {
  81. return _filename;
  82. }
  83. static bool file_exists(const filename_t& name)
  84. {
  85. return os::file_exists(name);
  86. }
  87. private:
  88. FILE* _fd;
  89. filename_t _filename;
  90. };
  91. }
  92. }