replacing thread_local, which does not exist on OS X, with pthread_getspecific call

This commit is contained in:
Stefan Eissing 2015-03-03 17:09:15 +01:00
parent a2a9f15307
commit 1fd44b1567
7 changed files with 33 additions and 18 deletions

View File

@ -1884,15 +1884,15 @@ int main(int argc, char **argv) {
} }
if (get_config()->uid != 0) { if (get_config()->uid != 0) {
if (log_config->accesslog_fd != -1 && if (log_config()->accesslog_fd != -1 &&
fchown(log_config->accesslog_fd, get_config()->uid, fchown(log_config()->accesslog_fd, get_config()->uid,
get_config()->gid) == -1) { get_config()->gid) == -1) {
auto error = errno; auto error = errno;
LOG(WARN) << "Changing owner of access log file failed: " LOG(WARN) << "Changing owner of access log file failed: "
<< strerror(error); << strerror(error);
} }
if (log_config->errorlog_fd != -1 && if (log_config()->errorlog_fd != -1 &&
fchown(log_config->errorlog_fd, get_config()->uid, get_config()->gid) == fchown(log_config()->errorlog_fd, get_config()->uid, get_config()->gid) ==
-1) { -1) {
auto error = errno; auto error = errno;
LOG(WARN) << "Changing owner of error log file failed: " LOG(WARN) << "Changing owner of error log file failed: "

View File

@ -358,7 +358,7 @@ int HttpDownstreamConnection::push_request_headers() {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
const char *hdrp; const char *hdrp;
std::string nhdrs; std::string nhdrs;
if (log_config->errorlog_tty) { if (log_config()->errorlog_tty) {
nhdrs = http::colorizeHeaders(hdrs.c_str()); nhdrs = http::colorizeHeaders(hdrs.c_str());
hdrp = nhdrs.c_str(); hdrp = nhdrs.c_str();
} else { } else {

View File

@ -808,7 +808,7 @@ int HttpsUpstream::on_downstream_abort_request(Downstream *downstream,
void HttpsUpstream::log_response_headers(const std::string &hdrs) const { void HttpsUpstream::log_response_headers(const std::string &hdrs) const {
const char *hdrp; const char *hdrp;
std::string nhdrs; std::string nhdrs;
if (log_config->errorlog_tty) { if (log_config()->errorlog_tty) {
nhdrs = http::colorizeHeaders(hdrs.c_str()); nhdrs = http::colorizeHeaders(hdrs.c_str());
hdrp = nhdrs.c_str(); hdrp = nhdrs.c_str();
} else { } else {

View File

@ -99,7 +99,7 @@ Log::~Log() {
return; return;
} }
auto lgconf = log_config; auto lgconf = log_config();
if (!log_enabled(severity_) || if (!log_enabled(severity_) ||
(lgconf->errorlog_fd == -1 && !get_config()->errorlog_syslog)) { (lgconf->errorlog_fd == -1 && !get_config()->errorlog_syslog)) {
@ -159,7 +159,7 @@ std::pair<OutputIterator, size_t> copy(const char *src, size_t avail,
} // namespace } // namespace
void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) { void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
auto lgconf = log_config; auto lgconf = log_config();
if (lgconf->accesslog_fd == -1 && !get_config()->accesslog_syslog) { if (lgconf->accesslog_fd == -1 && !get_config()->accesslog_syslog) {
return; return;
@ -272,7 +272,7 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
int reopen_log_files() { int reopen_log_files() {
int res = 0; int res = 0;
auto lgconf = log_config; auto lgconf = log_config();
if (lgconf->accesslog_fd != -1) { if (lgconf->accesslog_fd != -1) {
close(lgconf->accesslog_fd); close(lgconf->accesslog_fd);

View File

@ -97,8 +97,8 @@ private:
static int severity_thres_; static int severity_thres_;
}; };
#define TTY_HTTP_HD (log_config->errorlog_tty ? "\033[1;34m" : "") #define TTY_HTTP_HD (log_config()->errorlog_tty ? "\033[1;34m" : "")
#define TTY_RST (log_config->errorlog_tty ? "\033[0m" : "") #define TTY_RST (log_config()->errorlog_tty ? "\033[0m" : "")
enum LogFragmentType { enum LogFragmentType {
SHRPX_LOGF_NONE, SHRPX_LOGF_NONE,

View File

@ -33,9 +33,28 @@ LogConfig::LogConfig()
: accesslog_fd(-1), errorlog_fd(-1), errorlog_tty(false) {} : accesslog_fd(-1), errorlog_fd(-1), errorlog_tty(false) {}
#ifndef NOTHREADS #ifndef NOTHREADS
thread_local static pthread_key_t lckey;
static pthread_once_t lckey_once = PTHREAD_ONCE_INIT;
static void make_key(void) {
pthread_key_create(&lckey, NULL);
}
LogConfig *log_config(void) {
pthread_once(&lckey_once, make_key);
LogConfig *config = (LogConfig *)pthread_getspecific(lckey);
if (!config) {
config = new LogConfig();
pthread_setspecific(lckey, config);
}
return config;
}
#else
static LogConfig *config = new LogConfig();
LogConfig *log_config(void) {
return config;
}
#endif // NOTHREADS #endif // NOTHREADS
LogConfig *log_config = new LogConfig();
void void
LogConfig::update_tstamp(const std::chrono::system_clock::time_point &now) { LogConfig::update_tstamp(const std::chrono::system_clock::time_point &now) {

View File

@ -46,11 +46,7 @@ struct LogConfig {
// We need LogConfig per thread to avoid data race around opening file // We need LogConfig per thread to avoid data race around opening file
// descriptor for log files. // descriptor for log files.
extern extern LogConfig *log_config(void);
#ifndef NOTHREADS
thread_local
#endif // NOTHREADS
LogConfig *log_config;
} // namespace shrpx } // namespace shrpx