shrpx: Add syslog support
This commit is contained in:
parent
7962c1bf6c
commit
0c10528ca3
|
@ -34,6 +34,7 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <limits>
|
||||
#include <cstdlib>
|
||||
|
@ -328,6 +329,10 @@ void fill_default_config()
|
|||
SPDYLAY_INITIAL_MAX_CONCURRENT_STREAMS;
|
||||
|
||||
set_config_str(&mod_config()->conf_path, "/etc/shrpx/shrpx.conf");
|
||||
|
||||
mod_config()->syslog = false;
|
||||
mod_config()->syslog_facility = LOG_DAEMON;
|
||||
mod_config()->use_syslog = false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
@ -414,6 +419,11 @@ void print_help(std::ostream& out)
|
|||
<< " --conf=<PATH> Load configuration from PATH.\n"
|
||||
<< " Default: "
|
||||
<< get_config()->conf_path << "\n"
|
||||
<< " --syslog Send log messages to syslog.\n"
|
||||
<< " --syslog-facility=<FACILITY>\n"
|
||||
<< " Set syslog facility.\n"
|
||||
<< " Default: "
|
||||
<< str_syslog_facility(get_config()->syslog_facility) << "\n"
|
||||
<< " -h, --help Print this help.\n"
|
||||
<< std::endl;
|
||||
}
|
||||
|
@ -448,6 +458,8 @@ int main(int argc, char **argv)
|
|||
{"pid-file", required_argument, &flag, 10 },
|
||||
{"user", required_argument, &flag, 11 },
|
||||
{"conf", required_argument, &flag, 12 },
|
||||
{"syslog", no_argument, &flag, 13 },
|
||||
{"syslog-facility", required_argument, &flag, 14 },
|
||||
{"help", no_argument, 0, 'h' },
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
|
@ -540,6 +552,14 @@ int main(int argc, char **argv)
|
|||
// --conf
|
||||
set_config_str(&mod_config()->conf_path, optarg);
|
||||
break;
|
||||
case 13:
|
||||
// --syslog
|
||||
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SYSLOG, "yes"));
|
||||
break;
|
||||
case 14:
|
||||
// --syslog-facility
|
||||
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SYSLOG_FACILITY, optarg));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -598,6 +618,12 @@ int main(int argc, char **argv)
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(get_config()->syslog) {
|
||||
openlog("shrpx", LOG_NDELAY | LOG_NOWAIT | LOG_PID,
|
||||
get_config()->syslog_facility);
|
||||
mod_config()->use_syslog = true;
|
||||
}
|
||||
|
||||
if(get_config()->daemon) {
|
||||
if(daemon(0, 0) == -1) {
|
||||
LOG(FATAL) << "Failed to daemonize: " << strerror(errno);
|
||||
|
|
|
@ -24,10 +24,14 @@
|
|||
*/
|
||||
#include "shrpx_accesslog.h"
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include <ctime>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "shrpx_config.h"
|
||||
|
||||
namespace shrpx {
|
||||
|
||||
namespace {
|
||||
|
@ -51,17 +55,23 @@ void upstream_connect(const std::string& client_ip)
|
|||
{
|
||||
char datestr[64];
|
||||
get_datestr(datestr);
|
||||
fprintf(stderr, "[%s] %s\n", datestr, client_ip.c_str());
|
||||
fprintf(stderr, "[%s] Accepted %s\n", datestr, client_ip.c_str());
|
||||
fflush(stderr);
|
||||
if(get_config()->use_syslog) {
|
||||
syslog(LOG_INFO, "Accepted %s\n", client_ip.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void upstream_spdy_stream(const std::string& client_ip, int32_t stream_id)
|
||||
{
|
||||
char datestr[64];
|
||||
get_datestr(datestr);
|
||||
fprintf(stderr, "[%s] %s stream_id=%d\n", datestr, client_ip.c_str(),
|
||||
fprintf(stderr, "[%s] %s SPDY stream_id=%d\n", datestr, client_ip.c_str(),
|
||||
stream_id);
|
||||
fflush(stderr);
|
||||
if(get_config()->use_syslog) {
|
||||
syslog(LOG_INFO, "%s SPDY stream_id=%d\n", client_ip.c_str(), stream_id);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace shrpx
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <pwd.h>
|
||||
#include <netdb.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
|
@ -63,6 +64,8 @@ SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT[] = "backend-keep-alive-timeout";
|
|||
const char SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS[] = "frontend-spdy-window-bits";
|
||||
const char SHRPX_OPT_PID_FILE[] = "pid-file";
|
||||
const char SHRPX_OPT_USER[] = "user";
|
||||
const char SHRPX_OPT_SYSLOG[] = "syslog";
|
||||
const char SHRPX_OPT_SYSLOG_FACILITY[] = "syslog-facility";
|
||||
|
||||
Config::Config()
|
||||
: verbose(false),
|
||||
|
@ -86,7 +89,10 @@ Config::Config()
|
|||
pid_file(0),
|
||||
uid(0),
|
||||
gid(0),
|
||||
conf_path(0)
|
||||
conf_path(0),
|
||||
syslog(false),
|
||||
syslog_facility(0),
|
||||
use_syslog(false)
|
||||
{}
|
||||
|
||||
namespace {
|
||||
|
@ -223,6 +229,15 @@ int parse_config(const char *opt, const char *optarg)
|
|||
set_config_str(&mod_config()->private_key_file, optarg);
|
||||
} else if(util::strieq(opt, SHRPX_OPT_CERTIFICATE_FILE)) {
|
||||
set_config_str(&mod_config()->cert_file, optarg);
|
||||
} else if(util::strieq(opt, SHRPX_OPT_SYSLOG)) {
|
||||
mod_config()->syslog = util::strieq(optarg, "yes");
|
||||
} else if(util::strieq(opt, SHRPX_OPT_SYSLOG_FACILITY)) {
|
||||
int facility = int_syslog_facility(optarg);
|
||||
if(facility == -1) {
|
||||
LOG(ERROR) << "Unknown syslog facility: " << optarg;
|
||||
return -1;
|
||||
}
|
||||
mod_config()->syslog_facility = facility;
|
||||
} else if(util::strieq(opt, "conf")) {
|
||||
LOG(WARNING) << "conf is ignored";
|
||||
} else {
|
||||
|
@ -262,4 +277,97 @@ int load_config(const char *filename)
|
|||
return 0;
|
||||
}
|
||||
|
||||
const char* str_syslog_facility(int facility)
|
||||
{
|
||||
switch(facility) {
|
||||
case(LOG_AUTH):
|
||||
return "auth";
|
||||
case(LOG_AUTHPRIV):
|
||||
return "authpriv";
|
||||
case(LOG_CRON):
|
||||
return "cron";
|
||||
case(LOG_DAEMON):
|
||||
return "daemon";
|
||||
case(LOG_FTP):
|
||||
return "ftp";
|
||||
case(LOG_KERN):
|
||||
return "kern";
|
||||
case(LOG_LOCAL0):
|
||||
return "local0";
|
||||
case(LOG_LOCAL1):
|
||||
return "local1";
|
||||
case(LOG_LOCAL2):
|
||||
return "local2";
|
||||
case(LOG_LOCAL3):
|
||||
return "local3";
|
||||
case(LOG_LOCAL4):
|
||||
return "local4";
|
||||
case(LOG_LOCAL5):
|
||||
return "local5";
|
||||
case(LOG_LOCAL6):
|
||||
return "local6";
|
||||
case(LOG_LOCAL7):
|
||||
return "local7";
|
||||
case(LOG_LPR):
|
||||
return "lpr";
|
||||
case(LOG_MAIL):
|
||||
return "mail";
|
||||
case(LOG_SYSLOG):
|
||||
return "syslog";
|
||||
case(LOG_USER):
|
||||
return "user";
|
||||
case(LOG_UUCP):
|
||||
return "uucp";
|
||||
default:
|
||||
return "(unknown)";
|
||||
}
|
||||
}
|
||||
|
||||
int int_syslog_facility(const char *strfacility)
|
||||
{
|
||||
if(util::strieq(strfacility, "auth")) {
|
||||
return LOG_AUTH;
|
||||
} else if(util::strieq(strfacility, "authpriv")) {
|
||||
return LOG_AUTHPRIV;
|
||||
} else if(util::strieq(strfacility, "cron")) {
|
||||
return LOG_CRON;
|
||||
} else if(util::strieq(strfacility, "daemon")) {
|
||||
return LOG_DAEMON;
|
||||
} else if(util::strieq(strfacility, "ftp")) {
|
||||
return LOG_FTP;
|
||||
} else if(util::strieq(strfacility, "kern")) {
|
||||
return LOG_KERN;
|
||||
} else if(util::strieq(strfacility, "local0")) {
|
||||
return LOG_LOCAL0;
|
||||
} else if(util::strieq(strfacility, "local1")) {
|
||||
return LOG_LOCAL1;
|
||||
} else if(util::strieq(strfacility, "local2")) {
|
||||
return LOG_LOCAL2;
|
||||
} else if(util::strieq(strfacility, "local3")) {
|
||||
return LOG_LOCAL3;
|
||||
} else if(util::strieq(strfacility, "local4")) {
|
||||
return LOG_LOCAL4;
|
||||
} else if(util::strieq(strfacility, "local5")) {
|
||||
return LOG_LOCAL5;
|
||||
} else if(util::strieq(strfacility, "local6")) {
|
||||
return LOG_LOCAL6;
|
||||
} else if(util::strieq(strfacility, "local7")) {
|
||||
return LOG_LOCAL7;
|
||||
} else if(util::strieq(strfacility, "lpr")) {
|
||||
return LOG_LPR;
|
||||
} else if(util::strieq(strfacility, "mail")) {
|
||||
return LOG_MAIL;
|
||||
} else if(util::strieq(strfacility, "news")) {
|
||||
return LOG_NEWS;
|
||||
} else if(util::strieq(strfacility, "syslog")) {
|
||||
return LOG_SYSLOG;
|
||||
} else if(util::strieq(strfacility, "user")) {
|
||||
return LOG_USER;
|
||||
} else if(util::strieq(strfacility, "uucp")) {
|
||||
return LOG_UUCP;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace shrpx
|
||||
|
|
|
@ -56,6 +56,8 @@ extern const char SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT[];
|
|||
extern const char SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS[];
|
||||
extern const char SHRPX_OPT_PID_FILE[];
|
||||
extern const char SHRPX_OPT_USER[];
|
||||
extern const char SHRPX_OPT_SYSLOG[];
|
||||
extern const char SHRPX_OPT_SYSLOG_FACILITY[];
|
||||
|
||||
union sockaddr_union {
|
||||
sockaddr sa;
|
||||
|
@ -94,6 +96,10 @@ struct Config {
|
|||
uid_t uid;
|
||||
gid_t gid;
|
||||
char *conf_path;
|
||||
bool syslog;
|
||||
int syslog_facility;
|
||||
// This member finally decides syslog is used or not
|
||||
bool use_syslog;
|
||||
Config();
|
||||
};
|
||||
|
||||
|
@ -115,6 +121,12 @@ int load_config(const char *filename);
|
|||
// NULL, it is freed before copying.
|
||||
void set_config_str(char **destp, const char *val);
|
||||
|
||||
// Returns string for syslog |facility|.
|
||||
const char* str_syslog_facility(int facility);
|
||||
|
||||
// Returns integer value of syslog |facility| string.
|
||||
int int_syslog_facility(const char *strfacility);
|
||||
|
||||
} // namespace shrpx
|
||||
|
||||
#endif // SHRPX_CONFIG_H
|
||||
|
|
|
@ -24,9 +24,13 @@
|
|||
*/
|
||||
#include "shrpx_log.h"
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "shrpx_config.h"
|
||||
|
||||
namespace shrpx {
|
||||
|
||||
const char *SEVERITY_STR[] = {
|
||||
|
@ -51,6 +55,23 @@ int Log::set_severity_level_by_name(const char *name)
|
|||
return -1;
|
||||
}
|
||||
|
||||
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:
|
||||
// Not reachable
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
Log::Log(int severity, const char *filename, int linenum)
|
||||
: severity_(severity),
|
||||
filename_(filename),
|
||||
|
@ -64,6 +85,10 @@ Log::~Log()
|
|||
SEVERITY_STR[severity_], stream_.str().c_str(),
|
||||
filename_, linenum_);
|
||||
fflush(stderr);
|
||||
if(get_config()->use_syslog) {
|
||||
syslog(severity_to_syslog_level(severity_), "%s (%s, line %d)\n",
|
||||
stream_.str().c_str(), filename_, linenum_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue