X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fplugins%2Fsyslog.c;h=8bf16774370f39a4bef6c8ad08e605b602f2b4a3;hb=2618c6cbc271fcc78ddde81b7f689d366ed300d1;hp=f1c3e55fb5de3ffb71a28b9be3b9d37848a0f20a;hpb=eead68b7a05866ebbdf2f1f4d95303b2bd9bea6b;p=sysdb.git diff --git a/src/plugins/syslog.c b/src/plugins/syslog.c index f1c3e55..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,24 +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) { + 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) @@ -84,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 */