vici: Add a generic log event to raise events for log messages
authorMartin Willi <martin@revosec.ch>
Thu, 6 Mar 2014 09:44:55 +0000 (10:44 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 7 May 2014 12:13:37 +0000 (14:13 +0200)
src/libcharon/plugins/vici/Makefile.am
src/libcharon/plugins/vici/vici_logger.c [new file with mode: 0644]
src/libcharon/plugins/vici/vici_logger.h [new file with mode: 0644]
src/libcharon/plugins/vici/vici_plugin.c

index 7c6d144..370f8c5 100644 (file)
@@ -22,6 +22,7 @@ libstrongswan_vici_la_SOURCES = \
        vici_control.h vici_control.c \
        vici_config.h vici_config.c \
        vici_cred.h vici_cred.c \
+       vici_logger.h vici_logger.c \
        vici_plugin.h vici_plugin.c
 
 libstrongswan_vici_la_LDFLAGS = -module -avoid-version
diff --git a/src/libcharon/plugins/vici/vici_logger.c b/src/libcharon/plugins/vici/vici_logger.c
new file mode 100644 (file)
index 0000000..cffd65b
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2014 Martin Willi
+ * Copyright (C) 2014 revosec AG
+ *
+ * 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 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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.
+ */
+
+#include "vici_logger.h"
+#include "vici_builder.h"
+
+#include <daemon.h>
+#include <threading/mutex.h>
+
+typedef struct private_vici_logger_t private_vici_logger_t;
+
+/**
+ * Private data of an vici_logger_t object.
+ */
+struct private_vici_logger_t {
+
+       /**
+        * Public vici_logger_t interface.
+        */
+       vici_logger_t public;
+
+       /**
+        * Dispatcher
+        */
+       vici_dispatcher_t *dispatcher;
+
+       /**
+        * Recursiveness avoidance counter
+        */
+       int recursive;
+
+       /**
+        * Mutex to synchronize logging
+        */
+       mutex_t *mutex;
+};
+
+METHOD(logger_t, log_, void,
+       private_vici_logger_t *this, debug_t group, level_t level, int thread,
+       ike_sa_t* ike_sa, const char *msg)
+{
+       this->mutex->lock(this->mutex);
+
+       /* avoid recursive invocations by the vici subsystem */
+       if (this->recursive++ == 0)
+       {
+               vici_message_t *message;
+               vici_builder_t *builder;
+
+               builder = vici_builder_create();
+               builder->add_kv(builder, "group", "%N", debug_names, group);
+               builder->add_kv(builder, "level", "%d", level);
+               builder->add_kv(builder, "thread", "%d", thread);
+               if (ike_sa)
+               {
+                       builder->add_kv(builder, "ikesa-name", "%s",
+                                                       ike_sa->get_name(ike_sa));
+                       builder->add_kv(builder, "ikesa-uniqueid", "%u",
+                                                       ike_sa->get_unique_id(ike_sa));
+               }
+               builder->add_kv(builder, "msg", "%s", msg);
+
+               message = builder->finalize(builder);
+               if (message)
+               {
+                       this->dispatcher->raise_event(this->dispatcher, "log", 0, message);
+               }
+       }
+       this->recursive--;
+
+       this->mutex->unlock(this->mutex);
+}
+
+METHOD(logger_t, get_level, level_t,
+       private_vici_logger_t *this, debug_t group)
+{
+       return LEVEL_CTRL;
+}
+
+/**
+ * (Un-)register dispatcher functions/events
+ */
+static void manage_commands(private_vici_logger_t *this, bool reg)
+{
+       this->dispatcher->manage_event(this->dispatcher, "log", reg);
+}
+
+METHOD(vici_logger_t, destroy, void,
+       private_vici_logger_t *this)
+{
+       manage_commands(this, FALSE);
+       this->mutex->destroy(this->mutex);
+       free(this);
+}
+
+/**
+ * See header
+ */
+vici_logger_t *vici_logger_create(vici_dispatcher_t *dispatcher)
+{
+       private_vici_logger_t *this;
+
+       INIT(this,
+               .public = {
+                       .logger = {
+                               .log = _log_,
+                               .get_level = _get_level,
+                       },
+                       .destroy = _destroy,
+               },
+               .dispatcher = dispatcher,
+               .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
+       );
+
+       manage_commands(this, TRUE);
+
+       return &this->public;
+}
diff --git a/src/libcharon/plugins/vici/vici_logger.h b/src/libcharon/plugins/vici/vici_logger.h
new file mode 100644 (file)
index 0000000..7be1d60
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2014 Martin Willi
+ * Copyright (C) 2014 revosec AG
+ *
+ * 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 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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.
+ */
+
+/**
+ * @defgroup vici_logger vici_logger
+ * @{ @ingroup vici
+ */
+
+#ifndef VICI_LOGGER_H_
+#define VICI_LOGGER_H_
+
+#include "vici_dispatcher.h"
+
+#include <bus/listeners/logger.h>
+
+typedef struct vici_logger_t vici_logger_t;
+
+/**
+ * Generic debugging logger over vici.
+ */
+struct vici_logger_t {
+
+       /**
+        * Implements logger interface.
+        */
+       logger_t logger;
+
+       /**
+        * Destroy a vici_logger_t.
+        */
+       void (*destroy)(vici_logger_t *this);
+};
+
+/**
+ * Create a vici_logger instance.
+ *
+ * @param dispatcher           dispatcher to receive requests from
+ * @return                                     loggerential backend
+ */
+vici_logger_t *vici_logger_create(vici_dispatcher_t *dispatcher);
+
+#endif /** VICI_LOGGER_H_ @}*/
index 30e676c..fcd95ed 100644 (file)
@@ -19,6 +19,7 @@
 #include "vici_control.h"
 #include "vici_cred.h"
 #include "vici_config.h"
+#include "vici_logger.h"
 
 #include <library.h>
 #include <daemon.h>
@@ -59,6 +60,11 @@ struct private_vici_plugin_t {
         * Configuration backend
         */
        vici_config_t *config;
+
+       /**
+        * Generic debug logger
+        */
+       vici_logger_t *logger;
 };
 
 METHOD(plugin_t, get_name, char*,
@@ -86,18 +92,22 @@ static bool register_vici(private_vici_plugin_t *this,
                        this->control = vici_control_create(this->dispatcher);
                        this->cred = vici_cred_create(this->dispatcher);
                        this->config = vici_config_create(this->dispatcher);
+                       this->logger = vici_logger_create(this->dispatcher);
 
                        charon->backends->add_backend(charon->backends,
                                                                                  &this->config->backend);
+                       charon->bus->add_logger(charon->bus, &this->logger->logger);
                        return TRUE;
                }
                return FALSE;
        }
        else
        {
+               charon->bus->remove_logger(charon->bus, &this->logger->logger);
                charon->backends->remove_backend(charon->backends,
                                                                                 &this->config->backend);
 
+               this->logger->destroy(this->logger);
                this->config->destroy(this->config);
                this->cred->destroy(this->cred);
                this->control->destroy(this->control);