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