Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger: Use a function-level static var for the logger object #6689

Merged
merged 2 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions pdns/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,22 @@ extern StatBag S;
#include "lock.hh"
#include "namespaces.hh"

pthread_once_t Logger::s_once;
pthread_key_t Logger::g_loggerKey;
thread_local Logger::PerThread Logger::t_perThread;

Logger g_log("", LOG_DAEMON);
Logger& getLogger()
{
/* Since the Logger can be called very early, we need to make sure
that the relevant parts are initialized no matter what, which is tricky
because we can't easily control the initialization order, especially with
built-in backends.
t_perThread is thread_local, so it will be initialized when first accessed,
but we need to make sure that the object itself is initialized, and making
it a function-level static variable achieves that, because it will be
initialized the first time we enter this function at the very last.
*/
static Logger log("", LOG_DAEMON);
return log;
}

void Logger::log(const string &msg, Urgency u)
{
Expand Down Expand Up @@ -129,52 +141,29 @@ void Logger::setName(const string &_name)
open();
}

void Logger::initKey()
{
if(pthread_key_create(&g_loggerKey, perThreadDestructor))
unixDie("Creating thread key for logger");
}

Logger::Logger(const string &n, int facility) :
name(n), flags(LOG_PID|LOG_NDELAY), d_facility(facility), d_loglevel(Logger::None),
consoleUrgency(Error), opened(false), d_disableSyslog(false)
{
if(pthread_once(&s_once, initKey))
unixDie("Creating thread key for logger");

open();

}

Logger& Logger::operator<<(Urgency u)
{
getPerThread()->d_urgency=u;
getPerThread().d_urgency=u;
return *this;
}

void Logger::perThreadDestructor(void* buf)
Logger::PerThread& Logger::getPerThread()
{
PerThread* pt = (PerThread*) buf;
delete pt;
}

Logger::PerThread* Logger::getPerThread()
{
void *buf=pthread_getspecific(g_loggerKey);
PerThread* ret;
if(buf)
ret = (PerThread*) buf;
else {
ret = new PerThread();
pthread_setspecific(g_loggerKey, (void*)ret);
}
return ret;
return t_perThread;
}

Logger& Logger::operator<<(const string &s)
{
PerThread* pt =getPerThread();
pt->d_output.append(s);
PerThread& pt = getPerThread();
pt.d_output.append(s);
return *this;
}

Expand Down Expand Up @@ -244,11 +233,11 @@ Logger& Logger::operator<<(long i)

Logger& Logger::operator<<(ostream & (&)(ostream &))
{
PerThread* pt =getPerThread();
PerThread& pt = getPerThread();

log(pt->d_output, pt->d_urgency);
pt->d_output.clear();
pt->d_urgency=Info;
log(pt.d_output, pt.d_urgency);
pt.d_output.clear();
pt.d_urgency=Info;
return *this;
}

Expand Down
12 changes: 5 additions & 7 deletions pdns/logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <iostream>
#include <sstream>
#include <syslog.h>
#include <pthread.h>

#include "namespaces.hh"
#include "dnsname.hh"
Expand Down Expand Up @@ -101,11 +100,10 @@ private:
string d_output;
Urgency d_urgency;
};
static void initKey();
static void perThreadDestructor(void *);
PerThread* getPerThread();
PerThread& getPerThread();
void open();

static thread_local PerThread t_perThread;
string name;
int flags;
int d_facility;
Expand All @@ -115,11 +113,11 @@ private:
bool d_disableSyslog;
bool d_timestamps{true};
bool d_prefixed{false};
static pthread_once_t s_once;
static pthread_key_t g_loggerKey;
};

extern Logger g_log;
Logger& getLogger();

#define g_log getLogger()

#ifdef VERBOSELOG
#define DLOG(x) x
Expand Down