option to prevent child processes from inheriting log file desciptors (#define SPDLOG_PREVENT_CHILD_FD)
This commit is contained in:
parent
af35f9c086
commit
92db8115b7
@ -11,10 +11,11 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
@ -26,34 +27,32 @@
|
|||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#endif
|
#endif
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <process.h>
|
#include <process.h> // _get_pid support
|
||||||
|
#include <io.h> // _get_osfhandle support
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
#include <share.h>
|
#include <share.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <sys/types.h>
|
#else // unix
|
||||||
#include <io.h>
|
|
||||||
|
|
||||||
#elif __linux__
|
|
||||||
|
|
||||||
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
||||||
|
|
||||||
#elif __FreeBSD__
|
#elif __FreeBSD__
|
||||||
#include <sys/thr.h> //Use thr_self() syscall under FreeBSD to get thread id
|
#include <sys/thr.h> //Use thr_self() syscall under FreeBSD to get thread id
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#else
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif //unix
|
||||||
|
|
||||||
#ifndef __has_feature // Clang - feature checking macros.
|
#ifndef __has_feature // Clang - feature checking macros.
|
||||||
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
namespace details
|
namespace details
|
||||||
@ -143,6 +142,18 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
|
|||||||
SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL;
|
SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL;
|
||||||
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
|
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
|
||||||
|
|
||||||
|
inline void prevent_child_fd(FILE *f)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
|
||||||
|
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
|
||||||
|
throw spdlog_ex("SetHandleInformation failed", errno);
|
||||||
|
#else
|
||||||
|
auto fd = fileno(f);
|
||||||
|
if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
||||||
|
throw spdlog_ex("fcntl with FD_CLOEXEC failed", errno);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//fopen_s on non windows for writing
|
//fopen_s on non windows for writing
|
||||||
@ -154,13 +165,18 @@ inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode
|
|||||||
#else
|
#else
|
||||||
*fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
|
*fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR);
|
||||||
#endif
|
#endif
|
||||||
return *fp == nullptr;
|
#else //unix
|
||||||
#else
|
|
||||||
*fp = fopen((filename.c_str()), mode.c_str());
|
*fp = fopen((filename.c_str()), mode.c_str());
|
||||||
return *fp == nullptr;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef SPDLOG_PREVENT_CHILD_FD
|
||||||
|
if(*fp != nullptr)
|
||||||
|
prevent_child_fd(*fp);
|
||||||
|
#endif
|
||||||
|
return *fp == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline int remove(const filename_t &filename)
|
inline int remove(const filename_t &filename)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
|
@ -100,4 +100,8 @@
|
|||||||
// #define SPDLOG_ENABLE_SYSLOG
|
// #define SPDLOG_ENABLE_SYSLOG
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Uncomment to prevent child processes from inheriting log file desciptors
|
||||||
|
//
|
||||||
|
// #define SPDLOG_PREVENT_CHILD_FD
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user