diff options
author | Martin Schwenke <martin@meltin.net> | 2014-08-11 17:07:41 +1000 |
---|---|---|
committer | Amitay Isaacs <amitay@samba.org> | 2014-10-28 05:42:04 +0100 |
commit | 1d1cd04cb9b8e66e0303c362669db2c6c47fccae (patch) | |
tree | 09244a813bbdbb0a0ad20a44c42cd5c8fe699860 /ctdb/server | |
parent | 29745543566ef66c1f2972fd5cd68e17ddf4de4f (diff) | |
download | samba-1d1cd04cb9b8e66e0303c362669db2c6c47fccae.tar.gz |
ctdb-logging: New option CTDB_LOGGING, remove CTDB_LOGFILE, CTDB_SYSLOG
Remove --logfile and --syslog daemon options and replace with
--logging.
Modularise and clean up logging initialisation code. The
initialisation API includes an app_name argument that is currently
unused - this will be used in extensions to the syslog backend.
Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
Diffstat (limited to 'ctdb/server')
-rw-r--r-- | ctdb/server/ctdb_logging.c | 67 | ||||
-rw-r--r-- | ctdb/server/ctdb_logging_file.c | 19 | ||||
-rw-r--r-- | ctdb/server/ctdb_logging_syslog.c | 9 | ||||
-rw-r--r-- | ctdb/server/ctdbd.c | 13 |
4 files changed, 78 insertions, 30 deletions
diff --git a/ctdb/server/ctdb_logging.c b/ctdb/server/ctdb_logging.c index bc47dd273fe..85dbfcfcc00 100644 --- a/ctdb/server/ctdb_logging.c +++ b/ctdb/server/ctdb_logging.c @@ -24,6 +24,13 @@ #include "system/time.h" #include "system/filesys.h" #include "lib/util/debug.h" +#include "lib/util/dlinklist.h" + +struct ctdb_log_backend { + struct ctdb_log_backend *prev, *next; + const char *prefix; + ctdb_log_setup_fn_t setup; +}; struct ctdb_log_state { const char *prefix; @@ -32,16 +39,34 @@ struct ctdb_log_state { uint16_t buf_used; void (*logfn)(const char *, uint16_t, void *); void *logfn_private; + struct ctdb_log_backend *backends; }; -/* we need this global to keep the DEBUG() syntax */ +/* Used by ctdb_set_child_logging() */ static struct ctdb_log_state *log_state; -/* - choose the logfile location -*/ -int ctdb_set_logfile(TALLOC_CTX *mem_ctx, const char *logfile, bool use_syslog) +void ctdb_log_register_backend(const char *prefix, ctdb_log_setup_fn_t setup) { + struct ctdb_log_backend *b; + + b = talloc_zero(log_state, struct ctdb_log_backend); + if (b == NULL) { + printf("Failed to register backend \"%s\" - no memory\n", + prefix); + return; + } + + b->prefix = prefix; + b->setup = setup; + + DLIST_ADD_END(log_state->backends, b, NULL); +} + + +/* Initialise logging */ +bool ctdb_logging_init(TALLOC_CTX *mem_ctx, const char *logging) +{ + struct ctdb_log_backend *b; int ret; log_state = talloc_zero(mem_ctx, struct ctdb_log_state); @@ -50,22 +75,26 @@ int ctdb_set_logfile(TALLOC_CTX *mem_ctx, const char *logfile, bool use_syslog) abort(); } - if (use_syslog) { - ret = ctdb_log_setup_syslog(); - if (ret != 0) { - printf("Setup of syslog logging failed with \"%s\"\n", - strerror(ret)); - abort(); - } - } else { - ret = ctdb_log_setup_file(mem_ctx, logfile); - if (ret != 0) { - printf("Setup of file logging failed with \"%s\"\n", - strerror(ret)); - abort(); + ctdb_log_init_file(); + ctdb_log_init_syslog(); + + for (b = log_state->backends; b != NULL; b = b->next) { + size_t l = strlen(b->prefix); + /* Exact match with prefix or prefix followed by ':' */ + if (strncmp(b->prefix, logging, l) == 0 && + (logging[l] == '\0' || logging[l] == ':')) { + ret = b->setup(mem_ctx, logging, "ctdbd"); + if (ret == 0) { + return true; + } + printf("Log init for \"%s\" failed with \"%s\"\n", + logging, strerror(ret)); + return false; } } - return 0; + + printf("Unable to find log backend for \"%s\"\n", logging); + return false; } /* Note that do_debug always uses the global log state. */ diff --git a/ctdb/server/ctdb_logging_file.c b/ctdb/server/ctdb_logging_file.c index 28b72231d9d..f931b4a8b83 100644 --- a/ctdb/server/ctdb_logging_file.c +++ b/ctdb/server/ctdb_logging_file.c @@ -24,6 +24,8 @@ #include "system/filesys.h" #include "lib/util/time_basic.h" +#define CTDB_LOG_FILE_PREFIX "file" + struct file_state { int fd; }; @@ -64,9 +66,19 @@ static int file_state_destructor(struct file_state *state) return 0; } -int ctdb_log_setup_file(TALLOC_CTX *mem_ctx, const char *logfile) +static int ctdb_log_setup_file(TALLOC_CTX *mem_ctx, + const char *logging, + const char *app_name) { struct file_state *state; + const char *logfile; + size_t l; + + l = strlen(CTDB_LOG_FILE_PREFIX); + if (logging[l] != ':') { + return EINVAL; + } + logfile = &logging[0] + l + 1; state = talloc_zero(mem_ctx, struct file_state); if (state == NULL) { @@ -94,3 +106,8 @@ int ctdb_log_setup_file(TALLOC_CTX *mem_ctx, const char *logfile) return 0; } + +void ctdb_log_init_file(void) +{ + ctdb_log_register_backend(CTDB_LOG_FILE_PREFIX, ctdb_log_setup_file); +} diff --git a/ctdb/server/ctdb_logging_syslog.c b/ctdb/server/ctdb_logging_syslog.c index 382c7916e5a..58093375f2d 100644 --- a/ctdb/server/ctdb_logging_syslog.c +++ b/ctdb/server/ctdb_logging_syslog.c @@ -54,8 +54,15 @@ static void ctdb_log_to_syslog(void *private_ptr, int dbglevel, const char *s) "%s%s", debug_extra, s); } -int ctdb_log_setup_syslog(void) +static int ctdb_log_setup_syslog(TALLOC_CTX *mem_ctx, + const char *logging, + const char *app_name) { debug_set_callback(NULL, ctdb_log_to_syslog); return 0; } + +void ctdb_log_init_syslog(void) +{ + ctdb_log_register_backend("syslog", ctdb_log_setup_syslog); +} diff --git a/ctdb/server/ctdbd.c b/ctdb/server/ctdbd.c index 16a647ac48c..ec285c043cc 100644 --- a/ctdb/server/ctdbd.c +++ b/ctdb/server/ctdbd.c @@ -33,7 +33,7 @@ static struct { const char *public_address_list; const char *event_script_dir; const char *notification_script; - const char *logfile; + const char *logging; const char *recovery_lock_file; const char *db_dir; const char *db_dir_persistent; @@ -42,7 +42,6 @@ static struct { const char *single_public_ip; int valgrinding; int nosetsched; - int use_syslog; int start_as_disabled; int start_as_stopped; int no_lmaster; @@ -56,7 +55,7 @@ static struct { .public_address_list = NULL, .transport = "tcp", .event_script_dir = NULL, - .logfile = LOGDIR "/log.ctdb", + .logging = "file:" LOGDIR "/log.ctdb", .db_dir = CTDB_VARDIR, .db_dir_persistent = CTDB_VARDIR "/persistent", .db_dir_state = CTDB_VARDIR "/state", @@ -111,7 +110,7 @@ int main(int argc, const char *argv[]) { "public-interface", 0, POPT_ARG_STRING, &options.public_interface, 0, "public interface", "interface"}, { "single-public-ip", 0, POPT_ARG_STRING, &options.single_public_ip, 0, "single public ip", "ip-address"}, { "event-script-dir", 0, POPT_ARG_STRING, &options.event_script_dir, 0, "event script directory", "dirname" }, - { "logfile", 0, POPT_ARG_STRING, &options.logfile, 0, "log file location", "filename" }, + { "logging", 0, POPT_ARG_STRING, &options.logging, 0, "logging method to be used", NULL }, { "nlist", 0, POPT_ARG_STRING, &options.nlist, 0, "node list file", "filename" }, { "notification-script", 0, POPT_ARG_STRING, &options.notification_script, 0, "notification script", "filename" }, { "listen", 0, POPT_ARG_STRING, &options.myaddress, 0, "address to listen on", "address" }, @@ -123,7 +122,6 @@ int main(int argc, const char *argv[]) { "pidfile", 0, POPT_ARG_STRING, &ctdbd_pidfile, 0, "location of PID file", "filename" }, { "valgrinding", 0, POPT_ARG_NONE, &options.valgrinding, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL }, { "nosetsched", 0, POPT_ARG_NONE, &options.nosetsched, 0, "disable setscheduler SCHED_FIFO call, use mmap for tdbs", NULL }, - { "syslog", 0, POPT_ARG_NONE, &options.use_syslog, 0, "log messages to syslog", NULL }, { "start-as-disabled", 0, POPT_ARG_NONE, &options.start_as_disabled, 0, "Node starts in disabled state", NULL }, { "start-as-stopped", 0, POPT_ARG_NONE, &options.start_as_stopped, 0, "Node starts in stopped state", NULL }, { "no-lmaster", 0, POPT_ARG_NONE, &options.no_lmaster, 0, "disable lmaster role on this node", NULL }, @@ -175,10 +173,7 @@ int main(int argc, const char *argv[]) script_log_level = options.script_log_level; - ret = ctdb_set_logfile(ctdb, options.logfile, options.use_syslog); - if (ret == -1) { - printf("ctdb_set_logfile to %s failed - %s\n", - options.use_syslog?"syslog":options.logfile, ctdb_errstr(ctdb)); + if (!ctdb_logging_init(ctdb, options.logging)) { exit(1); } |