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
|
|
|
|
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>
|
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 {
|
2012-06-04 16:48:31 +02:00
|
|
|
const char *SEVERITY_STR[] = {
|
|
|
|
"INFO", "WARN", "ERROR", "FATAL"
|
|
|
|
};
|
2012-12-09 13:02:48 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const char *SEVERITY_COLOR[] = {
|
|
|
|
"\033[1;32m", // INFO
|
|
|
|
"\033[1;33m", // WARN
|
|
|
|
"\033[1;31m", // ERROR
|
|
|
|
"\033[1;35m", // FATAL
|
|
|
|
};
|
|
|
|
} // namespace
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-06-05 19:23:07 +02:00
|
|
|
int Log::severity_thres_ = WARNING;
|
|
|
|
|
|
|
|
void Log::set_severity_level(int severity)
|
|
|
|
{
|
|
|
|
severity_thres_ = severity;
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:58:19 +02:00
|
|
|
int Log::set_severity_level_by_name(const char *name)
|
|
|
|
{
|
|
|
|
for(size_t i = 0, max = sizeof(SEVERITY_STR)/sizeof(char*); i < max; ++i) {
|
|
|
|
if(strcmp(SEVERITY_STR[i], name) == 0) {
|
|
|
|
severity_thres_ = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-01 18:20:18 +02:00
|
|
|
int severity_to_syslog_level(int severity)
|
|
|
|
{
|
|
|
|
switch(severity) {
|
|
|
|
case(INFO):
|
|
|
|
return LOG_INFO;
|
|
|
|
case(WARNING):
|
|
|
|
return LOG_WARNING;
|
|
|
|
case(ERROR):
|
|
|
|
return LOG_ERR;
|
|
|
|
case(FATAL):
|
|
|
|
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)
|
2013-12-06 15:17:38 +01:00
|
|
|
: filename_(filename),
|
|
|
|
severity_(severity),
|
2012-06-04 16:48:31 +02:00
|
|
|
linenum_(linenum)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Log::~Log()
|
|
|
|
{
|
2014-07-05 11:22:40 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
if(!log_enabled(severity_) ||
|
|
|
|
(worker_config.errorlog_fd == -1 && !get_config()->errorlog_syslog)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(get_config()->errorlog_syslog) {
|
|
|
|
syslog(severity_to_syslog_level(severity_), "[%s] %s (%s:%d)",
|
|
|
|
SEVERITY_STR[severity_], stream_.str().c_str(),
|
|
|
|
filename_, linenum_);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
auto tty = worker_config.errorlog_tty;
|
|
|
|
|
2014-07-06 06:27:51 +02:00
|
|
|
auto cached_time = get_config()->cached_time;
|
|
|
|
|
2014-07-05 11:22:40 +02:00
|
|
|
rv = snprintf(buf, sizeof(buf),
|
2014-07-06 12:26:12 +02:00
|
|
|
"%s PID%d [%s%s%s] %s%s:%d%s %s\n",
|
2014-07-06 06:27:51 +02:00
|
|
|
cached_time->c_str(),
|
2014-07-05 15:53:17 +02:00
|
|
|
getpid(),
|
2014-07-05 11:22:40 +02:00
|
|
|
tty ? SEVERITY_COLOR[severity_] : "",
|
|
|
|
SEVERITY_STR[severity_],
|
|
|
|
tty ? "\033[0m" : "",
|
|
|
|
tty ? "\033[1;30m" : "",
|
|
|
|
filename_, linenum_,
|
2014-07-06 12:26:12 +02:00
|
|
|
tty ? "\033[0m" : "",
|
|
|
|
stream_.str().c_str());
|
2014-07-05 11:22:40 +02:00
|
|
|
|
|
|
|
if(rv < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nwrite = std::min(static_cast<size_t>(rv), sizeof(buf) - 1);
|
|
|
|
|
|
|
|
write(worker_config.errorlog_fd, buf, nwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
void upstream_accesslog(const std::string& client_ip, unsigned int status_code,
|
|
|
|
Downstream *downstream)
|
|
|
|
{
|
|
|
|
if(worker_config.accesslog_fd == -1 && !get_config()->accesslog_syslog) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
const char *path;
|
|
|
|
const char *method;
|
|
|
|
unsigned int major, minor;
|
|
|
|
const char *user_agent;
|
|
|
|
int64_t response_bodylen;
|
|
|
|
|
|
|
|
if(!downstream) {
|
|
|
|
path = "-";
|
|
|
|
method = "-";
|
|
|
|
major = 1;
|
|
|
|
minor = 0;
|
|
|
|
user_agent = "-";
|
|
|
|
response_bodylen = 0;
|
|
|
|
} else {
|
|
|
|
if(downstream->get_request_path().empty()) {
|
|
|
|
path = downstream->get_request_http2_authority().c_str();
|
|
|
|
} else {
|
|
|
|
path = downstream->get_request_path().c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
method = downstream->get_request_method().c_str();
|
|
|
|
major = downstream->get_request_major();
|
|
|
|
minor = downstream->get_request_minor();
|
|
|
|
user_agent = downstream->get_request_user_agent().c_str();
|
2014-07-05 12:48:14 +02:00
|
|
|
if(!user_agent[0]) {
|
|
|
|
user_agent = "-";
|
|
|
|
}
|
2014-07-05 11:22:40 +02:00
|
|
|
response_bodylen = downstream->get_response_bodylen();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char fmt[] =
|
2014-07-05 11:51:55 +02:00
|
|
|
"%s - - [%s] \"%s %s HTTP/%u.%u\" %u %lld \"-\" \"%s\"\n";
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-07-06 06:27:51 +02:00
|
|
|
auto cached_time = get_config()->cached_time;
|
|
|
|
|
2014-07-05 11:22:40 +02:00
|
|
|
rv = snprintf(buf, sizeof(buf), fmt,
|
|
|
|
client_ip.c_str(),
|
2014-07-06 06:27:51 +02:00
|
|
|
cached_time->c_str(),
|
2014-07-05 11:22:40 +02:00
|
|
|
method,
|
|
|
|
path,
|
|
|
|
major,
|
|
|
|
minor,
|
|
|
|
status_code,
|
2014-07-05 11:51:55 +02:00
|
|
|
(long long int)response_bodylen,
|
2014-07-05 11:22:40 +02:00
|
|
|
user_agent);
|
|
|
|
|
|
|
|
if(rv < 0) {
|
2014-06-12 14:46:25 +02:00
|
|
|
return;
|
2012-06-05 19:23:07 +02:00
|
|
|
}
|
2014-06-12 14:46:25 +02:00
|
|
|
|
2014-07-05 11:22:40 +02:00
|
|
|
auto nwrite = std::min(static_cast<size_t>(rv), sizeof(buf) - 1);
|
|
|
|
|
|
|
|
if(get_config()->accesslog_syslog) {
|
|
|
|
syslog(LOG_INFO, "%s", buf);
|
2014-06-12 14:46:25 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-05 11:22:40 +02:00
|
|
|
write(worker_config.accesslog_fd, buf, nwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
int reopen_log_files()
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if(worker_config.accesslog_fd != -1) {
|
|
|
|
close(worker_config.accesslog_fd);
|
|
|
|
worker_config.accesslog_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!get_config()->accesslog_syslog && get_config()->accesslog_file) {
|
|
|
|
|
|
|
|
worker_config.accesslog_fd =
|
|
|
|
util::reopen_log_file(get_config()->accesslog_file.get());
|
|
|
|
|
|
|
|
if(worker_config.accesslog_fd == -1) {
|
|
|
|
LOG(ERROR) << "Failed to open accesslog file "
|
|
|
|
<< get_config()->accesslog_file.get();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int new_errorlog_fd = -1;
|
|
|
|
|
|
|
|
if(!get_config()->errorlog_syslog && get_config()->errorlog_file) {
|
|
|
|
|
|
|
|
new_errorlog_fd = util::reopen_log_file(get_config()->errorlog_file.get());
|
|
|
|
|
|
|
|
if(new_errorlog_fd == -1) {
|
|
|
|
if(worker_config.errorlog_fd != -1) {
|
|
|
|
LOG(ERROR) << "Failed to open errorlog file "
|
|
|
|
<< get_config()->errorlog_file.get();
|
|
|
|
} else {
|
|
|
|
std::cerr << "Failed to open errorlog file "
|
|
|
|
<< get_config()->errorlog_file.get()
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(worker_config.errorlog_fd != -1) {
|
|
|
|
close(worker_config.errorlog_fd);
|
|
|
|
worker_config.errorlog_fd = -1;
|
|
|
|
worker_config.errorlog_tty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(new_errorlog_fd != -1) {
|
|
|
|
worker_config.errorlog_fd = new_errorlog_fd;
|
|
|
|
worker_config.errorlog_tty = isatty(worker_config.errorlog_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|