ctdb-common: Add config options for logging
authorMartin Schwenke <martin@meltin.net>
Fri, 15 Dec 2017 07:38:40 +0000 (18:38 +1100)
committerMartin Schwenke <martins@samba.org>
Sat, 12 May 2018 10:06:28 +0000 (12:06 +0200)
Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/common/logging_conf.c [new file with mode: 0644]
ctdb/common/logging_conf.h [new file with mode: 0644]
ctdb/wscript

diff --git a/ctdb/common/logging_conf.c b/ctdb/common/logging_conf.c
new file mode 100644 (file)
index 0000000..1cd929e
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+   CTDB logging config handling
+
+   Copyright (C) Martin Schwenke  2017
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+
+#include <talloc.h>
+
+#include "common/conf.h"
+#include "common/logging.h"
+#include "common/logging_conf.h"
+
+#define LOGGING_LOCATION_DEFAULT       "file:" LOGDIR "/log.ctdb"
+#define LOGGING_LOG_LEVEL_DEFAULT      "ERROR"
+
+static bool logging_conf_validate_log_level(const char *key,
+                                           const char *old_loglevel,
+                                           const char *new_loglevel,
+                                           enum conf_update_mode mode)
+{
+       int log_level;
+       bool ok;
+
+       ok = debug_level_parse(new_loglevel, &log_level);
+       if (!ok) {
+               return false;
+       }
+
+       return true;
+}
+
+static bool logging_conf_validate_location(const char *key,
+                                          const char *old_location,
+                                          const char *new_location,
+                                          enum conf_update_mode mode)
+{
+       bool ok;
+
+       ok = logging_validate(new_location);
+       if (!ok) {
+               return false;
+       }
+
+       if (mode == CONF_MODE_RELOAD &&
+           strcmp(old_location, new_location) != 0) {
+               D_WARNING("Ignoring update of %s config option \"%s\"\n",
+                         LOGGING_CONF_SECTION, key);
+               return false;
+       }
+
+       return true;
+}
+
+void logging_conf_init(struct conf_context *conf,
+                      const char *default_log_level)
+{
+       const char *log_level;
+
+       log_level = (default_log_level == NULL) ?
+                       LOGGING_LOG_LEVEL_DEFAULT :
+                       default_log_level;
+
+       conf_define_section(conf, LOGGING_CONF_SECTION, NULL);
+
+       conf_define_string(conf,
+                          LOGGING_CONF_SECTION,
+                          LOGGING_CONF_LOCATION,
+                          LOGGING_LOCATION_DEFAULT,
+                          logging_conf_validate_location);
+
+       conf_define_string(conf,
+                          LOGGING_CONF_SECTION,
+                          LOGGING_CONF_LOG_LEVEL,
+                          log_level,
+                          logging_conf_validate_log_level);
+}
+
+const char *logging_conf_location(struct conf_context *conf)
+{
+       const char *out = NULL;
+       int ret;
+
+       ret = conf_get_string(conf,
+                             LOGGING_CONF_SECTION,
+                             LOGGING_CONF_LOCATION,
+                             &out,
+                             NULL);
+       if (ret != 0) {
+               /* Can't really happen, but return default */
+               return LOGGING_LOCATION_DEFAULT;
+       }
+
+       return out;
+}
+
+const char *logging_conf_log_level(struct conf_context *conf)
+{
+       const char *out = NULL;
+       int ret;
+
+       ret = conf_get_string(conf,
+                             LOGGING_CONF_SECTION,
+                             LOGGING_CONF_LOG_LEVEL,
+                             &out,
+                             NULL);
+       if (ret != 0) {
+               /* Can't really happen, but return default */
+               return LOGGING_LOG_LEVEL_DEFAULT;
+       }
+
+       return out;
+}
diff --git a/ctdb/common/logging_conf.h b/ctdb/common/logging_conf.h
new file mode 100644 (file)
index 0000000..fab478d
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+   CTDB logging config handling
+
+   Copyright (C) Martin Schwenke  2017
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __LOGGING_CONF_H__
+#define __LOGGING_CONF_H__
+
+#include "common/conf.h"
+
+#define LOGGING_CONF_SECTION   "logging"
+
+#define LOGGING_CONF_LOCATION  "location"
+#define LOGGING_CONF_LOG_LEVEL "log level"
+
+void logging_conf_init(struct conf_context *conf,
+                      const char *default_log_level);
+
+const char *logging_conf_location(struct conf_context *conf);
+const char *logging_conf_log_level(struct conf_context *conf);
+
+#endif /* __LOGGING_CONF_H__ */
index 3f0371b78aadcc62fda36b4ace8bd4b4b8c52858..e90b9a02a49bc799eac94240b132800fb538dc30 100644 (file)
@@ -405,6 +405,10 @@ def build(bld):
                         deps='''samba-util sys_rw tevent-util
                                 replace talloc tevent tdb popt''')
 
+    bld.SAMBA_SUBSYSTEM('ctdb-logging-conf',
+                        source='common/logging_conf.c',
+                        deps='ctdb-util talloc')
+
     bld.SAMBA_SUBSYSTEM('ctdb-protocol',
                         source=bld.SUBDIR('protocol',
                                           '''protocol_header.c protocol_packet.c