1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-03-14 01:59:53 +08:00

exclude from compilation prevent_child_fd() if SPDLOG_PREVENT_CHILD_FD not defined

This commit is contained in:
gabime 2019-11-04 16:42:58 +02:00
parent d67efb2cab
commit 57085c892f
2 changed files with 5 additions and 3 deletions

View File

@ -126,15 +126,13 @@ SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT
return gmtime(now_t); return gmtime(now_t);
} }
#ifdef SPDLOG_PREVENT_CHILD_FD
SPDLOG_INLINE void prevent_child_fd(FILE *f) SPDLOG_INLINE void prevent_child_fd(FILE *f)
{ {
#ifdef _WIN32 #ifdef _WIN32
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(::_fileno(f))); auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(::_fileno(f)));
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno)); SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno));
#endif
#else #else
auto fd = ::fileno(f); auto fd = ::fileno(f);
if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
@ -143,6 +141,7 @@ SPDLOG_INLINE void prevent_child_fd(FILE *f)
} }
#endif #endif
} }
#endif //SPDLOG_PREVENT_CHILD_FD
// fopen_s on non windows for writing // fopen_s on non windows for writing
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
@ -158,6 +157,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
#endif #endif
#ifdef SPDLOG_PREVENT_CHILD_FD #ifdef SPDLOG_PREVENT_CHILD_FD
// prevent child processes from inheriting log file descriptors
if (*fp != nullptr) if (*fp != nullptr)
{ {
prevent_child_fd(*fp); prevent_child_fd(*fp);

View File

@ -38,7 +38,9 @@ static const char folder_sep = '\\';
SPDLOG_CONSTEXPR static const char folder_sep = '/'; SPDLOG_CONSTEXPR static const char folder_sep = '/';
#endif #endif
#ifdef SPDLOG_PREVENT_CHILD_FD
void prevent_child_fd(FILE *f); void prevent_child_fd(FILE *f);
#endif
// fopen_s on non windows for writing // fopen_s on non windows for writing
bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode); bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);