Commit d30103dc authored by Geoff Simmons's avatar Geoff Simmons

trackrdrd: added the -y option to set the syslog facility

parent c719a8b3
......@@ -9,6 +9,11 @@
default, syslog(3) is used for logging. Log levels correspond
to the 'priorities' defined by syslog(3).
-y syslog_facility
Set the syslog facility; legal values are 'user' or 'local0'
through 'local7', and the default is 'local0'. Options -y and
-l are mutually exclusive.
-f varnishlog_bindump
A binary dump of the Varnish SHM log produced by 'varnishlog
-w'. If this option is specified, trackrdrd reads from the
......
......@@ -35,12 +35,16 @@
#include <stdarg.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include "trackrdrd.h"
#include "libvarnish.h"
static const char *level2name[LOG_DEBUG];
static const int facilitynum[8] =
{ LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5,
LOG_LOCAL6, LOG_LOCAL7 };
static void
syslog_setlevel(int level)
......@@ -48,6 +52,22 @@ syslog_setlevel(int level)
setlogmask(LOG_UPTO(level));
}
static int
syslog_getFacility(const char *facility) {
int localnum;
if (strcasecmp(facility, "USER") == 0)
return LOG_USER;
if (strlen(facility) != 6
|| strncasecmp(facility, "LOCAL", 5) != 0
|| !isdigit(facility[5]))
return(-1);
localnum = atoi(&facility[5]);
if (localnum > 7)
return(-1);
return(facilitynum[localnum]);
}
/* XXX: is this safe? */
static void
stdio_initnames(void)
......@@ -88,15 +108,23 @@ stdio_close(void)
fclose(logconf.out);
}
int LOG_Open(const char *progname, const char *dest)
int LOG_Open(const char *progname, const char *dest, const char *facility)
{
if (dest == NULL) {
/* syslog */
int fac = LOG_LOCAL0;
if (facility != NULL) {
fac = syslog_getFacility(facility);
if (fac < 0) {
fprintf(stderr, "Invalid facility: %s\n", facility);
return(-1);
}
}
logconf.log = syslog;
logconf.setlevel = syslog_setlevel;
logconf.close = closelog;
openlog(progname, LOG_PID | LOG_CONS | LOG_NDELAY | LOG_NOWAIT,
LOG_USER);
openlog(progname, LOG_PID | LOG_CONS | LOG_NDELAY | LOG_NOWAIT, fac);
setlogmask(LOG_UPTO(LOG_INFO));
atexit(closelog);
return(0);
......@@ -106,8 +134,10 @@ int LOG_Open(const char *progname, const char *dest)
logconf.out = stdout;
else {
logconf.out = fopen(dest, "w");
if (logconf.out == NULL)
if (logconf.out == NULL) {
perror(dest);
return(-1);
}
}
logconf.level = LOG_INFO;
logconf.log = stdio_log;
......
......@@ -76,10 +76,10 @@ OSL_Track(void *priv, enum VSL_tag_e tag, unsigned fd, unsigned len,
int datalen;
char *data;
(void) priv;
(void) bitmap;
#if 1
(void) spec;
(void) priv;
(void) fd;
#endif
#if 0
......@@ -191,14 +191,15 @@ main(int argc, char * const *argv)
{
int c;
int D_flag = 0, d_flag = 0;
const char *P_arg = NULL, *l_arg = NULL, *n_arg = NULL, *f_arg = NULL;
const char *P_arg = NULL, *l_arg = NULL, *n_arg = NULL, *f_arg = NULL,
*y_arg = NULL;
struct vpf_fh *pfh = NULL;
struct VSM_data *vd;
vd = VSM_New();
VSL_Setup(vd);
while ((c = getopt(argc, argv, "DP:Vn:hl:df:")) != -1) {
while ((c = getopt(argc, argv, "DP:Vn:hl:df:y:")) != -1) {
switch (c) {
case 'D':
D_flag = 1;
......@@ -221,6 +222,9 @@ main(int argc, char * const *argv)
case 'f':
f_arg = optarg;
break;
case 'y':
y_arg = optarg;
break;
case 'h':
usage(EXIT_SUCCESS);
default:
......@@ -233,14 +237,15 @@ main(int argc, char * const *argv)
if (f_arg && n_arg)
usage(EXIT_FAILURE);
if (l_arg && y_arg)
usage(EXIT_FAILURE);
if (f_arg && VSL_Arg(vd, 'r', f_arg) <= 0)
exit(EXIT_FAILURE);
else if (n_arg && VSL_Arg(vd, 'n', n_arg) <= 0)
exit(EXIT_FAILURE);
if (LOG_Open(PACKAGE_NAME, l_arg) != 0) {
perror(l_arg);
if (LOG_Open(PACKAGE_NAME, l_arg, y_arg) != 0) {
exit(EXIT_FAILURE);
}
if (d_flag)
......@@ -273,13 +278,9 @@ main(int argc, char * const *argv)
/* Only read the VSL tags relevant to tracking */
assert(VSL_Arg(vd, 'i', TRACK_TAGS) > 0);
while (VSL_Dispatch(vd, OSL_Track, stdout) >= 0) {
if (fflush(stdout) != 0) {
perror("stdout");
break;
}
}
while (VSL_Dispatch(vd, OSL_Track, NULL) >= 0)
;
/* XXX: Parent removes PID */
if (pfh != NULL)
VPF_Remove(pfh);
......
......@@ -44,7 +44,7 @@ struct logconf {
int level;
} logconf;
int LOG_Open(const char *progname, const char *dest);
int LOG_Open(const char *progname, const char *dest, const char *facility);
/* XXX: __VA_ARGS__ can't be empty ... */
#define LOG_Log0(level, msg) logconf.log(level, msg)
#define LOG_Log(level, msg, ...) logconf.log(level, msg, __VA_ARGS__)
......
trackrdrd [-n varnish_name] [-l log_file] [-f varnishlog_bindump]
trackrdrd [[-n varnish_name] | [-f varnishlog_bindump]]
[[-l log_file] | [-y syslog_facility]]
[-d] [-V] [-h]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment