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. 60 inline spdlog::log_clock::time_point now()
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)));
70 return log_clock::now();
73 inline std::tm localtime(
const std::time_t &time_tt)
78 localtime_s(&tm, &time_tt);
81 localtime_r(&time_tt, &tm);
86 inline std::tm localtime()
88 std::time_t now_t = time(
nullptr);
89 return localtime(now_t);
92 inline std::tm gmtime(
const std::time_t &time_tt)
97 gmtime_s(&tm, &time_tt);
100 gmtime_r(&time_tt, &tm);
105 inline std::tm gmtime()
107 std::time_t now_t = time(
nullptr);
108 return gmtime(now_t);
110 inline bool operator==(
const std::tm &tm1,
const std::tm &tm2)
112 return (tm1.tm_sec == tm2.tm_sec && tm1.tm_min == tm2.tm_min && tm1.tm_hour == tm2.tm_hour && tm1.tm_mday == tm2.tm_mday &&
113 tm1.tm_mon == tm2.tm_mon && tm1.tm_year == tm2.tm_year && tm1.tm_isdst == tm2.tm_isdst);
116 inline bool operator!=(
const std::tm &tm1,
const std::tm &tm2)
118 return !(tm1 == tm2);
122 #if !defined(SPDLOG_EOL) 124 #define SPDLOG_EOL "\r\n" 126 #define SPDLOG_EOL "\n" 130 SPDLOG_CONSTEXPR
static const char *default_eol = SPDLOG_EOL;
134 SPDLOG_CONSTEXPR
static const char folder_sep =
'\\';
136 SPDLOG_CONSTEXPR
static const char folder_sep =
'/';
139 inline void prevent_child_fd(FILE *f)
143 #if !defined(__cplusplus_winrt) 144 auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
145 if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
146 throw spdlog_ex(
"SetHandleInformation failed", errno);
150 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
152 throw spdlog_ex(
"fcntl with FD_CLOEXEC failed", errno);
158 inline bool fopen_s(FILE **fp,
const filename_t &filename,
const filename_t &mode)
161 #ifdef SPDLOG_WCHAR_FILENAMES 162 *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
164 *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
167 *fp = fopen((filename.c_str()), mode.c_str());
170 #ifdef SPDLOG_PREVENT_CHILD_FD 173 prevent_child_fd(*fp);
176 return *fp ==
nullptr;
179 inline int remove(
const filename_t &filename)
181 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 182 return _wremove(filename.c_str());
184 return std::remove(filename.c_str());
188 inline int rename(
const filename_t &filename1,
const filename_t &filename2)
190 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 191 return _wrename(filename1.c_str(), filename2.c_str());
193 return std::rename(filename1.c_str(), filename2.c_str());
198 inline bool file_exists(
const filename_t &filename)
201 #ifdef SPDLOG_WCHAR_FILENAMES 202 auto attribs = GetFileAttributesW(filename.c_str());
204 auto attribs = GetFileAttributesA(filename.c_str());
206 return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
207 #else // common linux/unix all have the stat system call 209 return (stat(filename.c_str(), &buffer) == 0);
214 inline size_t filesize(FILE *f)
218 throw spdlog_ex(
"Failed getting file size. fd is null");
220 #if defined(_WIN32) && !defined(__CYGWIN__) 222 #if _WIN64 // 64 bits 224 if (_fstat64(fd, &st) == 0)
229 #else // windows 32 bits 230 long ret = _filelength(fd);
233 return static_cast<size_t>(ret);
240 #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__) 242 if (fstat64(fd, &st) == 0)
244 return static_cast<size_t>(st.st_size);
246 #else // unix 32 bits or cygwin 249 if (fstat(fd, &st) == 0)
251 return static_cast<size_t>(st.st_size);
255 throw spdlog_ex(
"Failed getting file size from fd", errno);
259 inline int utc_minutes_offset(
const std::tm &tm = details::os::localtime())
263 #if _WIN32_WINNT < _WIN32_WINNT_WS08 264 TIME_ZONE_INFORMATION tzinfo;
265 auto rv = GetTimeZoneInformation(&tzinfo);
267 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
268 auto rv = GetDynamicTimeZoneInformation(&tzinfo);
270 if (rv == TIME_ZONE_ID_INVALID)
273 int offset = -tzinfo.Bias;
276 offset -= tzinfo.DaylightBias;
280 offset -= tzinfo.StandardBias;
285 #if defined(sun) || defined(__sun) || defined(_AIX) 289 static long int calculate_gmt_offset(
const std::tm &localtm = details::os::localtime(),
const std::tm &gmtm = details::os::gmtime())
291 int local_year = localtm.tm_year + (1900 - 1);
292 int gmt_year = gmtm.tm_year + (1900 - 1);
300 + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) +
301 ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
304 + (
long int)(local_year - gmt_year) * 365);
306 long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour);
307 long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min);
308 long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec);
314 auto offset_seconds = helper::calculate_gmt_offset(tm);
316 auto offset_seconds = tm.tm_gmtoff;
319 return static_cast<int>(offset_seconds / 60);
325 inline size_t _thread_id()
328 return static_cast<size_t>(::GetCurrentThreadId());
330 #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) 331 #define SYS_gettid __NR_gettid 333 return static_cast<size_t>(syscall(SYS_gettid));
337 return static_cast<size_t>(tid);
340 pthread_threadid_np(
nullptr, &tid);
341 return static_cast<size_t>(tid);
342 #else // Default to standard C++11 (other Unix) 343 return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
348 inline size_t thread_id()
350 #if defined(SPDLOG_DISABLE_TID_CACHING) || (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt) || \ 351 (defined(__clang__) && !__has_feature(cxx_thread_local)) 353 #else // cache thread id in tls 354 static thread_local
const size_t tid = _thread_id();
361 inline void sleep_for_millis(
int milliseconds)
364 ::Sleep(milliseconds);
366 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
371 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) 372 #define SPDLOG_FILENAME_T(s) L##s 373 inline std::string filename_to_str(
const filename_t &filename)
375 std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t> c;
376 return c.to_bytes(filename);
379 #define SPDLOG_FILENAME_T(s) s 380 inline std::string filename_to_str(
const filename_t &filename)
390 return static_cast<int>(::GetCurrentProcessId());
392 return static_cast<int>(::getpid());
398 inline bool is_color_terminal()
403 static constexpr
const char *Terms[] = {
404 "ansi",
"color",
"console",
"cygwin",
"gnome",
"konsole",
"kterm",
"linux",
"msys",
"putty",
"rxvt",
"screen",
"vt100",
"xterm"};
406 const char *env_p = std::getenv(
"TERM");
407 if (env_p ==
nullptr)
412 static const bool result =
413 std::any_of(std::begin(Terms), std::end(Terms), [&](
const char *term) {
return std::strstr(env_p, term) !=
nullptr; });
420 inline bool in_terminal(FILE *file)
424 return _isatty(_fileno(file)) != 0;
426 return isatty(fileno(file)) != 0;
Definition: lib/spdlog/common.h:146
Definition: async_logger.h:26