19 #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. 64 inline spdlog::log_clock::time_point
now()
67 #if defined __linux__ && defined SPDLOG_CLOCK_COARSE 69 ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
70 return std::chrono::time_point<log_clock, typename log_clock::duration>(
71 std::chrono::duration_cast<
typename log_clock::duration>(
72 std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec)));
80 inline std::tm
localtime(
const std::time_t &time_tt)
85 localtime_s(&tm, &time_tt);
88 localtime_r(&time_tt, &tm);
95 std::time_t now_t =
time(
nullptr);
100 inline std::tm
gmtime(
const std::time_t &time_tt)
105 gmtime_s(&tm, &time_tt);
108 gmtime_r(&time_tt, &tm);
115 std::time_t now_t =
time(
nullptr);
118 inline bool operator==(
const std::tm& tm1,
const std::tm& tm2)
120 return (tm1.tm_sec == tm2.tm_sec &&
121 tm1.tm_min == tm2.tm_min &&
122 tm1.tm_hour == tm2.tm_hour &&
123 tm1.tm_mday == tm2.tm_mday &&
124 tm1.tm_mon == tm2.tm_mon &&
125 tm1.tm_year == tm2.tm_year &&
126 tm1.tm_isdst == tm2.tm_isdst);
129 inline bool operator!=(
const std::tm& tm1,
const std::tm& tm2)
131 return !(tm1 == tm2);
135 #if !defined (SPDLOG_EOL) 137 #define SPDLOG_EOL "\r\n" 139 #define SPDLOG_EOL "\n" 149 auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
150 if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
151 throw spdlog_ex(
"SetHandleInformation failed", errno);
154 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
155 throw spdlog_ex(
"fcntl with FD_CLOEXEC failed", errno);
164 #ifdef SPDLOG_WCHAR_FILENAMES 165 *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
167 *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
170 *fp = fopen((filename.c_str()), mode.c_str());
173 #ifdef SPDLOG_PREVENT_CHILD_FD 177 return *fp ==
nullptr;
183 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 184 return _wremove(filename.c_str());
192 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 193 return _wrename(filename1.c_str(), filename2.c_str());
195 return std::rename(filename1.c_str(), filename2.c_str());
204 #ifdef SPDLOG_WCHAR_FILENAMES 205 auto attribs = GetFileAttributesW(filename.c_str());
207 auto attribs = GetFileAttributesA(filename.c_str());
209 return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
210 #else //common linux/unix all have the stat system call 212 return (stat(filename.c_str(), &buffer) == 0);
223 throw spdlog_ex(
"Failed getting file size. fd is null");
228 if (_fstat64(fd, &st) == 0)
231 #else //windows 32 bits 232 long ret = _filelength(fd);
234 return static_cast<size_t>(ret);
240 #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) 242 if (fstat64(fd, &st) == 0)
243 return static_cast<size_t>(st.st_size);
244 #else // unix 32 bits or osx 246 if (fstat(fd, &st) == 0)
247 return static_cast<size_t>(st.st_size);
250 throw spdlog_ex(
"Failed getting file size from fd", errno);
261 #if _WIN32_WINNT < _WIN32_WINNT_WS08 262 TIME_ZONE_INFORMATION tzinfo;
263 auto rv = GetTimeZoneInformation(&tzinfo);
265 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
266 auto rv = GetDynamicTimeZoneInformation(&tzinfo);
268 if (rv == TIME_ZONE_ID_INVALID)
271 int offset = -tzinfo.Bias;
273 offset -= tzinfo.DaylightBias;
275 offset -= tzinfo.StandardBias;
279 #if defined(sun) || defined(__sun) 285 int local_year = localtm.tm_year + (1900 - 1);
286 int gmt_year = gmtm.tm_year + (1900 - 1);
290 localtm.tm_yday - gmtm.tm_yday
293 + ((local_year >> 2) - (gmt_year >> 2))
294 - (local_year / 100 - gmt_year / 100)
295 + ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
298 + (
long int)(local_year - gmt_year) * 365
301 long int hours = (24 *
days) + (localtm.tm_hour - gmtm.tm_hour);
302 long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
303 long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec);
309 long int offset_seconds = helper::calculate_gmt_offset(tm);
311 long int offset_seconds = tm.tm_gmtoff;
314 return static_cast<int>(offset_seconds / 60);
323 return static_cast<size_t>(::GetCurrentThreadId());
325 # if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) 326 # define SYS_gettid __NR_gettid 328 return static_cast<size_t>(syscall(SYS_gettid));
332 return static_cast<size_t>(tid);
335 pthread_threadid_np(
nullptr, &tid);
336 return static_cast<size_t>(tid);
337 #else //Default to standard C++11 (other Unix) 338 return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
345 #if defined(_MSC_VER) && (_MSC_VER < 1900) || defined(__clang__) && !__has_feature(cxx_thread_local) 348 static thread_local
const size_t tid =
_thread_id();
357 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 358 #define SPDLOG_FILENAME_T(s) L ## s 361 std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
c;
362 return c.to_bytes(filename);
365 #define SPDLOG_FILENAME_T(s) s 385 return "Unknown error";
399 return "Unknown error";
401 #elif defined(__FreeBSD__) || defined(__APPLE__) || defined(ANDROID) || defined(__SUNPRO_CC) || \ 402 ((_POSIX_C_SOURCE >= 200112L) && ! defined(_GNU_SOURCE)) // posix version 407 return "Unknown error";
409 #else // gnu version (might not use the given buf, so its retval pointer must be used) 421 return static_cast<int>(::getpid());
434 static constexpr
const char* Terms[] =
436 "ansi",
"color",
"console",
"cygwin",
"gnome",
"konsole",
"kterm",
437 "linux",
"msys",
"putty",
"rxvt",
"screen",
"vt100",
"xterm" 440 const char *env_p = std::getenv(
"TERM");
441 if (env_p ==
nullptr)
446 static const bool result = std::any_of(
447 std::begin(Terms),
std::end(Terms), [&](
const char* term)
449 return std::strstr(env_p, term) !=
nullptr;
462 return _isatty(_fileno(file)) ?
true :
false;
464 return isatty(fileno(file)) ?
true :
false;
int rename(const filename_t &filename1, const filename_t &filename2)
bool file_exists(const filename_t &filename)
static SPDLOG_CONSTEXPR int eol_size
int remove(const filename_t &filename)
spdlog::log_clock::time_point now()
bool in_terminal(FILE *file)
int utc_minutes_offset(const std::tm &tm=details::os::localtime())
void prevent_child_fd(FILE *f)
std::string errno_str(int err_num)
std::tm localtime(const std::time_t &time_tt)
std::tm gmtime(const std::time_t &time_tt)
static SPDLOG_CONSTEXPR const char * eol
int fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
static const days_array & days()
bool operator!=(const std::tm &tm1, const std::tm &tm2)
std::string errno_to_string(char[256], char *res)
std::string filename_to_str(const filename_t &filename)
bool operator==(const std::tm &tm1, const std::tm &tm2)