Code

syslog plugin: Make log-level configurable.
authorSebastian Harl <sh@tokkee.org>
Sun, 1 Feb 2015 09:38:38 +0000 (10:38 +0100)
committerSebastian Harl <sh@tokkee.org>
Sun, 1 Feb 2015 14:00:36 +0000 (15:00 +0100)
src/include/utils/error.h
src/plugins/syslog.c

index ae653af2ce6250ea75a039def23b3fac1d6b7ddd..d00ee8c0f980b5abf8590b99f073a3344d0cdb1b 100644 (file)
@@ -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
index 248ba8ef5c1d208c9a3c0292cd95e4bfdae7ba01..8bf16774370f39a4bef6c8ad08e605b602f2b4a3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SysDB - src/plugins/syslog.c
- * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2013, 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "core/plugin.h"
 #include "utils/error.h"
 
+#include "liboconfig/utils.h"
+
 #include <assert.h>
+#include <strings.h>
 #include <syslog.h>
 
 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 */