Functions | Variables
spdlog::details::os Namespace Reference

Functions

spdlog::log_clock::time_point now () SPDLOG_NOEXCEPT
 
std::tm localtime (const std::time_t &time_tt) SPDLOG_NOEXCEPT
 
std::tm localtime () SPDLOG_NOEXCEPT
 
std::tm gmtime (const std::time_t &time_tt) SPDLOG_NOEXCEPT
 
std::tm gmtime () SPDLOG_NOEXCEPT
 
void prevent_child_fd (FILE *f)
 
bool fopen_s (FILE **fp, const filename_t &filename, const filename_t &mode)
 
int remove (const filename_t &filename) SPDLOG_NOEXCEPT
 
int rename (const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
 
bool file_exists (const filename_t &filename) SPDLOG_NOEXCEPT
 
size_t filesize (FILE *f)
 
int utc_minutes_offset (const std::tm &tm=details::os::localtime())
 
size_t _thread_id () SPDLOG_NOEXCEPT
 
size_t thread_id () SPDLOG_NOEXCEPT
 
void sleep_for_millis (int milliseconds) SPDLOG_NOEXCEPT
 
std::string filename_to_str (const filename_t &filename)
 
int pid ()
 
bool is_color_terminal () SPDLOG_NOEXCEPT
 
bool in_terminal (FILE *file) SPDLOG_NOEXCEPT
 

Variables

static SPDLOG_CONSTEXPR const char * default_eol = SPDLOG_EOL
 
static SPDLOG_CONSTEXPR const char folder_sep = '/'
 

Function Documentation

size_t spdlog::details::os::_thread_id ( )
inline

Definition at line 316 of file os.h.

317 {
318 #ifdef _WIN32
319  return static_cast<size_t>(::GetCurrentThreadId());
320 #elif __linux__
321 #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
322 #define SYS_gettid __NR_gettid
323 #endif
324  return static_cast<size_t>(syscall(SYS_gettid));
325 #elif __FreeBSD__
326  long tid;
327  thr_self(&tid);
328  return static_cast<size_t>(tid);
329 #elif __APPLE__
330  uint64_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()));
335 #endif
336 }
unsigned __int64 uint64_t
Definition: stdint.h:136
bool spdlog::details::os::file_exists ( const filename_t filename)
inline

Definition at line 188 of file os.h.

189 {
190 #ifdef _WIN32
191 #ifdef SPDLOG_WCHAR_FILENAMES
192  auto attribs = GetFileAttributesW(filename.c_str());
193 #else
194  auto attribs = GetFileAttributesA(filename.c_str());
195 #endif
196  return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
197 #else // common linux/unix all have the stat system call
198  struct stat buffer;
199  return (stat(filename.c_str(), &buffer) == 0);
200 #endif
201 }
string filename
Definition: train.py:213
internal::basic_buffer< FMT_CHAR(S)> buffer
Definition: printf.h:757
std::string spdlog::details::os::filename_to_str ( const filename_t filename)
inline

Definition at line 370 of file os.h.

371 {
372  return filename;
373 }
string filename
Definition: train.py:213
size_t spdlog::details::os::filesize ( FILE *  f)
inline

Definition at line 204 of file os.h.

205 {
206  if (f == nullptr)
207  {
208  throw spdlog_ex("Failed getting file size. fd is null");
209  }
210 #if defined(_WIN32) && !defined(__CYGWIN__)
211  int fd = _fileno(f);
212 #if _WIN64 // 64 bits
213  __int64 ret = _filelengthi64(fd);
214  if (ret >= 0)
215  {
216  return static_cast<size_t>(ret);
217  }
218 
219 #else // windows 32 bits
220  long ret = _filelength(fd);
221  if (ret >= 0)
222  {
223  return static_cast<size_t>(ret);
224  }
225 #endif
226 
227 #else // unix
228  int fd = fileno(f);
229 // 64 bits(but not in osx or cygwin, where fstat64 is deprecated)
230 #if !defined(__FreeBSD__) && !defined(__APPLE__) && (defined(__x86_64__) || defined(__ppc64__)) && !defined(__CYGWIN__)
231  struct stat64 st;
232  if (fstat64(fd, &st) == 0)
233  {
234  return static_cast<size_t>(st.st_size);
235  }
236 #else // unix 32 bits or cygwin
237  struct stat st;
238 
239  if (fstat(fd, &st) == 0)
240  {
241  return static_cast<size_t>(st.st_size);
242  }
243 #endif
244 #endif
245  throw spdlog_ex("Failed getting file size from fd", errno);
246 }
int errno
Contains the last error code.
Definition: structcmd.h:53
bool spdlog::details::os::fopen_s ( FILE **  fp,
const filename_t filename,
const filename_t mode 
)
inline

Definition at line 148 of file os.h.

149 {
150 #ifdef _WIN32
151 #ifdef SPDLOG_WCHAR_FILENAMES
152  *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
153 #else
154  *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
155 #endif
156 #else // unix
157  *fp = fopen((filename.c_str()), mode.c_str());
158 #endif
159 
160 #ifdef SPDLOG_PREVENT_CHILD_FD
161  if (*fp != nullptr)
162  {
163  prevent_child_fd(*fp);
164  }
165 #endif
166  return *fp == nullptr;
167 }
string filename
Definition: train.py:213
void prevent_child_fd(FILE *f)
Definition: os.h:129
std::tm spdlog::details::os::gmtime ( const std::time_t &  time_tt)
inline

Definition at line 92 of file os.h.

93 {
94 
95 #ifdef _WIN32
96  std::tm tm;
97  gmtime_s(&tm, &time_tt);
98 #else
99  std::tm tm;
100  gmtime_r(&time_tt, &tm);
101 #endif
102  return tm;
103 }
null gmtime_s(...)
Definition: time.h:25
null gmtime_r(...)
Definition: time.h:24
std::tm spdlog::details::os::gmtime ( )
inline

Definition at line 105 of file os.h.

106 {
107  std::time_t now_t = time(nullptr);
108  return gmtime(now_t);
109 }
std::tm gmtime(std::time_t time)
Definition: time.h:67
bool spdlog::details::os::in_terminal ( FILE *  file)
inline

Definition at line 410 of file os.h.

411 {
412 
413 #ifdef _WIN32
414  return _isatty(_fileno(file)) != 0;
415 #else
416  return isatty(fileno(file)) != 0;
417 #endif
418 }
Definition: posix.h:188
bool spdlog::details::os::is_color_terminal ( )
inline

Definition at line 388 of file os.h.

389 {
390 #ifdef _WIN32
391  return true;
392 #else
393  static constexpr const char *Terms[] = {
394  "ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys", "putty", "rxvt", "screen", "vt100", "xterm"};
395 
396  const char *env_p = std::getenv("TERM");
397  if (env_p == nullptr)
398  {
399  return false;
400  }
401 
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; });
404  return result;
405 #endif
406 }
static QCString result
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
std::string getenv(std::string const &name)
Definition: getenv.cc:15
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
std::tm spdlog::details::os::localtime ( const std::time_t &  time_tt)
inline

Definition at line 73 of file os.h.

74 {
75 
76 #ifdef _WIN32
77  std::tm tm;
78  localtime_s(&tm, &time_tt);
79 #else
80  std::tm tm;
81  localtime_r(&time_tt, &tm);
82 #endif
83  return tm;
84 }
null localtime_s(...)
Definition: time.h:23
std::tm spdlog::details::os::localtime ( )
inline

Definition at line 86 of file os.h.

87 {
88  std::time_t now_t = time(nullptr);
89  return localtime(now_t);
90 }
std::tm localtime(std::time_t time)
Definition: time.h:29
spdlog::log_clock::time_point spdlog::details::os::now ( )
inline

Definition at line 60 of file os.h.

61 {
62 
63 #if defined __linux__ && defined SPDLOG_CLOCK_COARSE
64  timespec ts;
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)));
68 
69 #else
70  return log_clock::now();
71 #endif
72 }
second seconds
Alias for common language habits.
Definition: spacetime.h:83
spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT
Definition: os.h:60
nanosecond nanoseconds
Alias for common language habits.
Definition: spacetime.h:134
int spdlog::details::os::pid ( )
inline

Definition at line 376 of file os.h.

377 {
378 
379 #ifdef _WIN32
380  return static_cast<int>(::GetCurrentProcessId());
381 #else
382  return static_cast<int>(::getpid());
383 #endif
384 }
void spdlog::details::os::prevent_child_fd ( FILE *  f)
inline

Definition at line 129 of file os.h.

130 {
131 
132 #ifdef _WIN32
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);
137 #endif
138 #else
139  auto fd = fileno(f);
140  if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
141  {
142  throw spdlog_ex("fcntl with FD_CLOEXEC failed", errno);
143  }
144 #endif
145 }
int errno
Contains the last error code.
Definition: structcmd.h:53
int spdlog::details::os::remove ( const filename_t filename)
inline

Definition at line 169 of file os.h.

170 {
171 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
172  return _wremove(filename.c_str());
173 #else
174  return std::remove(filename.c_str());
175 #endif
176 }
string filename
Definition: train.py:213
int remove(const filename_t &filename) SPDLOG_NOEXCEPT
Definition: os.h:169
int spdlog::details::os::rename ( const filename_t filename1,
const filename_t filename2 
)
inline

Definition at line 178 of file os.h.

179 {
180 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
181  return _wrename(filename1.c_str(), filename2.c_str());
182 #else
183  return std::rename(filename1.c_str(), filename2.c_str());
184 #endif
185 }
int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
Definition: os.h:178
void spdlog::details::os::sleep_for_millis ( int  milliseconds)
inline

Definition at line 351 of file os.h.

352 {
353 #if defined(_WIN32)
354  ::Sleep(milliseconds);
355 #else
356  std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
357 #endif
358 }
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:100
size_t spdlog::details::os::thread_id ( )
inline

Definition at line 339 of file os.h.

340 {
341 #if defined(SPDLOG_NO_TLS)
342  return _thread_id();
343 #else // cache thread id in tls
344  static thread_local const size_t tid = _thread_id();
345  return tid;
346 #endif
347 }
size_t _thread_id() SPDLOG_NOEXCEPT
Definition: os.h:316
int spdlog::details::os::utc_minutes_offset ( const std::tm &  tm = details::os::localtime())
inline

Definition at line 249 of file os.h.

250 {
251 
252 #ifdef _WIN32
253 #if _WIN32_WINNT < _WIN32_WINNT_WS08
254  TIME_ZONE_INFORMATION tzinfo;
255  auto rv = GetTimeZoneInformation(&tzinfo);
256 #else
257  DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
258  auto rv = GetDynamicTimeZoneInformation(&tzinfo);
259 #endif
260  if (rv == TIME_ZONE_ID_INVALID)
261  throw spdlog::spdlog_ex("Failed getting timezone info. ", errno);
262 
263  int offset = -tzinfo.Bias;
264  if (tm.tm_isdst)
265  {
266  offset -= tzinfo.DaylightBias;
267  }
268  else
269  {
270  offset -= tzinfo.StandardBias;
271  }
272  return offset;
273 #else
274 
275 #if defined(sun) || defined(__sun) || defined(_AIX)
276  // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
277  struct helper
278  {
279  static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(), const std::tm &gmtm = details::os::gmtime())
280  {
281  int local_year = localtm.tm_year + (1900 - 1);
282  int gmt_year = gmtm.tm_year + (1900 - 1);
283 
284  long int days = (
285  // difference in day of year
286  localtm.tm_yday -
287  gmtm.tm_yday
288 
289  // + intervening leap days
290  + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) +
291  ((local_year / 100 >> 2) - (gmt_year / 100 >> 2))
292 
293  // + difference in years * 365 */
294  + (long int)(local_year - gmt_year) * 365);
295 
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);
299 
300  return secs;
301  }
302  };
303 
304  auto offset_seconds = helper::calculate_gmt_offset(tm);
305 #else
306  auto offset_seconds = tm.tm_gmtoff;
307 #endif
308 
309  return static_cast<int>(offset_seconds / 60);
310 #endif
311 }
int errno
Contains the last error code.
Definition: structcmd.h:53
std::tm localtime(std::time_t time)
Definition: time.h:29
static const char * days[]
std::tm gmtime(std::time_t time)
Definition: time.h:67

Variable Documentation

SPDLOG_CONSTEXPR const char* spdlog::details::os::default_eol = SPDLOG_EOL
static

Definition at line 120 of file os.h.

SPDLOG_CONSTEXPR const char spdlog::details::os::folder_sep = '/'
static

Definition at line 126 of file os.h.