6#ifndef SPDLOG_HEADER_ONLY
34# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
46# include <sys/syscall.h>
51# elif defined(__DragonFly__) || defined(__FreeBSD__)
52# include <pthread_np.h>
54# elif defined(__NetBSD__)
64# define __has_feature(x) 0
74#if defined __linux__ && defined SPDLOG_CLOCK_COARSE
76 ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
89 ::localtime_s(&tm, &time_tt);
92 ::localtime_r(&time_tt, &tm);
108 ::gmtime_s(&tm, &time_tt);
111 ::gmtime_r(&time_tt, &tm);
126# ifdef SPDLOG_WCHAR_FILENAMES
127 *fp = ::_wfsopen((filename.
c_str()), mode.
c_str(), _SH_DENYNO);
129 *fp = ::_fsopen((filename.
c_str()), mode.
c_str(), _SH_DENYNO);
131# if defined(SPDLOG_PREVENT_CHILD_FD)
134 auto file_handle =
reinterpret_cast<HANDLE
>(_get_osfhandle(::_fileno(*fp)));
135 if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
143# if defined(SPDLOG_PREVENT_CHILD_FD)
145 const int fd = ::open((filename.
c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
150 *fp = ::fdopen(fd, mode.
c_str());
156 *fp = ::fopen((filename.
c_str()), mode.
c_str());
160 return *fp ==
nullptr;
165#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
166 return ::_wremove(filename.c_str());
168 return std::remove(filename.c_str());
179#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
180 return ::_wrename(filename1.c_str(), filename2.c_str());
182 return std::rename(filename1.c_str(), filename2.c_str());
190# ifdef SPDLOG_WCHAR_FILENAMES
191 auto attribs = ::GetFileAttributesW(filename.c_str());
193 auto attribs = ::GetFileAttributesA(filename.c_str());
195 return attribs != INVALID_FILE_ATTRIBUTES;
198 return (::stat(filename.c_str(), &
buffer) == 0);
204# pragma warning(push)
205# pragma warning(disable : 4702)
215#if defined(_WIN32) && !defined(__CYGWIN__)
216 int fd = ::_fileno(f);
218 __int64 ret = ::_filelengthi64(fd);
221 return static_cast<size_t>(ret);
225 long ret = ::_filelength(fd);
228 return static_cast<size_t>(ret);
234# if defined(__OpenBSD__)
237 int fd = ::fileno(f);
240# if (defined(__linux__) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
242 if (::fstat64(fd, &st) == 0)
244 return static_cast<size_t>(st.st_size);
248 if (::fstat(fd, &st) == 0)
250 return static_cast<size_t>(st.st_size);
267# if _WIN32_WINNT < _WIN32_WINNT_WS08
268 TIME_ZONE_INFORMATION tzinfo;
269 auto rv = ::GetTimeZoneInformation(&tzinfo);
271 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
272 auto rv = ::GetDynamicTimeZoneInformation(&tzinfo);
274 if (rv == TIME_ZONE_ID_INVALID)
277 int offset = -tzinfo.Bias;
280 offset -= tzinfo.DaylightBias;
284 offset -= tzinfo.StandardBias;
289# if defined(sun) || defined(__sun) || defined(_AIX) || (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
293 static long int calculate_gmt_offset(
const std::tm &localtm = details::os::localtime(),
const std::tm &gmtm = details::os::gmtime())
295 int local_year = localtm.tm_year + (1900 - 1);
296 int gmt_year = gmtm.tm_year + (1900 - 1);
304 + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) +
305 ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
308 + (
long int)(local_year - gmt_year) * 365);
310 long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour);
311 long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
312 long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec);
318 auto offset_seconds = helper::calculate_gmt_offset(tm);
320 auto offset_seconds = tm.tm_gmtoff;
323 return static_cast<int>(offset_seconds / 60);
333 return static_cast<size_t>(::GetCurrentThreadId());
334#elif defined(__linux__)
335# if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
336# define SYS_gettid __NR_gettid
338 return static_cast<size_t>(::syscall(SYS_gettid));
339#elif defined(_AIX) || defined(__DragonFly__) || defined(__FreeBSD__)
340 return static_cast<size_t>(::pthread_getthreadid_np());
341#elif defined(__NetBSD__)
342 return static_cast<size_t>(::_lwp_self());
343#elif defined(__OpenBSD__)
344 return static_cast<size_t>(::getthrid());
346 return static_cast<size_t>(::thr_self());
349 pthread_threadid_np(
nullptr, &tid);
350 return static_cast<size_t>(tid);
359#if defined(SPDLOG_NO_TLS)
362 static thread_local const size_t tid =
_thread_id();
372 ::Sleep(milliseconds);
379#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
383 wstr_to_utf8buf(filename, buf);
384 return fmt::to_string(buf);
397 return static_cast<int>(::GetCurrentProcessId());
399 return static_cast<int>(::getpid());
411 static const bool result = []() {
412 const char *env_colorterm_p =
std::getenv(
"COLORTERM");
413 if (env_colorterm_p !=
nullptr)
419 "msys",
"putty",
"rxvt",
"screen",
"vt100",
"xterm",
"alacritty",
"vt102"}};
422 if (env_term_p ==
nullptr)
427 return std::any_of(terms.
begin(), terms.
end(), [&](
const char *term) { return std::strstr(env_term_p, term) != nullptr; });
440 return ::_isatty(_fileno(file)) != 0;
442 return ::isatty(fileno(file)) != 0;
446#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
454 int wstr_size =
static_cast<int>(wstr.size());
461 int result_size =
static_cast<int>(target.capacity());
462 if ((wstr_size + 1) * 2 > result_size)
464 result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);
469 target.resize(result_size);
470 result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size, NULL, NULL);
474 target.resize(result_size);
479 throw_spdlog_ex(fmt::format(
"WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
489 int str_size =
static_cast<int>(str.size());
496 int result_size =
static_cast<int>(target.capacity());
497 if (str_size + 1 > result_size)
499 result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
504 target.resize(result_size);
505 result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
509 target.resize(result_size);
514 throw_spdlog_ex(fmt::format(
"MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
522# ifdef SPDLOG_WCHAR_FILENAMES
523 return ::_wmkdir(path.
c_str()) == 0;
525 return ::_mkdir(path.
c_str()) == 0;
528 return ::mkdir(path.
c_str(), mode_t(0755)) == 0;
546 size_t search_offset = 0;
551 if (token_pos == filename_t::npos)
553 token_pos = path.
size();
556 auto subdir = path.
substr(0, token_pos);
562 search_offset = token_pos + 1;
563 }
while (search_offset < path.
size());
583# if defined(__cplusplus_winrt)
588 bool ok = ::getenv_s(&len, buf,
sizeof(buf), field) == 0;
#define SPDLOG_FILENAME_T(s)
T find_first_of(T... args)
T find_last_of(T... args)
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT
SPDLOG_INLINE size_t filesize(FILE *f)
SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT
std::string SPDLOG_INLINE getenv(const char *field)
static SPDLOG_INLINE bool mkdir_(const filename_t &path)
SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT
SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT
SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT
SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT
SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT
SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT
SPDLOG_INLINE filename_t dir_name(filename_t path)
SPDLOG_INLINE bool create_dir(filename_t path)
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
static SPDLOG_CONSTEXPR const filename_t::value_type folder_seps_filename[]
fmt::basic_memory_buffer< wchar_t, 250 > wmemory_buf_t
fmt::basic_string_view< wchar_t > wstring_view_t
fmt::basic_string_view< char > string_view_t
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
fmt::basic_memory_buffer< char, 250 > memory_buf_t