From 51a00fb275d846c63402f84ffd47d899a59deaef Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 4 May 2010 18:13:27 +0200 Subject: [PATCH] Adding an Android specific logger. --- src/libcharon/plugins/android/Makefile.am | 3 +- .../plugins/android/android_logger.c | 96 +++++++++++++++++++ .../plugins/android/android_logger.h | 52 ++++++++++ .../plugins/android/android_plugin.c | 15 ++- 4 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 src/libcharon/plugins/android/android_logger.c create mode 100644 src/libcharon/plugins/android/android_logger.h diff --git a/src/libcharon/plugins/android/Makefile.am b/src/libcharon/plugins/android/Makefile.am index e8423589cc..dfac4738f4 100644 --- a/src/libcharon/plugins/android/Makefile.am +++ b/src/libcharon/plugins/android/Makefile.am @@ -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 index 0000000000..43c56e6563 --- /dev/null +++ b/src/libcharon/plugins/android/android_logger.c @@ -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 . + * + * 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 +#include + +#include "android_logger.h" + +#include +#include + +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 index 0000000000..c6fe5aff3a --- /dev/null +++ b/src/libcharon/plugins/android/android_logger.h @@ -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 . + * + * 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 + +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_ @}*/ diff --git a/src/libcharon/plugins/android/android_plugin.c b/src/libcharon/plugins/android/android_plugin.c index 9a558f53b1..7f6c20fa27 100644 --- a/src/libcharon/plugins/android/android_plugin.c +++ b/src/libcharon/plugins/android/android_plugin.c @@ -14,6 +14,7 @@ */ #include "android_plugin.h" +#include "android_logger.h" #include "android_handler.h" #include @@ -31,6 +32,11 @@ struct private_android_plugin_t { */ android_plugin_t public; + /** + * Android specific logger + */ + android_logger_t *logger; + /** * Android specific DNS handler */ @@ -38,10 +44,13 @@ struct private_android_plugin_t { }; 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; -- 2.47.2