added configurable logging facility / ident

This commit is contained in:
Axel Kittenberger 2011-03-01 14:57:26 +00:00
parent 08ede998ba
commit 26f0583dca
3 changed files with 88 additions and 4 deletions

View File

@ -14,6 +14,8 @@
#include "lsyncd.h" #include "lsyncd.h"
#define SYSLOG_NAMES 1
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/times.h> #include <sys/times.h>
#include <sys/types.h> #include <sys/types.h>
@ -79,10 +81,44 @@ static char *monitors[] = {
struct settings settings = { struct settings settings = {
.log_file = NULL, .log_file = NULL,
.log_syslog = false, .log_syslog = false,
.log_ident = NULL,
.log_facility = LOG_USER,
.log_level = 0, .log_level = 0,
.nodaemon = false, .nodaemon = false,
}; };
/**
* configurable names for logging facility
* to be translated to integer value.
*/
//struct {
// const char * c_name;
// int c_val;
//} facilitynames[] = {
// { "auth", LOG_AUTH },
// { "authprive", LOG_AUTHPRIV },
// { "cron", LOG_CRON },
// { "daemon", LOG_DAEMON },
// { "ftp", LOG_FTP },
// { "kern", LOG_KERN },
// { "lpr", LOG_LPR },
// { "mail", LOG_MAIL },
// { "news", LOG_NEWS },
// { "syslog", LOG_SYSLOG },
// { "user", LOG_USER },
// { "uucp", LOG_UUCP },
// { "local0", LOG_LOCAL0 },
// { "local1", LOG_LOCAL1 },
// { "local2", LOG_LOCAL2 },
// { "local3", LOG_LOCAL3 },
// { "local4", LOG_LOCAL4 },
// { "local5", LOG_LOCAL5 },
// { "local6", LOG_LOCAL6 },
// { "local7", LOG_LOCAL7 },
// { NULL, -1 },
//};
/** /**
* True when lsyncd daemonized itself. * True when lsyncd daemonized itself.
*/ */
@ -1128,6 +1164,12 @@ l_configure(lua_State *L)
* from this on log to configurated log end instead of * from this on log to configurated log end instead of
* stdout/stderr */ * stdout/stderr */
running = true; running = true;
if (settings.log_syslog) {
openlog(settings.log_ident ? settings.log_ident : "lsyncd",
0,
settings.log_facility
);
}
if (!settings.nodaemon && !is_daemon) { if (!settings.nodaemon && !is_daemon) {
if (!settings.log_file) { if (!settings.log_file) {
settings.log_syslog = true; settings.log_syslog = true;
@ -1152,6 +1194,32 @@ l_configure(lua_State *L)
free(settings.pidfile); free(settings.pidfile);
} }
settings.pidfile = s_strdup(file); settings.pidfile = s_strdup(file);
} else if (!strcmp(command, "logfacility")) {
if (lua_isstring(L, 2)) {
const char * fname = luaL_checkstring(L, 2);
int i;
for(i = 0; facilitynames[i].c_name; i++) {
if (!strcasecmp(fname, facilitynames[i].c_name)) {
break;
}
}
if (!facilitynames[i].c_name) {
printlogf(L, "Error", "Logging facility '%s' unknown.", fname);
exit(-1); //ERRNO
}
settings.log_facility = facilitynames[i].c_val;
} else if (lua_isnumber(L, 2)) {
settings.log_facility = luaL_checknumber(L, 2);
} else {
printlogf(L, "Error", "Logging facility must be a number or string");
exit(-1); // ERRNO;
}
} else if (!strcmp(command, "logident")) {
const char * ident = luaL_checkstring(L, 2);
if (settings.log_ident) {
free(settings.log_ident);
}
settings.log_ident = s_strdup(ident);
} else { } else {
printlogf(L, "Error", printlogf(L, "Error",
"Internal error, unknown parameter in l_configure(%s)", "Internal error, unknown parameter in l_configure(%s)",
@ -1967,10 +2035,14 @@ main1(int argc, char *argv[])
free(settings.log_file); free(settings.log_file);
settings.log_file = NULL; settings.log_file = NULL;
} }
settings.log_syslog = false, settings.log_syslog = false;
settings.log_level = 0, if (settings.log_ident) {
settings.nodaemon = false, free(settings.log_ident);
settings.log_ident = NULL;
}
settings.log_facility = LOG_USER;
settings.log_level = 0;
settings.nodaemon = false;
lua_close(L); lua_close(L);
return 0; return 0;
} }

View File

@ -31,6 +31,12 @@ extern struct settings {
/* If true Lsyncd sends log messages to syslog */ /* If true Lsyncd sends log messages to syslog */
bool log_syslog; bool log_syslog;
/* If not NULL the syslog identity (otherwise "Lsyncd") */
char * log_ident;
/* The syslog facility */
int log_facility;
/* -1 logs everything, 0 normal mode, LOG_ERROR errors only.*/ /* -1 logs everything, 0 normal mode, LOG_ERROR errors only.*/
int log_level; int log_level;

View File

@ -2772,6 +2772,12 @@ function runner.initialize()
if settings.logfile then if settings.logfile then
lsyncd.configure("logfile", settings.logfile) lsyncd.configure("logfile", settings.logfile)
end end
if settings.logident then
lsyncd.condigure("logident", settings.logident)
end
if settings.logfacility then
lsyncd.configure("logfacility", settings.logfacility)
end
if settings.pidfile then if settings.pidfile then
lsyncd.configure("pidfile", settings.pidfile) lsyncd.configure("pidfile", settings.pidfile)
end end