os-inl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/os.h>
  6. #endif
  7. #include <spdlog/common.h>
  8. #include <algorithm>
  9. #include <array>
  10. #include <chrono>
  11. #include <cstdio>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <ctime>
  15. #include <string>
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <thread>
  19. #ifdef _WIN32
  20. #include <spdlog/details/windows_include.h>
  21. #include <fileapi.h> // for FlushFileBuffers
  22. #include <io.h> // for _get_osfhandle, _isatty, _fileno
  23. #include <process.h> // for _get_pid
  24. #ifdef __MINGW32__
  25. #include <share.h>
  26. #endif
  27. #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
  28. #include <cassert>
  29. #include <limits>
  30. #endif
  31. #include <direct.h> // for _mkdir/_wmkdir
  32. #else // unix
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #ifdef __linux__
  36. #include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
  37. #elif defined(_AIX)
  38. #include <pthread.h> // for pthread_getthrds_np
  39. #elif defined(__DragonFly__) || defined(__FreeBSD__)
  40. #include <pthread_np.h> // for pthread_getthreadid_np
  41. #elif defined(__NetBSD__)
  42. #include <lwp.h> // for _lwp_self
  43. #elif defined(__sun)
  44. #include <thread.h> // for thr_self
  45. #endif
  46. #endif // unix
  47. #if defined __APPLE__
  48. #include <AvailabilityMacros.h>
  49. #endif
  50. #ifndef __has_feature // Clang - feature checking macros.
  51. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  52. #endif
  53. namespace spdlog {
  54. namespace details {
  55. namespace os {
  56. SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT {
  57. #if defined __linux__ && defined SPDLOG_CLOCK_COARSE
  58. timespec ts;
  59. ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
  60. return std::chrono::time_point<log_clock, typename log_clock::duration>(
  61. std::chrono::duration_cast<typename log_clock::duration>(
  62. std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec)));
  63. #else
  64. return log_clock::now();
  65. #endif
  66. }
  67. SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT {
  68. #ifdef _WIN32
  69. std::tm tm;
  70. ::localtime_s(&tm, &time_tt);
  71. #else
  72. std::tm tm;
  73. ::localtime_r(&time_tt, &tm);
  74. #endif
  75. return tm;
  76. }
  77. SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT {
  78. std::time_t now_t = ::time(nullptr);
  79. return localtime(now_t);
  80. }
  81. SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT {
  82. #ifdef _WIN32
  83. std::tm tm;
  84. ::gmtime_s(&tm, &time_tt);
  85. #else
  86. std::tm tm;
  87. ::gmtime_r(&time_tt, &tm);
  88. #endif
  89. return tm;
  90. }
  91. SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT {
  92. std::time_t now_t = ::time(nullptr);
  93. return gmtime(now_t);
  94. }
  95. // fopen_s on non windows for writing
  96. SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) {
  97. #ifdef _WIN32
  98. #ifdef SPDLOG_WCHAR_FILENAMES
  99. *fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
  100. #else
  101. *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
  102. #endif
  103. #if defined(SPDLOG_PREVENT_CHILD_FD)
  104. if (*fp != nullptr) {
  105. auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(::_fileno(*fp)));
  106. if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) {
  107. ::fclose(*fp);
  108. *fp = nullptr;
  109. }
  110. }
  111. #endif
  112. #else // unix
  113. #if defined(SPDLOG_PREVENT_CHILD_FD)
  114. const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
  115. const int fd =
  116. ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
  117. if (fd == -1) {
  118. return true;
  119. }
  120. *fp = ::fdopen(fd, mode.c_str());
  121. if (*fp == nullptr) {
  122. ::close(fd);
  123. }
  124. #else
  125. *fp = ::fopen((filename.c_str()), mode.c_str());
  126. #endif
  127. #endif
  128. return *fp == nullptr;
  129. }
  130. SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT {
  131. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  132. return ::_wremove(filename.c_str());
  133. #else
  134. return std::remove(filename.c_str());
  135. #endif
  136. }
  137. SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT {
  138. return path_exists(filename) ? remove(filename) : 0;
  139. }
  140. SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT {
  141. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  142. return ::_wrename(filename1.c_str(), filename2.c_str());
  143. #else
  144. return std::rename(filename1.c_str(), filename2.c_str());
  145. #endif
  146. }
  147. // Return true if path exists (file or directory)
  148. SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT {
  149. #ifdef _WIN32
  150. struct _stat buffer;
  151. #ifdef SPDLOG_WCHAR_FILENAMES
  152. return (::_wstat(filename.c_str(), &buffer) == 0);
  153. #else
  154. return (::_stat(filename.c_str(), &buffer) == 0);
  155. #endif
  156. #else // common linux/unix all have the stat system call
  157. struct stat buffer;
  158. return (::stat(filename.c_str(), &buffer) == 0);
  159. #endif
  160. }
  161. #ifdef _MSC_VER
  162. // avoid warning about unreachable statement at the end of filesize()
  163. #pragma warning(push)
  164. #pragma warning(disable : 4702)
  165. #endif
  166. // Return file size according to open FILE* object
  167. SPDLOG_INLINE size_t filesize(FILE *f) {
  168. if (f == nullptr) {
  169. throw_spdlog_ex("Failed getting file size. fd is null");
  170. }
  171. #if defined(_WIN32) && !defined(__CYGWIN__)
  172. int fd = ::_fileno(f);
  173. #if defined(_WIN64) // 64 bits
  174. __int64 ret = ::_filelengthi64(fd);
  175. if (ret >= 0) {
  176. return static_cast<size_t>(ret);
  177. }
  178. #else // windows 32 bits
  179. long ret = ::_filelength(fd);
  180. if (ret >= 0) {
  181. return static_cast<size_t>(ret);
  182. }
  183. #endif
  184. #else // unix
  185. // OpenBSD and AIX doesn't compile with :: before the fileno(..)
  186. #if defined(__OpenBSD__) || defined(_AIX)
  187. int fd = fileno(f);
  188. #else
  189. int fd = ::fileno(f);
  190. #endif
  191. // 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated)
  192. #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && \
  193. (defined(__LP64__) || defined(_LP64))
  194. struct stat64 st;
  195. if (::fstat64(fd, &st) == 0) {
  196. return static_cast<size_t>(st.st_size);
  197. }
  198. #else // other unix or linux 32 bits or cygwin
  199. struct stat st;
  200. if (::fstat(fd, &st) == 0) {
  201. return static_cast<size_t>(st.st_size);
  202. }
  203. #endif
  204. #endif
  205. throw_spdlog_ex("Failed getting file size from fd", errno);
  206. return 0; // will not be reached.
  207. }
  208. #ifdef _MSC_VER
  209. #pragma warning(pop)
  210. #endif
  211. // Return utc offset in minutes or throw spdlog_ex on failure
  212. SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
  213. #ifdef _WIN32
  214. #if _WIN32_WINNT < _WIN32_WINNT_WS08
  215. TIME_ZONE_INFORMATION tzinfo;
  216. auto rv = ::GetTimeZoneInformation(&tzinfo);
  217. #else
  218. DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
  219. auto rv = ::GetDynamicTimeZoneInformation(&tzinfo);
  220. #endif
  221. if (rv == TIME_ZONE_ID_INVALID) throw_spdlog_ex("Failed getting timezone info. ", errno);
  222. int offset = -tzinfo.Bias;
  223. if (tm.tm_isdst) {
  224. offset -= tzinfo.DaylightBias;
  225. } else {
  226. offset -= tzinfo.StandardBias;
  227. }
  228. return offset;
  229. #else
  230. #if defined(sun) || defined(__sun) || defined(_AIX) || \
  231. (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
  232. (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
  233. // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
  234. struct helper {
  235. static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
  236. const std::tm &gmtm = details::os::gmtime()) {
  237. int local_year = localtm.tm_year + (1900 - 1);
  238. int gmt_year = gmtm.tm_year + (1900 - 1);
  239. long int days = (
  240. // difference in day of year
  241. localtm.tm_yday -
  242. gmtm.tm_yday
  243. // + intervening leap days
  244. + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) +
  245. ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
  246. // + difference in years * 365 */
  247. + static_cast<long int>(local_year - gmt_year) * 365);
  248. long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour);
  249. long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
  250. long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec);
  251. return secs;
  252. }
  253. };
  254. auto offset_seconds = helper::calculate_gmt_offset(tm);
  255. #else
  256. auto offset_seconds = tm.tm_gmtoff;
  257. #endif
  258. return static_cast<int>(offset_seconds / 60);
  259. #endif
  260. }
  261. // Return current thread id as size_t
  262. // It exists because the std::this_thread::get_id() is much slower(especially
  263. // under VS 2013)
  264. SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT {
  265. #ifdef _WIN32
  266. return static_cast<size_t>(::GetCurrentThreadId());
  267. #elif defined(__linux__)
  268. #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
  269. #define SYS_gettid __NR_gettid
  270. #endif
  271. return static_cast<size_t>(::syscall(SYS_gettid));
  272. #elif defined(_AIX)
  273. struct __pthrdsinfo buf;
  274. int reg_size = 0;
  275. pthread_t pt = pthread_self();
  276. int retval = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &buf, sizeof(buf), NULL, &reg_size);
  277. int tid = (!retval) ? buf.__pi_tid : 0;
  278. return static_cast<size_t>(tid);
  279. #elif defined(__DragonFly__) || defined(__FreeBSD__)
  280. return static_cast<size_t>(::pthread_getthreadid_np());
  281. #elif defined(__NetBSD__)
  282. return static_cast<size_t>(::_lwp_self());
  283. #elif defined(__OpenBSD__)
  284. return static_cast<size_t>(::getthrid());
  285. #elif defined(__sun)
  286. return static_cast<size_t>(::thr_self());
  287. #elif __APPLE__
  288. uint64_t tid;
  289. // There is no pthread_threadid_np prior to Mac OS X 10.6, and it is not supported on any PPC,
  290. // including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64.
  291. #ifdef MAC_OS_X_VERSION_MAX_ALLOWED
  292. {
  293. #if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060) || defined(__POWERPC__)
  294. tid = pthread_mach_thread_np(pthread_self());
  295. #elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
  296. if (&pthread_threadid_np) {
  297. pthread_threadid_np(nullptr, &tid);
  298. } else {
  299. tid = pthread_mach_thread_np(pthread_self());
  300. }
  301. #else
  302. pthread_threadid_np(nullptr, &tid);
  303. #endif
  304. }
  305. #else
  306. pthread_threadid_np(nullptr, &tid);
  307. #endif
  308. return static_cast<size_t>(tid);
  309. #else // Default to standard C++11 (other Unix)
  310. return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
  311. #endif
  312. }
  313. // Return current thread id as size_t (from thread local storage)
  314. SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT {
  315. #if defined(SPDLOG_NO_TLS)
  316. return _thread_id();
  317. #else // cache thread id in tls
  318. static thread_local const size_t tid = _thread_id();
  319. return tid;
  320. #endif
  321. }
  322. // This is avoid msvc issue in sleep_for that happens if the clock changes.
  323. // See https://github.com/gabime/spdlog/issues/609
  324. SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT {
  325. #if defined(_WIN32)
  326. ::Sleep(milliseconds);
  327. #else
  328. std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
  329. #endif
  330. }
  331. // wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
  332. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  333. SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) {
  334. memory_buf_t buf;
  335. wstr_to_utf8buf(filename, buf);
  336. return SPDLOG_BUF_TO_STRING(buf);
  337. }
  338. #else
  339. SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { return filename; }
  340. #endif
  341. SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT {
  342. #ifdef _WIN32
  343. return conditional_static_cast<int>(::GetCurrentProcessId());
  344. #else
  345. return conditional_static_cast<int>(::getpid());
  346. #endif
  347. }
  348. // Determine if the terminal supports colors
  349. // Based on: https://github.com/agauniyal/rang/
  350. SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT {
  351. #ifdef _WIN32
  352. return true;
  353. #else
  354. static const bool result = []() {
  355. const char *env_colorterm_p = std::getenv("COLORTERM");
  356. if (env_colorterm_p != nullptr) {
  357. return true;
  358. }
  359. static constexpr std::array<const char *, 16> terms = {
  360. {"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys",
  361. "putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}};
  362. const char *env_term_p = std::getenv("TERM");
  363. if (env_term_p == nullptr) {
  364. return false;
  365. }
  366. return std::any_of(terms.begin(), terms.end(), [&](const char *term) {
  367. return std::strstr(env_term_p, term) != nullptr;
  368. });
  369. }();
  370. return result;
  371. #endif
  372. }
  373. // Determine if the terminal attached
  374. // Source: https://github.com/agauniyal/rang/
  375. SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT {
  376. #ifdef _WIN32
  377. return ::_isatty(_fileno(file)) != 0;
  378. #else
  379. return ::isatty(fileno(file)) != 0;
  380. #endif
  381. }
  382. #if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
  383. SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) {
  384. if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) / 4 - 1) {
  385. throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8");
  386. }
  387. int wstr_size = static_cast<int>(wstr.size());
  388. if (wstr_size == 0) {
  389. target.resize(0);
  390. return;
  391. }
  392. int result_size = static_cast<int>(target.capacity());
  393. if ((wstr_size + 1) * 4 > result_size) {
  394. result_size =
  395. ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
  396. }
  397. if (result_size > 0) {
  398. target.resize(result_size);
  399. result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(),
  400. result_size, NULL, NULL);
  401. if (result_size > 0) {
  402. target.resize(result_size);
  403. return;
  404. }
  405. }
  406. throw_spdlog_ex(
  407. fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
  408. }
  409. SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
  410. if (str.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) - 1) {
  411. throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16");
  412. }
  413. int str_size = static_cast<int>(str.size());
  414. if (str_size == 0) {
  415. target.resize(0);
  416. return;
  417. }
  418. // find the size to allocate for the result buffer
  419. int result_size =
  420. ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
  421. if (result_size > 0) {
  422. target.resize(result_size);
  423. result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size,
  424. target.data(), result_size);
  425. if (result_size > 0) {
  426. assert(result_size == target.size());
  427. return;
  428. }
  429. }
  430. throw_spdlog_ex(
  431. fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
  432. }
  433. #endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) &&
  434. // defined(_WIN32)
  435. // return true on success
  436. static SPDLOG_INLINE bool mkdir_(const filename_t &path) {
  437. #ifdef _WIN32
  438. #ifdef SPDLOG_WCHAR_FILENAMES
  439. return ::_wmkdir(path.c_str()) == 0;
  440. #else
  441. return ::_mkdir(path.c_str()) == 0;
  442. #endif
  443. #else
  444. return ::mkdir(path.c_str(), mode_t(0755)) == 0;
  445. #endif
  446. }
  447. // create the given directory - and all directories leading to it
  448. // return true on success or if the directory already exists
  449. SPDLOG_INLINE bool create_dir(const filename_t &path) {
  450. if (path_exists(path)) {
  451. return true;
  452. }
  453. if (path.empty()) {
  454. return false;
  455. }
  456. size_t search_offset = 0;
  457. do {
  458. auto token_pos = path.find_first_of(folder_seps_filename, search_offset);
  459. // treat the entire path as a folder if no folder separator not found
  460. if (token_pos == filename_t::npos) {
  461. token_pos = path.size();
  462. }
  463. auto subdir = path.substr(0, token_pos);
  464. #ifdef _WIN32
  465. // if subdir is just a drive letter, add a slash e.g. "c:"=>"c:\",
  466. // otherwise path_exists(subdir) returns false (issue #3079)
  467. const bool is_drive = subdir.length() == 2 && subdir[1] == ':';
  468. if (is_drive) {
  469. subdir += '\\';
  470. token_pos++;
  471. }
  472. #endif
  473. if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir)) {
  474. return false; // return error if failed creating dir
  475. }
  476. search_offset = token_pos + 1;
  477. } while (search_offset < path.size());
  478. return true;
  479. }
  480. // Return directory name from given path or empty string
  481. // "abc/file" => "abc"
  482. // "abc/" => "abc"
  483. // "abc" => ""
  484. // "abc///" => "abc//"
  485. SPDLOG_INLINE filename_t dir_name(const filename_t &path) {
  486. auto pos = path.find_last_of(folder_seps_filename);
  487. return pos != filename_t::npos ? path.substr(0, pos) : filename_t{};
  488. }
  489. std::string SPDLOG_INLINE getenv(const char *field) {
  490. #if defined(_MSC_VER)
  491. #if defined(__cplusplus_winrt)
  492. return std::string{}; // not supported under uwp
  493. #else
  494. size_t len = 0;
  495. char buf[128];
  496. bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0;
  497. return ok ? buf : std::string{};
  498. #endif
  499. #else // revert to getenv
  500. char *buf = ::getenv(field);
  501. return buf ? buf : std::string{};
  502. #endif
  503. }
  504. // Do fsync by FILE handlerpointer
  505. // Return true on success
  506. SPDLOG_INLINE bool fsync(FILE *fp) {
  507. #ifdef _WIN32
  508. return FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp)))) != 0;
  509. #else
  510. return ::fsync(fileno(fp)) == 0;
  511. #endif
  512. }
  513. } // namespace os
  514. } // namespace details
  515. } // namespace spdlog