spdlog/include/spdlog/details/os.h

123 lines
3.8 KiB
C
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2017-11-06 18:39:04 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2019-05-12 01:06:17 +08:00
2017-11-06 18:39:04 +08:00
#pragma once
#include <spdlog/common.h>
2019-05-11 08:05:22 +08:00
#include <ctime> // std::time_t
2017-11-06 18:39:04 +08:00
2018-03-17 18:47:46 +08:00
namespace spdlog {
namespace details {
namespace os {
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API std::tm localtime() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
// eol definition
2018-03-09 21:26:33 +08:00
#if !defined(SPDLOG_EOL)
2021-07-19 05:50:51 +08:00
# ifdef _WIN32
# define SPDLOG_EOL "\r\n"
# else
# define SPDLOG_EOL "\n"
# endif
2017-11-06 18:39:04 +08:00
#endif
2018-03-09 21:26:33 +08:00
SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL;
// folder separator
#if !defined(SPDLOG_FOLDER_SEPS)
2021-07-19 05:50:51 +08:00
# ifdef _WIN32
# define SPDLOG_FOLDER_SEPS "\\/"
# else
# define SPDLOG_FOLDER_SEPS "/"
# endif
#endif
SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS;
SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
2018-03-09 21:26:33 +08:00
// fopen_s on non windows for writing
2020-03-10 03:02:16 +08:00
SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
2017-11-06 18:39:04 +08:00
2019-09-15 23:34:29 +08:00
// Remove filename. return 0 on success
2020-03-10 03:02:16 +08:00
SPDLOG_API int remove(const filename_t &filename) SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2019-09-15 23:34:29 +08:00
// Remove file if exists. return 0 on success
// Note: Non atomic (might return failure to delete if concurrently deleted by other process/thread)
2020-03-10 03:02:16 +08:00
SPDLOG_API int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
2019-09-15 23:34:29 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2019-09-15 23:34:29 +08:00
// Return if file exists.
2020-03-10 03:02:16 +08:00
SPDLOG_API bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2018-03-09 21:26:33 +08:00
// Return file size according to open FILE* object
2020-03-10 03:02:16 +08:00
SPDLOG_API size_t filesize(FILE *f);
2017-11-06 18:39:04 +08:00
2018-03-09 21:26:33 +08:00
// Return utc offset in minutes or throw spdlog_ex on failure
2020-03-10 03:02:16 +08:00
SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime());
2017-11-06 18:39:04 +08:00
2018-03-09 21:26:33 +08:00
// Return current thread id as size_t
2018-07-22 04:48:07 +08:00
// It exists because the std::this_thread::get_id() is much slower(especially
// under VS 2013)
2020-03-10 03:02:16 +08:00
SPDLOG_API size_t _thread_id() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2018-03-09 21:26:33 +08:00
// Return current thread id as size_t (from thread local storage)
2020-03-10 03:02:16 +08:00
SPDLOG_API size_t thread_id() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2018-01-12 20:09:07 +08:00
// This is avoid msvc issue in sleep_for that happens if the clock changes.
// See https://github.com/gabime/spdlog/issues/609
2021-06-13 20:17:47 +08:00
SPDLOG_API void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API std::string filename_to_str(const filename_t &filename);
2017-11-06 18:39:04 +08:00
2020-03-10 03:02:16 +08:00
SPDLOG_API int pid() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
// Determine if the terminal supports colors
2017-11-06 18:39:04 +08:00
// Source: https://github.com/agauniyal/rang/
2020-03-10 03:02:16 +08:00
SPDLOG_API bool is_color_terminal() SPDLOG_NOEXCEPT;
2017-11-06 18:39:04 +08:00
// Determine if the terminal attached
2017-11-06 18:39:04 +08:00
// Source: https://github.com/agauniyal/rang/
2020-03-10 03:02:16 +08:00
SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
2020-05-27 04:34:17 +08:00
SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target);
SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
#endif
2019-10-20 22:40:56 +08:00
// Return directory name from given path or empty string
// "abc/file" => "abc"
// "abc/" => "abc"
// "abc" => ""
2019-10-21 00:09:37 +08:00
// "abc///" => "abc//"
2021-10-19 00:06:05 +08:00
SPDLOG_API filename_t dir_name(const filename_t &path);
2019-10-20 22:40:56 +08:00
// Create a dir from the given path.
// Return true if succeeded or if this dir already exists.
2021-10-19 00:06:05 +08:00
SPDLOG_API bool create_dir(const filename_t &path);
2019-10-20 22:40:56 +08:00
2019-12-08 19:35:15 +08:00
// non thread safe, cross platform getenv/getenv_s
// return empty string if field not found
2020-03-10 03:02:16 +08:00
SPDLOG_API std::string getenv(const char *field);
2019-12-08 19:35:15 +08:00
2023-01-15 22:00:26 +08:00
// Do fsync by FILE objectpointer.
// Return true on success.
SPDLOG_API bool fsync(FILE * fp);
2023-01-15 21:33:40 +08:00
2018-03-17 18:47:46 +08:00
} // namespace os
} // namespace details
} // namespace spdlog
2019-04-05 21:44:17 +08:00
#ifdef SPDLOG_HEADER_ONLY
2021-07-19 05:50:51 +08:00
# include "os-inl.h"
2019-04-05 21:44:17 +08:00
#endif