|
|
|
|
@ -4,7 +4,7 @@
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#ifndef SPDLOG_HEADER_ONLY
|
|
|
|
|
#include <spdlog/details/os.h>
|
|
|
|
|
#include <spdlog/details/os.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <spdlog/common.h>
|
|
|
|
|
@ -22,50 +22,50 @@
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <spdlog/details/windows_include.h>
|
|
|
|
|
#include <io.h> // for _get_osfhandle, _isatty, _fileno
|
|
|
|
|
#include <process.h> // for _get_pid
|
|
|
|
|
#include <spdlog/details/windows_include.h>
|
|
|
|
|
#include <io.h> // for _get_osfhandle, _isatty, _fileno
|
|
|
|
|
#include <process.h> // for _get_pid
|
|
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
#include <share.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
#include <share.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <direct.h> // for _mkdir/_wmkdir
|
|
|
|
|
#include <direct.h> // for _mkdir/_wmkdir
|
|
|
|
|
|
|
|
|
|
#else // unix
|
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
|
|
|
|
|
|
|
|
|
#elif defined(_AIX)
|
|
|
|
|
#include <pthread.h> // for pthread_getthrds_np
|
|
|
|
|
#elif defined(_AIX)
|
|
|
|
|
#include <pthread.h> // for pthread_getthrds_np
|
|
|
|
|
|
|
|
|
|
#elif defined(__DragonFly__) || defined(__FreeBSD__)
|
|
|
|
|
#include <pthread_np.h> // for pthread_getthreadid_np
|
|
|
|
|
#elif defined(__DragonFly__) || defined(__FreeBSD__)
|
|
|
|
|
#include <pthread_np.h> // for pthread_getthreadid_np
|
|
|
|
|
|
|
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
|
#include <lwp.h> // for _lwp_self
|
|
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
|
#include <lwp.h> // for _lwp_self
|
|
|
|
|
|
|
|
|
|
#elif defined(__sun)
|
|
|
|
|
#include <thread.h> // for thr_self
|
|
|
|
|
#endif
|
|
|
|
|
#elif defined(__sun)
|
|
|
|
|
#include <thread.h> // for thr_self
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // unix
|
|
|
|
|
|
|
|
|
|
#if defined __APPLE__
|
|
|
|
|
#include <AvailabilityMacros.h>
|
|
|
|
|
#include <AvailabilityMacros.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef __has_feature // Clang - feature checking macros.
|
|
|
|
|
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
|
|
|
|
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace spdlog {
|
|
|
|
|
@ -119,12 +119,12 @@ SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT {
|
|
|
|
|
// fopen_s on non windows for writing
|
|
|
|
|
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
|
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
|
|
|
|
*fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
*fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
|
|
|
|
if (*fp != nullptr) {
|
|
|
|
|
auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(::_fileno(*fp)));
|
|
|
|
|
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) {
|
|
|
|
|
@ -132,9 +132,9 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
|
|
|
|
|
*fp = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#else // unix
|
|
|
|
|
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
|
|
|
|
#if defined(SPDLOG_PREVENT_CHILD_FD)
|
|
|
|
|
const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
|
|
|
|
|
const int fd =
|
|
|
|
|
::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
|
|
|
|
|
@ -145,9 +145,9 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
|
|
|
|
|
if (*fp == nullptr) {
|
|
|
|
|
::close(fd);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
*fp = ::fopen((filename.c_str()), mode.c_str());
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return *fp == nullptr;
|
|
|
|
|
@ -177,11 +177,11 @@ SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename
|
|
|
|
|
SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
struct _stat buffer;
|
|
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
|
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
|
|
|
|
return (::_wstat(filename.c_str(), &buffer) == 0);
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
return (::_stat(filename.c_str(), &buffer) == 0);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#else // common linux/unix all have the stat system call
|
|
|
|
|
struct stat buffer;
|
|
|
|
|
return (::stat(filename.c_str(), &buffer) == 0);
|
|
|
|
|
@ -189,9 +189,9 @@ SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
// avoid warning about unreachable statement at the end of filesize()
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4702)
|
|
|
|
|
// avoid warning about unreachable statement at the end of filesize()
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4702)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Return file size according to open FILE* object
|
|
|
|
|
@ -201,59 +201,59 @@ SPDLOG_INLINE size_t filesize(FILE *f) {
|
|
|
|
|
}
|
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
|
int fd = ::_fileno(f);
|
|
|
|
|
#if defined(_WIN64) // 64 bits
|
|
|
|
|
#if defined(_WIN64) // 64 bits
|
|
|
|
|
__int64 ret = ::_filelengthi64(fd);
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
|
return static_cast<size_t>(ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else // windows 32 bits
|
|
|
|
|
#else // windows 32 bits
|
|
|
|
|
long ret = ::_filelength(fd);
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
|
return static_cast<size_t>(ret);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#else // unix
|
|
|
|
|
// OpenBSD and AIX doesn't compile with :: before the fileno(..)
|
|
|
|
|
#if defined(__OpenBSD__) || defined(_AIX)
|
|
|
|
|
// OpenBSD and AIX doesn't compile with :: before the fileno(..)
|
|
|
|
|
#if defined(__OpenBSD__) || defined(_AIX)
|
|
|
|
|
int fd = fileno(f);
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
int fd = ::fileno(f);
|
|
|
|
|
#endif
|
|
|
|
|
// 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated)
|
|
|
|
|
#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && \
|
|
|
|
|
#endif
|
|
|
|
|
// 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated)
|
|
|
|
|
#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && \
|
|
|
|
|
(defined(__LP64__) || defined(_LP64))
|
|
|
|
|
struct stat64 st;
|
|
|
|
|
if (::fstat64(fd, &st) == 0) {
|
|
|
|
|
return static_cast<size_t>(st.st_size);
|
|
|
|
|
}
|
|
|
|
|
#else // other unix or linux 32 bits or cygwin
|
|
|
|
|
#else // other unix or linux 32 bits or cygwin
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (::fstat(fd, &st) == 0) {
|
|
|
|
|
return static_cast<size_t>(st.st_size);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
throw_spdlog_ex("Failed getting file size from fd", errno);
|
|
|
|
|
return 0; // will not be reached.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Return utc offset in minutes or throw spdlog_ex on failure
|
|
|
|
|
#if !defined(SPDLOG_NO_TZ_OFFSET)
|
|
|
|
|
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#if _WIN32_WINNT < _WIN32_WINNT_WS08
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#if _WIN32_WINNT < _WIN32_WINNT_WS08
|
|
|
|
|
TIME_ZONE_INFORMATION tzinfo;
|
|
|
|
|
auto rv = ::GetTimeZoneInformation(&tzinfo);
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
|
|
|
|
|
auto rv = ::GetDynamicTimeZoneInformation(&tzinfo);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
if (rv == TIME_ZONE_ID_INVALID) throw_spdlog_ex("Failed getting timezone info. ", errno);
|
|
|
|
|
|
|
|
|
|
int offset = -tzinfo.Bias;
|
|
|
|
|
@ -263,10 +263,10 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
|
|
|
|
|
offset -= tzinfo.StandardBias;
|
|
|
|
|
}
|
|
|
|
|
return offset;
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
auto offset_seconds = tm.tm_gmtoff;
|
|
|
|
|
return static_cast<int>(offset_seconds / 60);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif // SPDLOG_NO_TZ_OFFSET
|
|
|
|
|
|
|
|
|
|
@ -277,9 +277,9 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
return static_cast<size_t>(::GetCurrentThreadId());
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
#if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
|
|
|
|
|
#define SYS_gettid __NR_gettid
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
|
|
|
|
|
#define SYS_gettid __NR_gettid
|
|
|
|
|
#endif
|
|
|
|
|
return static_cast<size_t>(::syscall(SYS_gettid));
|
|
|
|
|
#elif defined(_AIX)
|
|
|
|
|
struct __pthrdsinfo buf;
|
|
|
|
|
@ -298,25 +298,25 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT {
|
|
|
|
|
return static_cast<size_t>(::thr_self());
|
|
|
|
|
#elif __APPLE__
|
|
|
|
|
uint64_t tid;
|
|
|
|
|
// There is no pthread_threadid_np prior to Mac OS X 10.6, and it is not supported on any PPC,
|
|
|
|
|
// including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64.
|
|
|
|
|
#ifdef MAC_OS_X_VERSION_MAX_ALLOWED
|
|
|
|
|
// There is no pthread_threadid_np prior to Mac OS X 10.6, and it is not supported on any PPC,
|
|
|
|
|
// including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64.
|
|
|
|
|
#ifdef MAC_OS_X_VERSION_MAX_ALLOWED
|
|
|
|
|
{
|
|
|
|
|
#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060) || defined(__POWERPC__)
|
|
|
|
|
#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060) || defined(__POWERPC__)
|
|
|
|
|
tid = pthread_mach_thread_np(pthread_self());
|
|
|
|
|
#elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
|
|
|
|
|
#elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
|
|
|
|
|
if (&pthread_threadid_np) {
|
|
|
|
|
pthread_threadid_np(nullptr, &tid);
|
|
|
|
|
} else {
|
|
|
|
|
tid = pthread_mach_thread_np(pthread_self());
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
pthread_threadid_np(nullptr, &tid);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
pthread_threadid_np(nullptr, &tid);
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
return static_cast<size_t>(tid);
|
|
|
|
|
#else // Default to standard C++11 (other Unix)
|
|
|
|
|
return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
|
|
|
|
|
@ -469,11 +469,11 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
|
|
|
|
// return true on success
|
|
|
|
|
static SPDLOG_INLINE bool mkdir_(const filename_t &path) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
|
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
|
|
|
|
return ::_wmkdir(path.c_str()) == 0;
|
|
|
|
|
#else
|
|
|
|
|
#else
|
|
|
|
|
return ::_mkdir(path.c_str()) == 0;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#else
|
|
|
|
|
return ::mkdir(path.c_str(), mode_t(0755)) == 0;
|
|
|
|
|
#endif
|
|
|
|
|
@ -529,8 +529,8 @@ SPDLOG_INLINE filename_t dir_name(const filename_t &path) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4996)
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4996)
|
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
std::string SPDLOG_INLINE getenv(const char *field) {
|
|
|
|
|
#if defined(_MSC_VER) && defined(WINAPI_FAMILY) && defined(WINAPI_FAMILY_DESKTOP_APP) && \
|
|
|
|
|
@ -542,7 +542,7 @@ std::string SPDLOG_INLINE getenv(const char *field) {
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
|
|
// Do fsync by FILE handlerpointer
|
|
|
|
|
|