os.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <ctime> // std::time_t
  6. namespace spdlog {
  7. namespace details {
  8. namespace os {
  9. SPDLOG_API spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT;
  10. SPDLOG_API std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
  11. SPDLOG_API std::tm localtime() SPDLOG_NOEXCEPT;
  12. SPDLOG_API std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
  13. SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT;
  14. // eol definition
  15. #if !defined(SPDLOG_EOL)
  16. # ifdef _WIN32
  17. # define SPDLOG_EOL "\r\n"
  18. # else
  19. # define SPDLOG_EOL "\n"
  20. # endif
  21. #endif
  22. SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL;
  23. // folder separator
  24. #if !defined(SPDLOG_FOLDER_SEPS)
  25. # ifdef _WIN32
  26. # define SPDLOG_FOLDER_SEPS "\\/"
  27. # else
  28. # define SPDLOG_FOLDER_SEPS "/"
  29. # endif
  30. #endif
  31. SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS;
  32. SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
  33. // fopen_s on non windows for writing
  34. SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
  35. // Remove filename. return 0 on success
  36. SPDLOG_API int remove(const filename_t &filename) SPDLOG_NOEXCEPT;
  37. // Remove file if exists. return 0 on success
  38. // Note: Non atomic (might return failure to delete if concurrently deleted by other process/thread)
  39. SPDLOG_API int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
  40. SPDLOG_API int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT;
  41. // Return if file exists.
  42. SPDLOG_API bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
  43. // Return file size according to open FILE* object
  44. SPDLOG_API size_t filesize(FILE *f);
  45. // Return utc offset in minutes or throw spdlog_ex on failure
  46. SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime());
  47. // Return current thread id as size_t
  48. // It exists because the std::this_thread::get_id() is much slower(especially
  49. // under VS 2013)
  50. SPDLOG_API size_t _thread_id() SPDLOG_NOEXCEPT;
  51. // Return current thread id as size_t (from thread local storage)
  52. SPDLOG_API size_t thread_id() SPDLOG_NOEXCEPT;
  53. // This is avoid msvc issue in sleep_for that happens if the clock changes.
  54. // See https://github.com/gabime/spdlog/issues/609
  55. SPDLOG_API void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT;
  56. SPDLOG_API std::string filename_to_str(const filename_t &filename);
  57. SPDLOG_API int pid() SPDLOG_NOEXCEPT;
  58. // Determine if the terminal supports colors
  59. // Source: https://github.com/agauniyal/rang/
  60. SPDLOG_API bool is_color_terminal() SPDLOG_NOEXCEPT;
  61. // Determine if the terminal attached
  62. // Source: https://github.com/agauniyal/rang/
  63. SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
  64. #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
  65. SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target);
  66. SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
  67. #endif
  68. // Return directory name from given path or empty string
  69. // "abc/file" => "abc"
  70. // "abc/" => "abc"
  71. // "abc" => ""
  72. // "abc///" => "abc//"
  73. SPDLOG_API filename_t dir_name(const filename_t &path);
  74. // Create a dir from the given path.
  75. // Return true if succeeded or if this dir already exists.
  76. SPDLOG_API bool create_dir(const filename_t &path);
  77. // non thread safe, cross platform getenv/getenv_s
  78. // return empty string if field not found
  79. SPDLOG_API std::string getenv(const char *field);
  80. // Do fsync by FILE objectpointer.
  81. // Return true on success.
  82. SPDLOG_API bool fsync(FILE *fp);
  83. } // namespace os
  84. } // namespace details
  85. } // namespace spdlog
  86. #ifdef SPDLOG_HEADER_ONLY
  87. # include "os-inl.h"
  88. #endif