Adding an Android specific logger.
authorTobias Brunner <tobias@strongswan.org>
Tue, 4 May 2010 16:13:27 +0000 (18:13 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 15 Jun 2010 17:58:58 +0000 (19:58 +0200)
src/libcharon/plugins/android/Makefile.am
src/libcharon/plugins/android/android_logger.c [new file with mode: 0644]
src/libcharon/plugins/android/android_logger.h [new file with mode: 0644]
src/libcharon/plugins/android/android_plugin.c

index e842358..dfac473 100644 (file)
@@ -12,7 +12,8 @@ endif
 
 libstrongswan_android_la_SOURCES = \
        android_plugin.c android_plugin.h \
-       android_handler.c android_handler.h
+       android_handler.c android_handler.h \
+       android_logger.c android_logger.h
 
 libstrongswan_android_la_LDFLAGS = -module -avoid-version
 libstrongswan_android_la_LIBADD  = -lcutils
diff --git a/src/libcharon/plugins/android/android_logger.c b/src/libcharon/plugins/android/android_logger.c
new file mode 100644 (file)
index 0000000..43c56e6
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2010 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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 <string.h>
+#include <android/log.h>
+
+#include "android_logger.h"
+
+#include <library.h>
+#include <daemon.h>
+
+typedef struct private_android_logger_t private_android_logger_t;
+
+/**
+ * Private data of an android_logger_t object
+ */
+struct private_android_logger_t {
+
+       /**
+        * Public interface
+        */
+       android_logger_t public;
+
+       /**
+        * logging level
+        */
+       int level;
+
+};
+
+
+METHOD(listener_t, log_, bool,
+          private_android_logger_t *this, debug_t group, level_t level,
+          int thread, ike_sa_t* ike_sa, char *format, va_list args)
+{
+       if (level <= this->level)
+       {
+               char sgroup[16], buffer[8192];
+               char *current = buffer, *next;
+               snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
+               vsnprintf(buffer, sizeof(buffer), format, args);
+               while (current)
+               {       /* log each line seperately */
+                       next = strchr(current, '\n');
+                       if (next)
+                       {
+                               *(next++) = '\0';
+                       }
+                       __android_log_print(ANDROID_LOG_INFO, "charon", "%.2d[%s] %s\n",
+                                                               thread, sgroup, current);
+                       current = next;
+               }
+       }
+       /* always stay registered */
+       return TRUE;
+}
+
+METHOD(android_logger_t, destroy, void,
+          private_android_logger_t *this)
+{
+       free(this);
+}
+
+/**
+ * Described in header.
+ */
+android_logger_t *android_logger_create()
+{
+       private_android_logger_t *this;
+
+       INIT(this,
+               .public = {
+                       .listener = {
+                               .log = _log_,
+                       },
+                       .destroy = _destroy,
+               },
+               .level = lib->settings->get_int(lib->settings,
+                                                                               "charon.plugins.android.loglevel", 1),
+       );
+
+       return &this->public;
+}
+
diff --git a/src/libcharon/plugins/android/android_logger.h b/src/libcharon/plugins/android/android_logger.h
new file mode 100644 (file)
index 0000000..c6fe5af
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2010 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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 android_logger android_logger
+ * @{ @ingroup android
+ */
+
+#ifndef ANDROID_LOGGER_H_
+#define ANDROID_LOGGER_H_
+
+#include <bus/bus.h>
+
+typedef struct android_logger_t android_logger_t;
+
+/**
+ * Android specific logger.
+ */
+struct android_logger_t {
+
+       /**
+        * Implements bus_listener_t interface
+        */
+       listener_t listener;
+
+       /**
+        * Destroy the logger.
+        */
+       void (*destroy)(android_logger_t *this);
+
+};
+
+/**
+ * Create an Android specific logger instance.
+ *
+ * @return                     logger instance
+ */
+android_logger_t *android_logger_create();
+
+#endif /** ANDROID_LOGGER_H_ @}*/
index 9a558f5..7f6c20f 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 #include "android_plugin.h"
+#include "android_logger.h"
 #include "android_handler.h"
 
 #include <hydra.h>
@@ -32,16 +33,24 @@ struct private_android_plugin_t {
        android_plugin_t public;
 
        /**
+        * Android specific logger
+        */
+       android_logger_t *logger;
+
+       /**
         * Android specific DNS handler
         */
        android_handler_t *handler;
 };
 
 METHOD(plugin_t, destroy, void,
-       private_android_plugin_t *this)
+          private_android_plugin_t *this)
 {
-       hydra->attributes->remove_handler(hydra->attributes, &this->handler->handler);
+       hydra->attributes->remove_handler(hydra->attributes,
+                                                                         &this->handler->handler);
+       charon->bus->remove_listener(charon->bus, &this->logger->listener);
        this->handler->destroy(this->handler);
+       this->logger->destroy(this->logger);
        free(this);
 }
 
@@ -56,9 +65,11 @@ plugin_t *android_plugin_create()
                .public.plugin = {
                        .destroy = _destroy,
                },
+               .logger = android_logger_create(),
                .handler = android_handler_create(),
        );
 
+       charon->bus->add_listener(charon->bus, &this->logger->listener);
        hydra->attributes->add_handler(hydra->attributes, &this->handler->handler);
 
        return &this->public.plugin;