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