nghttpx: Don't stderr log if syslog is used

This change also reverts previous commits and tty is set to false if
syslog is used.
This commit is contained in:
Tatsuhiro Tsujikawa 2014-06-12 21:46:25 +09:00
parent 6a2950aef0
commit 1af9a9cee0
4 changed files with 83 additions and 68 deletions

View File

@ -280,10 +280,6 @@ int event_loop()
LOG(FATAL) << "Failed to daemonize: " << strerror(errno);
exit(EXIT_FAILURE);
}
// Set tty = false when daemonized. syslog is garbling with ANSI
// escape.
mod_config()->tty = false;
}
if(get_config()->pid_file) {
@ -1182,6 +1178,13 @@ int main(int argc, char **argv)
}
}
if(get_config()->syslog) {
openlog("nghttpx", LOG_NDELAY | LOG_NOWAIT | LOG_PID,
get_config()->syslog_facility);
mod_config()->use_syslog = true;
mod_config()->tty = false;
}
if(get_config()->npn_list.empty()) {
mod_config()->npn_list = parse_config_str_list(DEFAULT_NPN_LIST);
}
@ -1303,12 +1306,6 @@ int main(int argc, char **argv)
}
}
if(get_config()->syslog) {
openlog("nghttpx", LOG_NDELAY | LOG_NOWAIT | LOG_PID,
get_config()->syslog_facility);
mod_config()->use_syslog = true;
}
mod_config()->rate_limit_cfg = ev_token_bucket_cfg_new
(get_rate_limit(get_config()->read_rate),
get_rate_limit(get_config()->read_burst),

View File

@ -36,8 +36,9 @@
namespace shrpx {
namespace {
void get_datestr(char *buf)
std::string get_datestr()
{
char buf[64];
time_t now = time(0);
if(ctime_r(&now, buf) == 0) {
buf[0] = '\0';
@ -49,18 +50,22 @@ void get_datestr(char *buf)
buf[strlen(buf)-1] = '\0';
}
}
return buf;
}
} // namespace
void upstream_connect(const std::string& client_ip)
{
char datestr[64];
get_datestr(datestr);
fprintf(stderr, "%s [%s] ACCEPT\n", client_ip.c_str(), datestr);
fflush(stderr);
if(get_config()->use_syslog) {
syslog(LOG_INFO, "%s ACCEPT\n", client_ip.c_str());
return;
}
fprintf(stderr, "%s [%s] ACCEPT\n", client_ip.c_str(),
get_datestr().c_str());
fflush(stderr);
}
namespace {
@ -85,49 +90,57 @@ const char* status_code_color(unsigned int status_code)
void upstream_response(const std::string& client_ip, unsigned int status_code,
Downstream *downstream)
{
char datestr[64];
get_datestr(datestr);
if(downstream) {
const char *path;
if(downstream->get_request_path().empty()) {
path = downstream->get_request_http2_authority().c_str();
} else {
path = downstream->get_request_path().c_str();
}
fprintf(stderr, "%s%s [%s] %u%s %d \"%s %s HTTP/%u.%u\"\n",
get_config()->tty ? status_code_color(status_code) : "",
client_ip.c_str(), datestr,
status_code,
get_config()->tty ? "\033[0m" : "",
downstream->get_stream_id(),
downstream->get_request_method().c_str(),
path,
downstream->get_request_major(),
downstream->get_request_minor());
fflush(stderr);
if(get_config()->use_syslog) {
syslog(LOG_INFO, "%s %u %d \"%s %s HTTP/%u.%u\"\n",
client_ip.c_str(),
status_code,
downstream->get_stream_id(),
downstream->get_request_method().c_str(),
path,
downstream->get_request_major(),
downstream->get_request_minor());
}
} else {
fprintf(stderr, "%s%s [%s] %u%s 0 \"-\"\n",
get_config()->tty ? status_code_color(status_code) : "",
client_ip.c_str(), datestr,
status_code,
get_config()->tty ? "\033[0m" : "");
if(!downstream) {
if(get_config()->use_syslog) {
syslog(LOG_INFO, "%s %u 0 \"-\"\n", client_ip.c_str(), status_code);
return;
}
fprintf(stderr, "%s%s [%s] %u%s 0 \"-\"\n",
get_config()->tty ? status_code_color(status_code) : "",
client_ip.c_str(), get_datestr().c_str(),
status_code,
get_config()->tty ? "\033[0m" : "");
fflush(stderr);
return;
}
const char *path;
if(downstream->get_request_path().empty()) {
path = downstream->get_request_http2_authority().c_str();
} else {
path = downstream->get_request_path().c_str();
}
if(get_config()->use_syslog) {
syslog(LOG_INFO, "%s %u %d \"%s %s HTTP/%u.%u\"",
client_ip.c_str(),
status_code,
downstream->get_stream_id(),
downstream->get_request_method().c_str(),
path,
downstream->get_request_major(),
downstream->get_request_minor());
return;
}
fprintf(stderr, "%s [%s] %s%u%s stream(%d) \"%s %s HTTP/%u.%u\"\n",
client_ip.c_str(), get_datestr().c_str(),
get_config()->tty ? status_code_color(status_code) : "",
status_code,
get_config()->tty ? "\033[0m" : "",
downstream->get_stream_id(),
downstream->get_request_method().c_str(),
path,
downstream->get_request_major(),
downstream->get_request_minor());
fflush(stderr);
}
} // namespace shrpx

View File

@ -251,7 +251,7 @@ struct Config {
bool insecure;
bool backend_ipv4;
bool backend_ipv6;
// true if stderr refers to a terminal and not daemonized
// true if stderr refers to a terminal and syslog is not used
bool tty;
bool http2_no_cookie_crumbling;
bool upstream_frame_debug;

View File

@ -90,21 +90,26 @@ Log::Log(int severity, const char *filename, int linenum)
Log::~Log()
{
if(log_enabled(severity_)) {
fprintf(stderr, "[%s%s%s] %s\n %s(%s:%d)%s\n",
get_config()->tty ? SEVERITY_COLOR[severity_] : "",
SEVERITY_STR[severity_],
get_config()->tty ? "\033[0m" : "",
stream_.str().c_str(),
get_config()->tty ? "\033[1;30m" : "",
filename_, linenum_,
get_config()->tty ? "\033[0m" : "");
fflush(stderr);
if(get_config()->use_syslog) {
syslog(severity_to_syslog_level(severity_), "%s (%s:%d)\n",
stream_.str().c_str(), filename_, linenum_);
}
if(!log_enabled(severity_)) {
return;
}
if(get_config()->use_syslog) {
syslog(severity_to_syslog_level(severity_), "%s (%s:%d)",
stream_.str().c_str(), filename_, linenum_);
return;
}
fprintf(stderr, "[%s%s%s] %s\n %s(%s:%d)%s\n",
get_config()->tty ? SEVERITY_COLOR[severity_] : "",
SEVERITY_STR[severity_],
get_config()->tty ? "\033[0m" : "",
stream_.str().c_str(),
get_config()->tty ? "\033[1;30m" : "",
filename_, linenum_,
get_config()->tty ? "\033[0m" : "");
fflush(stderr);
}
} // namespace shrpx