X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fplugins%2Fsyslog.c;h=8bf16774370f39a4bef6c8ad08e605b602f2b4a3;hp=b548988a82835be9aedac97099aeed29d7d3caf2;hb=1f99d1a0d8a7e923dede7c938340c26061b8398b;hpb=6138090532ecfc07b00b10287f835dfe5a0bef3b diff --git a/src/plugins/syslog.c b/src/plugins/syslog.c index b548988..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 @@ -27,9 +27,12 @@ #include "sysdb.h" #include "core/plugin.h" -#include "core/error.h" +#include "utils/error.h" + +#include "liboconfig/utils.h" #include +#include #include SDB_PLUGIN_MAGIC; @@ -51,36 +54,70 @@ SDB_PLUGIN_MAGIC; # define SDB_LOG_PRIO_TO_SYSLOG(prio) (prio) #endif +static int loglevel = SDB_DEFAULT_LOGLEVEL; + /* * plugin API */ static int -sdb_syslog_init(sdb_object_t __attribute__((unused)) *user_data) +syslog_log(int prio, const char *msg, + sdb_object_t __attribute__((unused)) *user_data) { - openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON); + if (prio > loglevel) + return 0; + syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg); return 0; -} /* sdb_syslog_init */ +} /* syslog_log */ static int -sdb_syslog_log(int prio, const char *msg, - sdb_object_t __attribute__((unused)) *user_data) +syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data) { - syslog(SDB_LOG_PRIO_TO_SYSLOG(prio), "%s", msg); + closelog(); return 0; -} /* sdb_syslog_log */ +} /* syslog_shutdown */ static int -sdb_syslog_shutdown(sdb_object_t __attribute__((unused)) *user_data) +syslog_config(oconfig_item_t *ci) { - closelog(); + 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; -} /* sdb_syslog_shutdown */ +} /* syslog_config */ int sdb_module_init(sdb_plugin_info_t *info) { - sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "syslog"); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "log messages to the system logger"); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT, @@ -89,9 +126,12 @@ sdb_module_init(sdb_plugin_info_t *info) sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); - sdb_plugin_register_init("syslog", sdb_syslog_init, NULL); - sdb_plugin_register_log("syslog", sdb_syslog_log, NULL); - sdb_plugin_register_shutdown("syslog", sdb_syslog_shutdown, NULL); + if (info) + openlog("sysdbd", LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_DAEMON); + + 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 */