6#ifndef SPDLOG_HEADER_ONLY
35#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
48#include <sys/syscall.h>
53#elif defined(__DragonFly__) || defined(__FreeBSD__)
54#include <pthread_np.h>
56#elif defined(__NetBSD__)
66#define __has_feature(x) 0
79#if defined __linux__ && defined SPDLOG_CLOCK_COARSE
81 ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
82 return std::chrono::time_point<log_clock, typename log_clock::duration>(
83 std::chrono::duration_cast<typename log_clock::duration>(std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec)));
86 return log_clock::now();
94 ::localtime_s(&tm, &time_tt);
97 ::localtime_r(&time_tt, &tm);
104 std::time_t now_t =
::time(
nullptr);
113 ::gmtime_s(&tm, &time_tt);
116 ::gmtime_r(&time_tt, &tm);
123 std::time_t now_t =
::time(
nullptr);
131#ifdef SPDLOG_WCHAR_FILENAMES
132 *
fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
134 *
fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
136#if defined(SPDLOG_PREVENT_CHILD_FD)
139 auto file_handle =
reinterpret_cast<HANDLE
>(_get_osfhandle(::_fileno(*
fp)));
140 if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
148#if defined(SPDLOG_PREVENT_CHILD_FD)
150 const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
155 *
fp = ::fdopen(fd, mode.c_str());
161 *
fp = ::fopen((filename.c_str()), mode.c_str());
165 return *
fp ==
nullptr;
170#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
171 return ::_wremove(filename.c_str());
173 return std::remove(filename.c_str());
184#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
185 return ::_wrename(filename1.c_str(), filename2.c_str());
187 return std::rename(filename1.c_str(), filename2.c_str());
195#ifdef SPDLOG_WCHAR_FILENAMES
196 auto attribs = ::GetFileAttributesW(filename.c_str());
198 auto attribs = ::GetFileAttributesA(filename.c_str());
200 return attribs != INVALID_FILE_ATTRIBUTES;
203 return (::stat(filename.c_str(), &
buffer) == 0);
210#pragma warning(disable : 4702)
220#if defined(_WIN32) && !defined(__CYGWIN__)
221 int fd = ::_fileno(f);
223 __int64 ret = ::_filelengthi64(fd);
226 return static_cast<size_t>(ret);
230 long ret = ::_filelength(fd);
233 return static_cast<size_t>(ret);
239#if defined(__OpenBSD__) || defined(_AIX)
242 int fd = ::fileno(f);
245#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
247 if (::fstat64(fd, &st) == 0)
249 return static_cast<size_t>(st.st_size);
253 if (::fstat(fd, &st) == 0)
255 return static_cast<size_t>(st.st_size);
272#if _WIN32_WINNT < _WIN32_WINNT_WS08
273 TIME_ZONE_INFORMATION tzinfo;
274 auto rv = ::GetTimeZoneInformation(&tzinfo);
276 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
277 auto rv = ::GetDynamicTimeZoneInformation(&tzinfo);
279 if (rv == TIME_ZONE_ID_INVALID)
282 int offset = -tzinfo.Bias;
285 offset -= tzinfo.DaylightBias;
289 offset -= tzinfo.StandardBias;
294#if defined(sun) || defined(__sun) || defined(_AIX) || (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
298 static long int calculate_gmt_offset(
const std::tm& localtm = details::os::localtime(),
const std::tm& gmtm = details::os::gmtime())
300 int local_year = localtm.tm_year + (1900 - 1);
301 int gmt_year = gmtm.tm_year + (1900 - 1);
309 + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) +
310 ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
313 +
static_cast<long int>(local_year - gmt_year) * 365);
315 long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour);
316 long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
317 long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec);
323 auto offset_seconds = helper::calculate_gmt_offset(tm);
325 auto offset_seconds = tm.tm_gmtoff;
328 return static_cast<int>(offset_seconds / 60);
338 return static_cast<size_t>(::GetCurrentThreadId());
339#elif defined(__linux__)
340#if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
341#define SYS_gettid __NR_gettid
343 return static_cast<size_t>(::syscall(SYS_gettid));
345 struct __pthrdsinfo buf;
347 pthread_t pt = pthread_self();
348 int retval = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &buf,
sizeof(buf),
NULL, ®_size);
349 int tid = (!retval) ? buf.__pi_tid : 0;
350 return static_cast<size_t>(tid);
351#elif defined(__DragonFly__) || defined(__FreeBSD__)
352 return static_cast<size_t>(::pthread_getthreadid_np());
353#elif defined(__NetBSD__)
354 return static_cast<size_t>(::_lwp_self());
355#elif defined(__OpenBSD__)
356 return static_cast<size_t>(::getthrid());
358 return static_cast<size_t>(::thr_self());
361 pthread_threadid_np(
nullptr, &tid);
362 return static_cast<size_t>(tid);
364 return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
371#if defined(SPDLOG_NO_TLS)
374 static thread_local const size_t tid =
_thread_id();
384 ::Sleep(milliseconds);
386 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
391#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
395 wstr_to_utf8buf(filename, buf);
409 return conditional_static_cast<int>(::GetCurrentProcessId());
411 return conditional_static_cast<int>(::getpid());
423 static const bool result = []() {
424 const char* env_colorterm_p = std::getenv(
"COLORTERM");
425 if (env_colorterm_p !=
nullptr)
430 static constexpr std::array<const char*, 16> terms = {{
"ansi",
"color",
"console",
"cygwin",
"gnome",
"konsole",
"kterm",
"linux",
431 "msys",
"putty",
"rxvt",
"screen",
"vt100",
"xterm",
"alacritty",
"vt102"}};
433 const char* env_term_p = std::getenv(
"TERM");
434 if (env_term_p ==
nullptr)
439 return std::any_of(terms.begin(), terms.end(), [&](
const char* term) { return std::strstr(env_term_p, term) != nullptr; });
452 return ::_isatty(_fileno(file)) != 0;
454 return ::isatty(fileno(file)) != 0;
458#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
461 if (wstr.size() >
static_cast<size_t>((std::numeric_limits<int>::max)()) / 2 - 1)
466 int wstr_size =
static_cast<int>(wstr.size());
473 int result_size =
static_cast<int>(target.capacity());
474 if ((wstr_size + 1) * 2 > result_size)
476 result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size,
NULL, 0,
NULL,
NULL);
481 target.resize(result_size);
482 result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), result_size,
NULL,
NULL);
486 target.resize(result_size);
491 throw_spdlog_ex(fmt_lib::format(
"WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
496 if (str.size() >
static_cast<size_t>((std::numeric_limits<int>::max)()) - 1)
501 int str_size =
static_cast<int>(str.size());
509 int result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size,
NULL, 0);
513 target.resize(result_size);
514 result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
517 assert(result_size == target.size());
522 throw_spdlog_ex(fmt_lib::format(
"MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
530#ifdef SPDLOG_WCHAR_FILENAMES
531 return ::_wmkdir(path.c_str()) == 0;
533 return ::_mkdir(path.c_str()) == 0;
536 return ::mkdir(path.c_str(), mode_t(0755)) == 0;
554 size_t search_offset = 0;
557 auto token_pos = path.find_first_of(folder_seps_filename, search_offset);
559 if (token_pos == filename_t::npos)
561 token_pos = path.size();
564 auto subdir = path.substr(0, token_pos);
566 if (!subdir.empty() && !
path_exists(subdir) && !mkdir_(subdir))
570 search_offset = token_pos + 1;
571 }
while (search_offset < path.size());
583 auto pos = path.find_last_of(folder_seps_filename);
584 return pos != filename_t::npos ? path.substr(0, pos) :
filename_t{};
591#if defined(__cplusplus_winrt)
592 return std::string{};
596 bool ok = ::getenv_s(&len, buf,
sizeof(buf), field) == 0;
597 return ok ? buf : std::string{};
601 return buf ? buf : std::string{};
610 return FlushFileBuffers(
reinterpret_cast<HANDLE
>(_get_osfhandle(_fileno(
fp)))) != 0;
612 return ::fsync(fileno(
fp)) == 0;
#define SPDLOG_NOEXCEPT
Definition common.h:69
#define SPDLOG_BUF_TO_STRING(x)
Definition common.h:197
#define SPDLOG_FILENAME_T(s)
Definition common.h:132
#define SPDLOG_INLINE
Definition common.h:47
#define offset(member)
Definition css_properties.cpp:5
SPDLOG_INLINE filename_t dir_name(const filename_t &path)
Definition os-inl.h:581
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
Definition os-inl.h:399
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
Definition os-inl.h:128
SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT
Definition os-inl.h:177
SPDLOG_INLINE size_t filesize(FILE *f)
Definition os-inl.h:214
SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT
Definition os-inl.h:417
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
Definition os-inl.h:268
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
Definition os-inl.h:168
SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
Definition os-inl.h:448
SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT
Definition os-inl.h:335
std::string SPDLOG_INLINE getenv(const char *field)
Definition os-inl.h:587
SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
Definition os-inl.h:182
SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT
Definition os-inl.h:76
SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT
Definition os-inl.h:102
SPDLOG_INLINE bool create_dir(const filename_t &path)
Definition os-inl.h:542
SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT
Definition os-inl.h:369
SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT
Definition os-inl.h:405
SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT
Definition os-inl.h:192
SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT
Definition os-inl.h:121
SPDLOG_INLINE bool fsync(FILE *fp)
Definition os-inl.h:607
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
Definition os-inl.h:381
fmt::basic_string_view< char > string_view_t
Definition common.h:172
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
std::string filename_t
Definition common.h:131
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
#define NULL
Definition strtod.cpp:30
annotation details
Definition tag_strings.h:125
time
Definition tag_strings.h:53