18 #include <sys/types.h> 24 #define NOMINMAX // prevent windows redefining min/max 27 #ifndef WIN32_LEAN_AND_MEAN 28 #define WIN32_LEAN_AND_MEAN 44 #include <sys/syscall.h> 52 #ifndef __has_feature // Clang - feature checking macros. 53 #define __has_feature(x) 0 // Compatibility with non-clang compilers. 63 #if defined __linux__ && defined SPDLOG_CLOCK_COARSE 65 ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
66 return std::chrono::time_point<log_clock, typename log_clock::duration>(
67 std::chrono::duration_cast<
typename log_clock::duration>(std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec)));
88 std::time_t now_t = time(
nullptr);
107 std::time_t now_t = time(
nullptr);
112 #if !defined(SPDLOG_EOL) 114 #define SPDLOG_EOL "\r\n" 116 #define SPDLOG_EOL "\n" 133 #if !defined(__cplusplus_winrt) 134 auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
135 if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
136 throw spdlog_ex(
"SetHandleInformation failed", errno);
140 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
142 throw spdlog_ex(
"fcntl with FD_CLOEXEC failed", errno);
151 #ifdef SPDLOG_WCHAR_FILENAMES 152 *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
154 *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
157 *fp = fopen((filename.c_str()), mode.c_str());
160 #ifdef SPDLOG_PREVENT_CHILD_FD 166 return *fp ==
nullptr;
171 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 172 return _wremove(filename.c_str());
180 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 181 return _wrename(filename1.c_str(), filename2.c_str());
183 return std::rename(filename1.c_str(), filename2.c_str());
191 #ifdef SPDLOG_WCHAR_FILENAMES 192 auto attribs = GetFileAttributesW(filename.c_str());
194 auto attribs = GetFileAttributesA(filename.c_str());
196 return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
197 #else // common linux/unix all have the stat system call 199 return (stat(filename.c_str(), &
buffer) == 0);
208 throw spdlog_ex(
"Failed getting file size. fd is null");
210 #if defined(_WIN32) && !defined(__CYGWIN__) 212 #if _WIN64 // 64 bits 213 __int64 ret = _filelengthi64(fd);
216 return static_cast<size_t>(ret);
219 #else // windows 32 bits 220 long ret = _filelength(fd);
223 return static_cast<size_t>(ret);
230 #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__) 232 if (fstat64(fd, &st) == 0)
234 return static_cast<size_t>(st.st_size);
236 #else // unix 32 bits or cygwin 239 if (fstat(fd, &st) == 0)
241 return static_cast<size_t>(st.st_size);
245 throw spdlog_ex(
"Failed getting file size from fd", errno);
253 #if _WIN32_WINNT < _WIN32_WINNT_WS08 254 TIME_ZONE_INFORMATION tzinfo;
255 auto rv = GetTimeZoneInformation(&tzinfo);
257 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
258 auto rv = GetDynamicTimeZoneInformation(&tzinfo);
260 if (rv == TIME_ZONE_ID_INVALID)
263 int offset = -tzinfo.Bias;
266 offset -= tzinfo.DaylightBias;
270 offset -= tzinfo.StandardBias;
275 #if defined(sun) || defined(__sun) || defined(_AIX) 281 int local_year = localtm.tm_year + (1900 - 1);
282 int gmt_year = gmtm.tm_year + (1900 - 1);
290 + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) +
291 ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
294 + (
long int)(local_year - gmt_year) * 365);
296 long int hours = (24 *
days) + (localtm.tm_hour - gmtm.tm_hour);
297 long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
298 long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec);
304 auto offset_seconds = helper::calculate_gmt_offset(tm);
306 auto offset_seconds = tm.tm_gmtoff;
309 return static_cast<int>(offset_seconds / 60);
319 return static_cast<size_t>(::GetCurrentThreadId());
321 #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) 322 #define SYS_gettid __NR_gettid 324 return static_cast<size_t>(syscall(SYS_gettid));
328 return static_cast<size_t>(tid);
331 pthread_threadid_np(
nullptr, &tid);
332 return static_cast<size_t>(tid);
333 #else // Default to standard C++11 (other Unix) 334 return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
341 #if defined(SPDLOG_NO_TLS) 343 #else // cache thread id in tls 344 static thread_local
const size_t tid =
_thread_id();
354 ::Sleep(milliseconds);
356 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
361 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 362 #define SPDLOG_FILENAME_T(s) L##s 365 std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t> c;
366 return c.to_bytes(filename);
369 #define SPDLOG_FILENAME_T(s) s 380 return static_cast<int>(::GetCurrentProcessId());
382 return static_cast<int>(::getpid());
393 static constexpr
const char *Terms[] = {
394 "ansi",
"color",
"console",
"cygwin",
"gnome",
"konsole",
"kterm",
"linux",
"msys",
"putty",
"rxvt",
"screen",
"vt100",
"xterm"};
396 const char *env_p = std::getenv(
"TERM");
397 if (env_p ==
nullptr)
402 static const bool result =
403 std::any_of(
std::begin(Terms),
std::end(Terms), [&](
const char *term) {
return std::strstr(env_p, term) !=
nullptr; });
414 return _isatty(_fileno(file)) != 0;
416 return isatty(fileno(file)) != 0;
auto begin(const C &c) -> decltype(c.begin())
spdlog::log_clock::time_point now() noexcept
void sleep_for_millis(int milliseconds) noexcept
std::tm localtime(const std::time_t &time_tt) noexcept
bool is_color_terminal() noexcept
std::tm gmtime(const std::time_t &time_tt) noexcept
int utc_minutes_offset(const std::tm &tm=details::os::localtime())
void prevent_child_fd(FILE *f)
size_t thread_id() noexcept
int remove(const filename_t &filename) noexcept
basic_buffer< char > buffer
static const char * days[]
bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
bool in_terminal(FILE *file) noexcept
int rename(const filename_t &filename1, const filename_t &filename2) noexcept
size_t _thread_id() noexcept
static constexpr const char * default_eol
bool file_exists(const filename_t &filename) noexcept
static constexpr const char folder_sep
std::string filename_to_str(const filename_t &filename)
auto end(const C &c) -> decltype(c.end())