1
0
Fork 0

cache pid for logging

This commit is contained in:
jsteemann 2018-09-27 17:38:41 +02:00
parent f4b6d3ef70
commit 805a1c5d51
3 changed files with 20 additions and 2 deletions

View File

@ -240,8 +240,9 @@ int DaemonFeature::forkProcess() {
TRI_ASSERT(pid == 0); // we are in the child
// child
// child
LogAppender::allowStdLogging(false);
Logger::clearCachedPid();
// change the file mode mask
umask(0);

View File

@ -67,6 +67,7 @@ bool Logger::_keepLogRotate(false);
bool Logger::_useMicrotime(false);
bool Logger::_showRole(false);
char Logger::_role('\0');
TRI_pid_t Logger::_cachedPid(0);
std::string Logger::_outputPrefix("");
std::unique_ptr<LogThread> Logger::_loggingThread(nullptr);
@ -329,7 +330,17 @@ void Logger::log(char const* function, char const* file, int line,
}
// append the process / thread identifier
out << '[' << Thread::currentProcessId();
// we only determine our pid once, as currentProcessId() will
// likely do a syscall.
// this read-check-update sequence is not thread-safe, but this
// should not matter, as the pid value is only changed from 0 to the
// actual pid and never changes afterwards
if (_cachedPid == 0) {
_cachedPid = Thread::currentProcessId();
}
TRI_ASSERT(_cachedPid != 0);
out << '[' << _cachedPid;
if (_showThreadIdentifier) {
out << '-' << Thread::currentThreadNumber();
@ -444,6 +455,8 @@ void Logger::shutdown() {
// cleanup appenders
LogAppender::shutdown();
_cachedPid = 0;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -234,6 +234,9 @@ class Logger {
static void setUseMicrotime(bool);
static bool getUseMicrotime() {return _useMicrotime;};
static void setKeepLogrotate(bool);
// can be called after fork()
static void clearCachedPid() { _cachedPid = 0; }
static std::string const& translateLogLevel(LogLevel);
@ -275,6 +278,7 @@ class Logger {
static bool _keepLogRotate;
static bool _useMicrotime;
static char _role; // current server role to log
static TRI_pid_t _cachedPid;
static std::string _outputPrefix;
static std::unique_ptr<LogThread> _loggingThread;