From 68bd9ee1b5d5a09880d046aa3ba594818fdc31fb Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 1 Feb 2015 10:38:38 +0100 Subject: [PATCH] syslog plugin: Make log-level configurable. --- src/include/utils/error.h | 4 +++ src/plugins/syslog.c | 61 +++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/include/utils/error.h b/src/include/utils/error.h index ae653af..d00ee8c 100644 --- a/src/include/utils/error.h +++ b/src/include/utils/error.h @@ -62,6 +62,10 @@ enum { : ((prio) == SDB_LOG_INFO) ? "INFO" \ : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN") +#ifndef SDB_DEFAULT_LOGLEVEL +# define SDB_DEFAULT_LOGLEVEL SDB_LOG_INFO +#endif + /* * sdb_error_set_logger: * Set the logging callback to be used for logging messages. By default (or diff --git a/src/plugins/syslog.c b/src/plugins/syslog.c index 248ba8e..8bf1677 100644 --- a/src/plugins/syslog.c +++ b/src/plugins/syslog.c @@ -1,6 +1,6 @@ /* * SysDB - src/plugins/syslog.c - * Copyright (C) 2013 Sebastian 'tokkee' Harl + * Copyright (C) 2013, 2015 Sebastian 'tokkee' Harl * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,7 +29,10 @@ #include "core/plugin.h" #include "utils/error.h" +#include "liboconfig/utils.h" + #include +#include #include SDB_PLUGIN_MAGIC; @@ -51,27 +54,66 @@ SDB_PLUGIN_MAGIC; # define SDB_LOG_PRIO_TO_SYSLOG(prio) (prio) #endif +static int loglevel = SDB_DEFAULT_LOGLEVEL; + /* * plugin API */ static int -sdb_syslog_log(int prio, const char *msg, +syslog_log(int prio, const char *msg, sdb_object_t __attribute__((unused)) *user_data) { - /* XXX: make the log-level configurable */ - if (prio >= SDB_LOG_DEBUG) + if (prio > loglevel) return 0; syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg); return 0; -} /* sdb_syslog_log */ +} /* syslog_log */ static int -sdb_syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data) +syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data) { closelog(); return 0; -} /* sdb_syslog_shutdown */ +} /* syslog_shutdown */ + +static int +syslog_config(oconfig_item_t *ci) +{ + int i; + + if (! ci) { + /* reset loglevel on deconfigure */ + loglevel = SDB_DEFAULT_LOGLEVEL; + return 0; + } + + for (i = 0; i < ci->children_num; ++i) { + oconfig_item_t *child = ci->children + i; + + if (! strcasecmp(child->key, "LogLevel")) { + char *level = NULL; + if (oconfig_get_string(child, &level)) { + sdb_log(SDB_LOG_ERR, "syslog plugin: LogLevel requires " + "a single string argument\n\tUsage: Loglevel LEVEL"); + return -1; + } + loglevel = sdb_error_parse_priority(level); + if (loglevel < 0) { + loglevel = SDB_DEFAULT_LOGLEVEL; + sdb_log(SDB_LOG_ERR, + "syslog plugin: Invalid loglevel: '%s'", level); + return -1; + } + sdb_log(SDB_LOG_INFO, "syslog plugin: Log-level set to %s", + SDB_LOG_PRIO_TO_STRING(loglevel)); + } + else + sdb_log(SDB_LOG_WARNING, "syslog plugin: Ignoring unknown config " + "option '%s'.", child->key); + } + return 0; +} /* syslog_config */ int sdb_module_init(sdb_plugin_info_t *info) @@ -87,8 +129,9 @@ sdb_module_init(sdb_plugin_info_t *info) if (info) openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON); - sdb_plugin_register_log("main", sdb_syslog_log, NULL); - sdb_plugin_register_shutdown("main", sdb_syslog_shutdown, NULL); + sdb_plugin_register_log("main", syslog_log, NULL); + sdb_plugin_register_config(syslog_config); + sdb_plugin_register_shutdown("main", syslog_shutdown, NULL); return 0; } /* sdb_module_init */ -- 2.30.2