2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-04 16:48:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_log.h"
|
|
|
|
|
2012-08-01 18:20:18 +02:00
|
|
|
#include <syslog.h>
|
2014-07-05 11:22:40 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <inttypes.h>
|
2012-08-01 18:20:18 +02:00
|
|
|
|
2014-08-05 17:23:46 +02:00
|
|
|
#include <cerrno>
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <cstdio>
|
2012-06-06 16:58:19 +02:00
|
|
|
#include <cstring>
|
2014-07-05 11:22:40 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
2014-11-19 17:53:30 +01:00
|
|
|
#include <iomanip>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-08-01 18:20:18 +02:00
|
|
|
#include "shrpx_config.h"
|
2014-07-05 11:22:40 +02:00
|
|
|
#include "shrpx_downstream.h"
|
|
|
|
#include "shrpx_worker_config.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
2012-08-01 18:20:18 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
2012-12-09 13:02:48 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
const char *SEVERITY_STR[] = {"INFO", "NOTICE", "WARN", "ERROR", "FATAL"};
|
2012-12-09 13:02:48 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const char *SEVERITY_COLOR[] = {
|
2014-11-27 15:39:04 +01:00
|
|
|
"\033[1;32m", // INFO
|
|
|
|
"\033[1;36m", // NOTICE
|
|
|
|
"\033[1;33m", // WARN
|
|
|
|
"\033[1;31m", // ERROR
|
|
|
|
"\033[1;35m", // FATAL
|
2012-12-09 13:02:48 +01:00
|
|
|
};
|
|
|
|
} // namespace
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-06 15:32:56 +01:00
|
|
|
int Log::severity_thres_ = NOTICE;
|
2012-06-05 19:23:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Log::set_severity_level(int severity) { severity_thres_ = severity; }
|
2012-06-05 19:23:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Log::set_severity_level_by_name(const char *name) {
|
|
|
|
for (size_t i = 0, max = util::array_size(SEVERITY_STR); i < max; ++i) {
|
|
|
|
if (strcmp(SEVERITY_STR[i], name) == 0) {
|
2012-06-06 16:58:19 +02:00
|
|
|
severity_thres_ = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int severity_to_syslog_level(int severity) {
|
|
|
|
switch (severity) {
|
|
|
|
case (INFO):
|
2012-08-01 18:20:18 +02:00
|
|
|
return LOG_INFO;
|
2014-11-27 15:39:04 +01:00
|
|
|
case (NOTICE):
|
2014-11-06 15:32:56 +01:00
|
|
|
return LOG_NOTICE;
|
2014-11-27 15:39:04 +01:00
|
|
|
case (WARN):
|
2012-08-01 18:20:18 +02:00
|
|
|
return LOG_WARNING;
|
2014-11-27 15:39:04 +01:00
|
|
|
case (ERROR):
|
2012-08-01 18:20:18 +02:00
|
|
|
return LOG_ERR;
|
2014-11-27 15:39:04 +01:00
|
|
|
case (FATAL):
|
2012-08-01 18:20:18 +02:00
|
|
|
return LOG_CRIT;
|
|
|
|
default:
|
2012-08-01 19:07:51 +02:00
|
|
|
return -1;
|
2012-08-01 18:20:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
Log::Log(int severity, const char *filename, int linenum)
|
2014-11-27 15:39:04 +01:00
|
|
|
: filename_(filename), severity_(severity), linenum_(linenum) {}
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Log::~Log() {
|
2014-07-05 11:22:40 +02:00
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()) {
|
2014-11-18 16:56:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-19 14:33:54 +02:00
|
|
|
auto wconf = worker_config;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!log_enabled(severity_) ||
|
|
|
|
(wconf->errorlog_fd == -1 && !get_config()->errorlog_syslog)) {
|
2014-07-05 11:22:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->errorlog_syslog) {
|
2014-11-06 15:32:56 +01:00
|
|
|
if (severity_ == NOTICE) {
|
|
|
|
syslog(severity_to_syslog_level(severity_), "[%s] %s",
|
|
|
|
SEVERITY_STR[severity_], stream_.str().c_str());
|
|
|
|
} else {
|
|
|
|
syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)",
|
2014-11-27 15:39:04 +01:00
|
|
|
SEVERITY_STR[severity_], stream_.str().c_str(), filename_,
|
|
|
|
linenum_);
|
2014-11-06 15:32:56 +01:00
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[4096];
|
2014-08-19 14:33:54 +02:00
|
|
|
auto tty = wconf->errorlog_tty;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-07-06 06:27:51 +02:00
|
|
|
auto cached_time = get_config()->cached_time;
|
|
|
|
|
2014-11-06 15:32:56 +01:00
|
|
|
if (severity_ == NOTICE) {
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s\n",
|
|
|
|
cached_time->c_str(), get_config()->pid,
|
|
|
|
tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
|
|
|
|
tty ? "\033[0m" : "", stream_.str().c_str());
|
2014-11-06 15:32:56 +01:00
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s%s:%d%s %s\n",
|
|
|
|
cached_time->c_str(), get_config()->pid,
|
|
|
|
tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
|
|
|
|
tty ? "\033[0m" : "", tty ? "\033[1;30m" : "", filename_,
|
|
|
|
linenum_, tty ? "\033[0m" : "", stream_.str().c_str());
|
2014-11-06 15:32:56 +01:00
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2014-07-05 11:22:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nwrite = std::min(static_cast<size_t>(rv), sizeof(buf) - 1);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
while (write(wconf->errorlog_fd, buf, nwrite) == -1 && errno == EINTR)
|
|
|
|
;
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
template <typename OutputIterator>
|
|
|
|
std::pair<OutputIterator, size_t> copy(const char *src, size_t avail,
|
|
|
|
OutputIterator oitr) {
|
2014-11-18 16:56:44 +01:00
|
|
|
auto nwrite = std::min(strlen(src), avail);
|
|
|
|
auto noitr = std::copy_n(src, nwrite, oitr);
|
|
|
|
return std::make_pair(noitr, avail - nwrite);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
|
2014-08-19 14:33:54 +02:00
|
|
|
auto wconf = worker_config;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (wconf->accesslog_fd == -1 && !get_config()->accesslog_syslog) {
|
2014-07-05 11:22:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
char buf[4096];
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
auto downstream = lgsp->downstream;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
auto p = buf;
|
|
|
|
auto avail = sizeof(buf) - 2;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &lf : lfv) {
|
|
|
|
switch (lf.type) {
|
2014-11-18 16:56:44 +01:00
|
|
|
case SHRPX_LOGF_LITERAL:
|
|
|
|
std::tie(p, avail) = copy(lf.value.get(), avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_REMOTE_ADDR:
|
|
|
|
std::tie(p, avail) = copy(lgsp->remote_addr, avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_TIME_LOCAL:
|
2014-11-27 15:39:04 +01:00
|
|
|
std::tie(p, avail) =
|
|
|
|
copy(util::format_common_log(lgsp->time_now).c_str(), avail, p);
|
2014-11-19 17:53:30 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_TIME_ISO8601:
|
2014-11-27 15:39:04 +01:00
|
|
|
std::tie(p, avail) =
|
|
|
|
copy(util::format_iso8601(lgsp->time_now).c_str(), avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_REQUEST:
|
|
|
|
std::tie(p, avail) = copy(lgsp->method, avail, p);
|
|
|
|
std::tie(p, avail) = copy(" ", avail, p);
|
|
|
|
std::tie(p, avail) = copy(lgsp->path, avail, p);
|
|
|
|
std::tie(p, avail) = copy(" HTTP/", avail, p);
|
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp->major).c_str(), avail, p);
|
|
|
|
std::tie(p, avail) = copy(".", avail, p);
|
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp->minor).c_str(), avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_STATUS:
|
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp->status).c_str(), avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_BODY_BYTES_SENT:
|
2014-11-27 15:39:04 +01:00
|
|
|
std::tie(p, avail) =
|
|
|
|
copy(util::utos(lgsp->body_bytes_sent).c_str(), avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_HTTP:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (downstream) {
|
2014-11-18 16:56:44 +01:00
|
|
|
auto hd = downstream->get_request_header(lf.value.get());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (hd != std::end(downstream->get_request_headers())) {
|
2014-11-18 16:56:44 +01:00
|
|
|
std::tie(p, avail) = copy((*hd).value.c_str(), avail, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tie(p, avail) = copy("-", avail, p);
|
|
|
|
|
2014-11-19 17:53:30 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_REMOTE_PORT:
|
|
|
|
std::tie(p, avail) = copy(lgsp->remote_port, avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_SERVER_PORT:
|
2014-11-27 15:39:04 +01:00
|
|
|
std::tie(p, avail) =
|
|
|
|
copy(util::utos(lgsp->server_port).c_str(), avail, p);
|
2014-11-19 17:53:30 +01:00
|
|
|
break;
|
2014-11-27 15:39:04 +01:00
|
|
|
case SHRPX_LOGF_REQUEST_TIME: {
|
|
|
|
auto t = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
lgsp->time_now - lgsp->request_start_time).count();
|
|
|
|
|
|
|
|
auto frac = util::utos(t % 1000);
|
|
|
|
auto sec = util::utos(t / 1000);
|
|
|
|
if (frac.size() < 3) {
|
|
|
|
frac = std::string(3 - frac.size(), '0') + frac;
|
2014-11-19 17:53:30 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
sec += ".";
|
|
|
|
sec += frac;
|
|
|
|
|
|
|
|
std::tie(p, avail) = copy(sec.c_str(), avail, p);
|
|
|
|
} break;
|
2014-11-24 06:34:43 +01:00
|
|
|
case SHRPX_LOGF_PID:
|
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp->pid).c_str(), avail, p);
|
|
|
|
break;
|
2014-11-24 07:22:10 +01:00
|
|
|
case SHRPX_LOGF_ALPN:
|
|
|
|
std::tie(p, avail) = copy(lgsp->alpn, avail, p);
|
|
|
|
break;
|
2014-11-19 17:53:30 +01:00
|
|
|
case SHRPX_LOGF_NONE:
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-06-05 19:23:07 +02:00
|
|
|
}
|
2014-06-12 14:46:25 +02:00
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
*p = '\0';
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (get_config()->accesslog_syslog) {
|
2014-07-05 11:22:40 +02:00
|
|
|
syslog(LOG_INFO, "%s", buf);
|
2014-06-12 14:46:25 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
*p++ = '\n';
|
|
|
|
|
|
|
|
auto nwrite = p - buf;
|
2014-11-27 15:39:04 +01:00
|
|
|
while (write(wconf->accesslog_fd, buf, nwrite) == -1 && errno == EINTR)
|
|
|
|
;
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int reopen_log_files() {
|
2014-07-05 11:22:40 +02:00
|
|
|
int res = 0;
|
|
|
|
|
2014-08-19 14:33:54 +02:00
|
|
|
auto wconf = worker_config;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (wconf->accesslog_fd != -1) {
|
2014-08-19 14:33:54 +02:00
|
|
|
close(wconf->accesslog_fd);
|
|
|
|
wconf->accesslog_fd = -1;
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->accesslog_syslog && get_config()->accesslog_file) {
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-08-19 14:33:54 +02:00
|
|
|
wconf->accesslog_fd =
|
2014-11-27 15:39:04 +01:00
|
|
|
util::reopen_log_file(get_config()->accesslog_file.get());
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (wconf->accesslog_fd == -1) {
|
2014-07-05 11:22:40 +02:00
|
|
|
LOG(ERROR) << "Failed to open accesslog file "
|
|
|
|
<< get_config()->accesslog_file.get();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int new_errorlog_fd = -1;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!get_config()->errorlog_syslog && get_config()->errorlog_file) {
|
2014-07-05 11:22:40 +02:00
|
|
|
|
|
|
|
new_errorlog_fd = util::reopen_log_file(get_config()->errorlog_file.get());
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (new_errorlog_fd == -1) {
|
|
|
|
if (wconf->errorlog_fd != -1) {
|
2014-07-05 11:22:40 +02:00
|
|
|
LOG(ERROR) << "Failed to open errorlog file "
|
|
|
|
<< get_config()->errorlog_file.get();
|
|
|
|
} else {
|
|
|
|
std::cerr << "Failed to open errorlog file "
|
2014-11-27 15:39:04 +01:00
|
|
|
<< get_config()->errorlog_file.get() << std::endl;
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (wconf->errorlog_fd != -1) {
|
2014-08-19 14:33:54 +02:00
|
|
|
close(wconf->errorlog_fd);
|
|
|
|
wconf->errorlog_fd = -1;
|
|
|
|
wconf->errorlog_tty = false;
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (new_errorlog_fd != -1) {
|
2014-08-19 14:33:54 +02:00
|
|
|
wconf->errorlog_fd = new_errorlog_fd;
|
|
|
|
wconf->errorlog_tty = isatty(wconf->errorlog_fd);
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|