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"
|
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2012-08-01 18:20:18 +02:00
|
|
|
#include <syslog.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_SYSLOG_H
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2014-07-05 11:22:40 +02:00
|
|
|
#include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
|
|
|
#ifdef HAVE_INTTYPES_H
|
2014-07-05 11:22:40 +02:00
|
|
|
#include <inttypes.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_INTTYPES_H
|
2012-08-01 18:20:18 +02:00
|
|
|
|
2015-10-24 12:31:09 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
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"
|
2016-08-04 17:04:47 +02:00
|
|
|
#include "shrpx_worker.h"
|
2014-07-05 11:22:40 +02:00
|
|
|
#include "util.h"
|
2015-02-06 14:44:09 +01:00
|
|
|
#include "template.h"
|
2014-07-05 11:22:40 +02:00
|
|
|
|
|
|
|
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 {
|
2016-03-24 13:05:02 +01:00
|
|
|
const StringRef SEVERITY_STR[] = {
|
|
|
|
StringRef::from_lit("INFO"), StringRef::from_lit("NOTICE"),
|
|
|
|
StringRef::from_lit("WARN"), StringRef::from_lit("ERROR"),
|
|
|
|
StringRef::from_lit("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
|
|
|
|
2016-03-24 13:05:02 +01:00
|
|
|
int Log::set_severity_level_by_name(const StringRef &name) {
|
2015-02-06 14:44:09 +01:00
|
|
|
for (size_t i = 0, max = array_size(SEVERITY_STR); i < max; ++i) {
|
2016-03-24 13:05:02 +01:00
|
|
|
if (name == SEVERITY_STR[i]) {
|
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;
|
2016-10-08 04:34:23 +02:00
|
|
|
auto config = get_config();
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2016-10-08 04:34:23 +02:00
|
|
|
if (!config) {
|
2014-11-18 16:56:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-03 17:09:15 +01:00
|
|
|
auto lgconf = log_config();
|
2014-08-19 14:33:54 +02:00
|
|
|
|
2016-10-08 04:34:23 +02:00
|
|
|
auto &errorconf = config->logging.error;
|
2016-01-18 09:26:27 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!log_enabled(severity_) ||
|
2016-01-18 09:26:27 +01:00
|
|
|
(lgconf->errorlog_fd == -1 && !errorconf.syslog)) {
|
2014-07-05 11:22:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-18 09:26:27 +01:00
|
|
|
if (errorconf.syslog) {
|
2014-11-06 15:32:56 +01:00
|
|
|
if (severity_ == NOTICE) {
|
|
|
|
syslog(severity_to_syslog_level(severity_), "[%s] %s",
|
2016-03-24 13:05:02 +01:00
|
|
|
SEVERITY_STR[severity_].c_str(), stream_.str().c_str());
|
2014-11-06 15:32:56 +01:00
|
|
|
} else {
|
|
|
|
syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)",
|
2016-03-24 13:05:02 +01:00
|
|
|
SEVERITY_STR[severity_].c_str(), stream_.str().c_str(), filename_,
|
2014-11-27 15:39:04 +01:00
|
|
|
linenum_);
|
2014-11-06 15:32:56 +01:00
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
char buf[4_k];
|
2015-02-25 16:02:29 +01:00
|
|
|
auto tty = lgconf->errorlog_tty;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-02-25 16:02:29 +01:00
|
|
|
lgconf->update_tstamp(std::chrono::system_clock::now());
|
2016-12-18 14:56:24 +01:00
|
|
|
|
|
|
|
// Error log format: <datetime> <master-pid> <current-pid>
|
|
|
|
// <thread-id> <level> (<filename>:<line>) <msg>
|
|
|
|
rv = snprintf(buf, sizeof(buf), "%s %d %d %s %s%s%s (%s:%d) %s\n",
|
|
|
|
lgconf->time_iso8601.c_str(), config->pid, lgconf->pid,
|
|
|
|
lgconf->thread_id.c_str(), tty ? SEVERITY_COLOR[severity_] : "",
|
|
|
|
SEVERITY_STR[severity_].c_str(), tty ? "\033[0m" : "",
|
|
|
|
filename_, linenum_, stream_.str().c_str());
|
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);
|
|
|
|
|
2015-02-25 16:02:29 +01:00
|
|
|
while (write(lgconf->errorlog_fd, buf, nwrite) == -1 && errno == EINTR)
|
2014-11-27 15:39:04 +01:00
|
|
|
;
|
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>
|
2015-10-28 15:12:16 +01:00
|
|
|
std::pair<OutputIterator, size_t> copy(const char *src, size_t srclen,
|
|
|
|
size_t avail, OutputIterator oitr) {
|
|
|
|
auto nwrite = std::min(srclen, avail);
|
2014-11-18 16:56:44 +01:00
|
|
|
auto noitr = std::copy_n(src, nwrite, oitr);
|
|
|
|
return std::make_pair(noitr, avail - nwrite);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-10-28 15:12:16 +01:00
|
|
|
namespace {
|
|
|
|
template <typename OutputIterator>
|
|
|
|
std::pair<OutputIterator, size_t> copy(const char *src, size_t avail,
|
|
|
|
OutputIterator oitr) {
|
|
|
|
return copy(src, strlen(src), avail, oitr);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <typename OutputIterator>
|
|
|
|
std::pair<OutputIterator, size_t> copy(const std::string &src, size_t avail,
|
|
|
|
OutputIterator oitr) {
|
|
|
|
return copy(src.c_str(), src.size(), avail, oitr);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-01-17 03:19:19 +01:00
|
|
|
namespace {
|
|
|
|
template <typename OutputIterator>
|
2016-01-17 03:33:45 +01:00
|
|
|
std::pair<OutputIterator, size_t> copy(const StringRef &src, size_t avail,
|
2016-01-17 03:19:19 +01:00
|
|
|
OutputIterator oitr) {
|
|
|
|
return copy(src.c_str(), src.size(), avail, oitr);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-01-21 09:12:40 +01:00
|
|
|
namespace {
|
|
|
|
template <typename OutputIterator>
|
|
|
|
std::pair<OutputIterator, size_t> copy(const ImmutableString &src, size_t avail,
|
|
|
|
OutputIterator oitr) {
|
|
|
|
return copy(src.c_str(), src.size(), avail, oitr);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-10-28 15:12:16 +01:00
|
|
|
namespace {
|
|
|
|
template <size_t N, typename OutputIterator>
|
2016-10-15 11:36:04 +02:00
|
|
|
std::pair<OutputIterator, size_t> copy_l(const char (&src)[N], size_t avail,
|
2015-10-28 15:12:16 +01:00
|
|
|
OutputIterator oitr) {
|
|
|
|
return copy(src, N - 1, avail, oitr);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-06-28 09:37:17 +02:00
|
|
|
namespace {
|
|
|
|
const char LOWER_XDIGITS[] = "0123456789abcdef";
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <typename OutputIterator>
|
|
|
|
std::pair<OutputIterator, size_t> copy_hex_low(const uint8_t *src,
|
|
|
|
size_t srclen, size_t avail,
|
|
|
|
OutputIterator oitr) {
|
|
|
|
auto nwrite = std::min(srclen * 2, avail) / 2;
|
2015-06-30 14:33:54 +02:00
|
|
|
for (auto i = 0u; i < nwrite; ++i) {
|
2015-06-28 09:37:17 +02:00
|
|
|
*oitr++ = LOWER_XDIGITS[src[i] >> 4];
|
|
|
|
*oitr++ = LOWER_XDIGITS[src[i] & 0xf];
|
|
|
|
}
|
|
|
|
return std::make_pair(oitr, avail - nwrite);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-05-04 15:45:34 +02:00
|
|
|
void upstream_accesslog(const std::vector<LogFragment> &lfv,
|
|
|
|
const LogSpec &lgsp) {
|
2015-03-03 17:09:15 +01:00
|
|
|
auto lgconf = log_config();
|
2016-01-18 09:26:27 +01:00
|
|
|
auto &accessconf = get_config()->logging.access;
|
2014-08-19 14:33:54 +02:00
|
|
|
|
2016-01-18 09:26:27 +01:00
|
|
|
if (lgconf->accesslog_fd == -1 && !accessconf.syslog) {
|
2014-07-05 11:22:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
char buf[4_k];
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-05-04 15:45:34 +02:00
|
|
|
auto downstream = lgsp.downstream;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
const Request *req = nullptr;
|
|
|
|
if (downstream) {
|
|
|
|
req = &downstream->request();
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:56:44 +01:00
|
|
|
auto p = buf;
|
|
|
|
auto avail = sizeof(buf) - 2;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-05-04 15:45:34 +02:00
|
|
|
lgconf->update_tstamp(lgsp.time_now);
|
2016-10-10 15:02:48 +02:00
|
|
|
auto &time_local = lgconf->time_local;
|
|
|
|
auto &time_iso8601 = lgconf->time_iso8601;
|
2015-01-21 15:52:30 +01: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:
|
2016-01-17 07:04:09 +01:00
|
|
|
std::tie(p, avail) = copy(lf.value, avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_REMOTE_ADDR:
|
2015-05-04 15:45:34 +02:00
|
|
|
std::tie(p, avail) = copy(lgsp.remote_addr, avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_TIME_LOCAL:
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(time_local, avail, p);
|
2014-11-19 17:53:30 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_TIME_ISO8601:
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(time_iso8601, avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_REQUEST:
|
2015-05-04 15:45:34 +02:00
|
|
|
std::tie(p, avail) = copy(lgsp.method, avail, p);
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l(" ", avail, p);
|
2015-05-04 15:45:34 +02:00
|
|
|
std::tie(p, avail) = copy(lgsp.path, avail, p);
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l(" HTTP/", avail, p);
|
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp.major), avail, p);
|
2015-05-04 15:45:34 +02:00
|
|
|
if (lgsp.major < 2) {
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l(".", avail, p);
|
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp.minor), avail, p);
|
2015-02-24 06:43:01 +01:00
|
|
|
}
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_STATUS:
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp.status), avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_BODY_BYTES_SENT:
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp.body_bytes_sent), avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_HTTP:
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req) {
|
2016-10-02 14:22:02 +02:00
|
|
|
auto hd = req->fs.header(lf.value);
|
2015-01-04 15:22:39 +01:00
|
|
|
if (hd) {
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy((*hd).value, avail, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_AUTHORITY:
|
2016-01-13 14:45:52 +01:00
|
|
|
if (req) {
|
|
|
|
if (!req->authority.empty()) {
|
|
|
|
std::tie(p, avail) = copy(req->authority, avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
2014-11-19 17:53:30 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_REMOTE_PORT:
|
2015-05-04 15:45:34 +02:00
|
|
|
std::tie(p, avail) = copy(lgsp.remote_port, avail, p);
|
2014-11-19 17:53:30 +01:00
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_SERVER_PORT:
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp.server_port), 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>(
|
2016-10-15 11:36:04 +02:00
|
|
|
lgsp.request_end_time - lgsp.request_start_time)
|
|
|
|
.count();
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-11-27 16:03:16 +01:00
|
|
|
sec += '.';
|
2014-11-27 15:39:04 +01:00
|
|
|
sec += frac;
|
|
|
|
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(sec, avail, p);
|
2014-11-27 15:39:04 +01:00
|
|
|
} break;
|
2014-11-24 06:34:43 +01:00
|
|
|
case SHRPX_LOGF_PID:
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy(util::utos(lgsp.pid), avail, p);
|
2014-11-24 06:34:43 +01:00
|
|
|
break;
|
2014-11-24 07:22:10 +01:00
|
|
|
case SHRPX_LOGF_ALPN:
|
2015-05-04 15:45:34 +02:00
|
|
|
std::tie(p, avail) = copy(lgsp.alpn, avail, p);
|
2014-11-24 07:22:10 +01:00
|
|
|
break;
|
2015-06-28 09:37:17 +02:00
|
|
|
case SHRPX_LOGF_SSL_CIPHER:
|
|
|
|
if (!lgsp.tls_info) {
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
2015-06-28 09:37:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(p, avail) = copy(lgsp.tls_info->cipher, avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_SSL_PROTOCOL:
|
|
|
|
if (!lgsp.tls_info) {
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
2015-06-28 09:37:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(p, avail) = copy(lgsp.tls_info->protocol, avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_SSL_SESSION_ID:
|
|
|
|
if (!lgsp.tls_info || lgsp.tls_info->session_id_length == 0) {
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
2015-06-28 09:37:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(p, avail) =
|
|
|
|
copy_hex_low(lgsp.tls_info->session_id,
|
|
|
|
lgsp.tls_info->session_id_length, avail, p);
|
|
|
|
break;
|
2015-06-28 15:15:04 +02:00
|
|
|
case SHRPX_LOGF_SSL_SESSION_REUSED:
|
|
|
|
if (!lgsp.tls_info) {
|
2015-10-28 15:12:16 +01:00
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
2015-06-28 15:15:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(p, avail) =
|
2015-10-28 15:12:16 +01:00
|
|
|
copy_l(lgsp.tls_info->session_reused ? "r" : ".", avail, p);
|
2015-06-28 15:15:04 +02:00
|
|
|
break;
|
2016-08-04 17:04:47 +02:00
|
|
|
case SHRPX_LOGF_BACKEND_HOST:
|
|
|
|
if (!lgsp.downstream_addr) {
|
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(p, avail) = copy(lgsp.downstream_addr->host, avail, p);
|
|
|
|
break;
|
|
|
|
case SHRPX_LOGF_BACKEND_PORT:
|
|
|
|
if (!lgsp.downstream_addr) {
|
|
|
|
std::tie(p, avail) = copy_l("-", avail, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::tie(p, avail) =
|
|
|
|
copy(util::utos(lgsp.downstream_addr->port), 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
|
|
|
|
2016-01-18 09:26:27 +01:00
|
|
|
if (accessconf.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;
|
2015-02-25 16:02:29 +01:00
|
|
|
while (write(lgconf->accesslog_fd, buf, nwrite) == -1 && errno == EINTR)
|
2014-11-27 15:39:04 +01:00
|
|
|
;
|
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;
|
2015-08-12 20:05:10 +02:00
|
|
|
int new_accesslog_fd = -1;
|
|
|
|
int new_errorlog_fd = -1;
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-03-03 17:09:15 +01:00
|
|
|
auto lgconf = log_config();
|
2016-10-08 04:34:23 +02:00
|
|
|
auto config = get_config();
|
|
|
|
auto &accessconf = config->logging.access;
|
|
|
|
auto &errorconf = config->logging.error;
|
2014-08-19 14:33:54 +02:00
|
|
|
|
2016-02-14 13:32:27 +01:00
|
|
|
if (!accessconf.syslog && !accessconf.file.empty()) {
|
|
|
|
new_accesslog_fd = util::open_log_file(accessconf.file.c_str());
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-08-12 20:05:10 +02:00
|
|
|
if (new_accesslog_fd == -1) {
|
2016-02-14 13:32:27 +01:00
|
|
|
LOG(ERROR) << "Failed to open accesslog file " << accessconf.file;
|
2014-07-05 11:22:40 +02:00
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 13:32:27 +01:00
|
|
|
if (!errorconf.syslog && !errorconf.file.empty()) {
|
|
|
|
new_errorlog_fd = util::open_log_file(errorconf.file.c_str());
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (new_errorlog_fd == -1) {
|
2015-02-25 16:02:29 +01:00
|
|
|
if (lgconf->errorlog_fd != -1) {
|
2016-02-14 13:32:27 +01:00
|
|
|
LOG(ERROR) << "Failed to open errorlog file " << errorconf.file;
|
2014-07-05 11:22:40 +02:00
|
|
|
} else {
|
2016-02-14 13:32:27 +01:00
|
|
|
std::cerr << "Failed to open errorlog file " << errorconf.file
|
2016-01-18 09:26:27 +01:00
|
|
|
<< std::endl;
|
2014-07-05 11:22:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:05:10 +02:00
|
|
|
util::close_log_file(lgconf->accesslog_fd);
|
|
|
|
util::close_log_file(lgconf->errorlog_fd);
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2015-08-12 20:05:10 +02:00
|
|
|
lgconf->accesslog_fd = new_accesslog_fd;
|
|
|
|
lgconf->errorlog_fd = new_errorlog_fd;
|
2015-10-15 14:42:11 +02:00
|
|
|
lgconf->errorlog_tty =
|
|
|
|
(new_errorlog_fd == -1) ? false : isatty(new_errorlog_fd);
|
2014-07-05 11:22:40 +02:00
|
|
|
|
|
|
|
return res;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2015-09-23 12:45:53 +02:00
|
|
|
void log_chld(pid_t pid, int rstatus, const char *msg) {
|
|
|
|
std::string signalstr;
|
|
|
|
if (WIFSIGNALED(rstatus)) {
|
|
|
|
signalstr += "; signal ";
|
|
|
|
auto sig = WTERMSIG(rstatus);
|
|
|
|
auto s = strsignal(sig);
|
|
|
|
if (s) {
|
|
|
|
signalstr += s;
|
2015-11-27 16:03:16 +01:00
|
|
|
signalstr += '(';
|
2015-09-23 12:45:53 +02:00
|
|
|
} else {
|
|
|
|
signalstr += "UNKNOWN(";
|
|
|
|
}
|
|
|
|
signalstr += util::utos(sig);
|
2015-11-27 16:03:16 +01:00
|
|
|
signalstr += ')';
|
2015-09-23 12:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(NOTICE) << msg << ": [" << pid << "] exited "
|
|
|
|
<< (WIFEXITED(rstatus) ? "normally" : "abnormally")
|
|
|
|
<< " with status " << std::hex << rstatus << std::oct
|
|
|
|
<< "; exit status " << WEXITSTATUS(rstatus)
|
|
|
|
<< (signalstr.empty() ? "" : signalstr.c_str());
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:59:58 +02:00
|
|
|
void redirect_stderr_to_errorlog() {
|
|
|
|
auto lgconf = log_config();
|
2016-01-18 09:26:27 +01:00
|
|
|
auto &errorconf = get_config()->logging.error;
|
2015-04-08 09:59:58 +02:00
|
|
|
|
2016-01-18 09:26:27 +01:00
|
|
|
if (errorconf.syslog || lgconf->errorlog_fd == -1) {
|
2015-04-08 09:59:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dup2(lgconf->errorlog_fd, STDERR_FILENO);
|
|
|
|
}
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
} // namespace shrpx
|