os.h 3.8 KB

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