Adding an Android specific logger.

This commit is contained in:
Tobias Brunner
2010-06-15 19:58:58 +02:00
parent 946be4d357
commit 51a00fb275
4 changed files with 163 additions and 3 deletions
+2 -1
View 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
@@ -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;
}
@@ -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_ @}*/
+13 -2
View File
@@ -14,6 +14,7 @@
*/
#include "android_plugin.h"
#include "android_logger.h"
#include "android_handler.h"
#include <hydra.h>
@@ -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;