introduced printf() specifiers for:
host_t (%H) identification_t (%D) chunk pointers (%B) memory pointer/length (%b) added a signaling bus: receives event and debug messages, sends them to its listeners stream_logger, sys_logger, file_logger added, listen to bus some other tweaks here and there
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* @file bus.c
|
||||
*
|
||||
* @brief Implementation of bus_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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 "bus.h"
|
||||
|
||||
typedef struct private_bus_t private_bus_t;
|
||||
|
||||
/**
|
||||
* Private data of a bus_t object.
|
||||
*/
|
||||
struct private_bus_t {
|
||||
/**
|
||||
* Public part of a bus_t object.
|
||||
*/
|
||||
bus_t public;
|
||||
|
||||
/**
|
||||
* List of registered listeners implementing the bus_t interface
|
||||
*/
|
||||
linked_list_t *listeners;
|
||||
|
||||
/**
|
||||
* Thread local storage for a unique, simple thread ID
|
||||
*/
|
||||
pthread_key_t thread_id;
|
||||
|
||||
/**
|
||||
* Thread local storage the threads IKE_SA
|
||||
*/
|
||||
pthread_key_t thread_sa;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a unique thread number for a calling thread. Since
|
||||
* pthread_self returns large and ugly numbers, use this function
|
||||
* for logging; these numbers are incremental starting at 1
|
||||
*/
|
||||
static int get_thread_number(private_bus_t *this)
|
||||
{
|
||||
static int current_num = 0, stored_num;
|
||||
|
||||
stored_num = (int)pthread_getspecific(this->thread_id);
|
||||
if (stored_num == 0)
|
||||
{ /* first call of current thread */
|
||||
pthread_setspecific(this->thread_id, (void*)++current_num);
|
||||
return current_num;
|
||||
}
|
||||
else
|
||||
{
|
||||
return stored_num;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of bus_t.add_listener.
|
||||
*/
|
||||
static void add_listener(private_bus_t *this, bus_listener_t *listener)
|
||||
{
|
||||
this->listeners->insert_last(this->listeners, (void*)listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of bus_t.set_sa.
|
||||
*/
|
||||
static void set_sa(private_bus_t *this, ike_sa_t *ike_sa)
|
||||
{
|
||||
pthread_setspecific(this->thread_sa, ike_sa);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of bus_t.signal.
|
||||
*/
|
||||
static void signal_(private_bus_t *this, signal_t signal, level_t condition,
|
||||
char* format, ...)
|
||||
{
|
||||
iterator_t *iterator;
|
||||
bus_listener_t *listener;
|
||||
va_list args;
|
||||
ike_sa_t *ike_sa;
|
||||
int thread;
|
||||
|
||||
ike_sa = pthread_getspecific(this->thread_sa);
|
||||
thread = get_thread_number(this);
|
||||
va_start(args, format);
|
||||
|
||||
iterator = this->listeners->create_iterator(this->listeners, TRUE);
|
||||
while (iterator->iterate(iterator, (void**)&listener))
|
||||
{
|
||||
listener->signal(listener, thread, ike_sa,
|
||||
signal, condition, format, args);
|
||||
}
|
||||
iterator->destroy(iterator);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of bus_t.destroy.
|
||||
*/
|
||||
static void destroy(private_bus_t *this)
|
||||
{
|
||||
this->listeners->destroy(this->listeners);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
bus_t *bus_create()
|
||||
{
|
||||
private_bus_t *this = malloc_thing(private_bus_t);
|
||||
|
||||
this->public.add_listener = (void(*)(bus_t*,bus_listener_t*))add_listener;
|
||||
this->public.set_sa = (void(*)(bus_t*,ike_sa_t*))set_sa;
|
||||
this->public.signal = (void(*)(bus_t*,signal_t,level_t,char*,...))signal_;
|
||||
this->public.destroy = (void(*)(bus_t*)) destroy;
|
||||
|
||||
this->listeners = linked_list_create();
|
||||
pthread_key_create(&this->thread_id, NULL);
|
||||
pthread_key_create(&this->thread_sa, NULL);
|
||||
|
||||
return &(this->public);
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
/**
|
||||
* @file bus.h
|
||||
*
|
||||
* @brief Interface of bus_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef BUS_H_
|
||||
#define BUS_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <sa/ike_sa.h>
|
||||
#include <sa/child_sa.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief Raise a signal for an occured event.
|
||||
*
|
||||
* @param sig signal_t signal description
|
||||
* @param level level for the signal
|
||||
* @param format printf() style format string
|
||||
* @param ... printf() style agument list
|
||||
*/
|
||||
#define SIG(sig, level, format, ...) charon->bus->signal(charon->bus, sig, level, format, ##__VA_ARGS__)
|
||||
|
||||
/**
|
||||
* @brief Set the IKE_SA the calling thread is using.
|
||||
*
|
||||
* @param ike_sa ike_sa to register, or NULL to unregister
|
||||
*/
|
||||
#define SIG_SA(ike_sa) charon->bus->set_sa(charon->bus, ike_sa)
|
||||
|
||||
/**
|
||||
* @brief Log a debug message via the signal bus.
|
||||
*
|
||||
* @param signal signal_t signal description
|
||||
* @param format printf() style format string
|
||||
* @param ... printf() style agument list
|
||||
*/
|
||||
#define DBG1(sig, format, ...) charon->bus->signal(charon->bus, sig, LEV_DBG1, format, ##__VA_ARGS__)
|
||||
#define DBG2(sig, format, ...) charon->bus->signal(charon->bus, sig, LEV_DBG2, format, ##__VA_ARGS__)
|
||||
#define DBG3(sig, format, ...) charon->bus->signal(charon->bus, sig, LEV_DBG3, format, ##__VA_ARGS__)
|
||||
#define DBG4(sig, format, ...) charon->bus->signal(charon->bus, sig, LEV_DBG4, format, ##__VA_ARGS__)
|
||||
|
||||
|
||||
typedef enum signal_t signal_t;
|
||||
|
||||
enum signal_t {
|
||||
/** an IKE_SA has been established */
|
||||
SIG_IKE_UP,
|
||||
/** an IKE_SA has been closed */
|
||||
SIG_IKE_DOWN,
|
||||
/** an IKE_SA has been rekeyed */
|
||||
SIG_IKE_REKEY,
|
||||
/** a CHILD_SA has been installed */
|
||||
SIG_CHILD_UP,
|
||||
/** a CHILD_SA has been closed */
|
||||
SIG_CHILD_DOWN,
|
||||
/** a CHILD_SA has been rekeyed */
|
||||
SIG_CHILD_REKEY,
|
||||
/** a CHILD_SA has been routed */
|
||||
SIG_CHILD_ROUTE,
|
||||
/** a CHILD_SA has been unrouted */
|
||||
SIG_CHILD_UNROUTE,
|
||||
/** a remote peer has been authenticated using RSA digital signature */
|
||||
SIG_AUTH_RSA,
|
||||
/** a remote peer has been authenticated using preshared keys */
|
||||
SIG_AUTH_PSK,
|
||||
|
||||
/** debugging message printed from an IKE_SA */
|
||||
SIG_DBG_IKE,
|
||||
/** debugging message printed from a CHILD_SA */
|
||||
SIG_DBG_CHD,
|
||||
/** debugging message printed from job processing */
|
||||
SIG_DBG_JOB,
|
||||
/** debugging message printed from configuration backends */
|
||||
SIG_DBG_CFG,
|
||||
/** debugging message printed from kernel interface */
|
||||
SIG_DBG_KNL,
|
||||
/** debugging message printed from networking */
|
||||
SIG_DBG_NET,
|
||||
/** debugging message printed from message encoding/decoding */
|
||||
SIG_DBG_ENC,
|
||||
|
||||
SIG_MAX,
|
||||
};
|
||||
|
||||
typedef enum level_t level_t;
|
||||
|
||||
enum level_t {
|
||||
/** Signal indicates something has failed */
|
||||
LEV_FAILED,
|
||||
/** Signal indicates something was successful */
|
||||
LEV_SUCCESS,
|
||||
/** Debug level 1, control flow messages */
|
||||
LEV_DBG1,
|
||||
/** Debug level 2, more detail informational messages */
|
||||
LEV_DBG2,
|
||||
/** Debug level 3, RAW data output */
|
||||
LEV_DBG3,
|
||||
/** Debug level 4, RAW data with sensitive (private) data */
|
||||
LEV_DBG4,
|
||||
};
|
||||
|
||||
typedef struct bus_listener_t bus_listener_t;
|
||||
|
||||
/**
|
||||
* @brief Interface for registering at the signal bus.
|
||||
*
|
||||
* To receive signals from the bus, the client implementing the
|
||||
* bus_listener_t interface registers itself at the signal bus.
|
||||
*
|
||||
* @ingroup bus
|
||||
*/
|
||||
struct bus_listener_t {
|
||||
|
||||
/**
|
||||
* @brief Send a signal to a bus listener.
|
||||
*
|
||||
* A numerical identification for the thread is included, as the
|
||||
* associated IKE_SA, if any. Signal specifies the type of
|
||||
* the event occured, with a verbosity level. The format string specifies
|
||||
* an additional informational or error message with a printf() like
|
||||
* variable argument list. This is in the va_list form, as forwarding
|
||||
* a "..." parameters to functions is not (cleanly) possible.
|
||||
*
|
||||
* @param this listener
|
||||
* @param thread ID of the thread raised this signal
|
||||
* @param ike_sa IKE_SA associated to the event
|
||||
* @param singal kind of the signal (up, down, rekeyed, ...)
|
||||
* @param level level for signal
|
||||
* @param format printf() style format string
|
||||
* @param args vprintf() style va_list argument list
|
||||
*/
|
||||
void (*signal) (bus_listener_t *this, int thread, ike_sa_t *ike_sa,
|
||||
signal_t signal, level_t level, char* format, va_list args);
|
||||
};
|
||||
|
||||
|
||||
typedef struct bus_t bus_t;
|
||||
|
||||
/**
|
||||
* @brief Signal bus which sends signals to registered listeners.
|
||||
*
|
||||
* The signal bus is not much more than a multiplexer. A listener interested
|
||||
* in receiving event signals registers at the bus. Any signals sent to
|
||||
* are delivered to all registered listeners.
|
||||
*
|
||||
*
|
||||
* @ingroup bus
|
||||
*/
|
||||
struct bus_t {
|
||||
|
||||
/**
|
||||
* @brief Register a listener to the bus.
|
||||
*
|
||||
* A registered listener receives all signals which are sent to the bus.
|
||||
*
|
||||
* @param this bus
|
||||
* @param listener listener to register.
|
||||
*/
|
||||
void (*add_listener) (bus_t *this, bus_listener_t *listener);
|
||||
|
||||
/**
|
||||
* @brief Set the IKE_SA the calling thread is using.
|
||||
*
|
||||
* To associate an received signal to an IKE_SA without passing it as
|
||||
* parameter each time, the thread registers it's used IKE_SA each
|
||||
* time it checked it out. Before checking it in, the thread unregisters
|
||||
* the IKE_SA (by passing NULL). This IKE_SA is stored per-thread, so each
|
||||
* thread has one IKE_SA registered (or not).
|
||||
* There is a macro to simplify the call.
|
||||
* @see SIG_SA()
|
||||
*
|
||||
* @param this bus
|
||||
* @param ike_sa ike_sa to register, or NULL to unregister
|
||||
*/
|
||||
void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
|
||||
|
||||
/**
|
||||
* @brief Send a signal to the bus.
|
||||
*
|
||||
* A signal may belong to an IKE_SA and a CHILD_SA. If so, these
|
||||
* are supplied to the signal function. The signal specifies the type of
|
||||
* the event occured. The format string specifies an additional
|
||||
* informational or error message with a printf() like variable argument
|
||||
* list.
|
||||
* Some useful macros may be available to shorten this call.
|
||||
* @see SIG(), DBG1()
|
||||
*
|
||||
* @param this bus
|
||||
* @param singal kind of the signal (up, down, rekeyed, ...)
|
||||
* @param level status level of the signal to send
|
||||
* @param format printf() style format string
|
||||
* @param ... printf() style argument list
|
||||
*/
|
||||
void (*signal) (bus_t *this, signal_t signal, level_t level, char* format, ...);
|
||||
|
||||
/**
|
||||
* @brief Destroy the signal bus.
|
||||
*
|
||||
* @param this bus to destroy
|
||||
*/
|
||||
void (*destroy) (bus_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Create the signal bus which multiplexes signals to its listeners.
|
||||
*
|
||||
* @return signal bus instance
|
||||
*
|
||||
* @ingroup bus
|
||||
*/
|
||||
bus_t *bus_create();
|
||||
|
||||
#endif /* BUS_H_ */
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* @file file_logger.c
|
||||
*
|
||||
* @brief Implementation of file_logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* for fmemopen() */
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "file_logger.h"
|
||||
|
||||
#include <bus/listeners/stream_logger.h>
|
||||
|
||||
|
||||
typedef struct private_file_logger_t private_file_logger_t;
|
||||
|
||||
/**
|
||||
* Private data of a file_logger_t object
|
||||
*/
|
||||
struct private_file_logger_t {
|
||||
|
||||
/**
|
||||
* Public data.
|
||||
*/
|
||||
file_logger_t public;
|
||||
|
||||
/**
|
||||
* output file
|
||||
*/
|
||||
FILE *out;
|
||||
|
||||
/**
|
||||
* Internal used stream logger that does the dirty work
|
||||
*/
|
||||
stream_logger_t *logger;
|
||||
|
||||
/**
|
||||
* Memory stream used for stream_logger
|
||||
*/
|
||||
FILE *stream;
|
||||
|
||||
/**
|
||||
* Underlying buffer for stream
|
||||
*/
|
||||
char buffer[4096];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of bus_listener_t.signal.
|
||||
*/
|
||||
static void signal_(private_file_logger_t *this, int thread, ike_sa_t* ike_sa,
|
||||
signal_t signal, level_t level,
|
||||
char *format, va_list args)
|
||||
{
|
||||
char line[512];
|
||||
char *prefix;
|
||||
FILE *reader;
|
||||
|
||||
switch (signal)
|
||||
{
|
||||
case SIG_IKE_UP:
|
||||
case SIG_IKE_DOWN:
|
||||
case SIG_IKE_REKEY:
|
||||
case SIG_DBG_IKE:
|
||||
prefix = "IKE";
|
||||
break;
|
||||
case SIG_DBG_CHD:
|
||||
prefix = "CHD";
|
||||
break;
|
||||
case SIG_DBG_JOB:
|
||||
prefix = "JOG";
|
||||
break;
|
||||
case SIG_DBG_CFG:
|
||||
prefix = "CFG";
|
||||
break;
|
||||
case SIG_DBG_KNL:
|
||||
prefix = "KNL";
|
||||
break;
|
||||
case SIG_DBG_NET:
|
||||
prefix = "NET";
|
||||
break;
|
||||
case SIG_DBG_ENC:
|
||||
prefix = "ENC";
|
||||
break;
|
||||
default:
|
||||
prefix = "???";
|
||||
break;
|
||||
}
|
||||
|
||||
flockfile(this->stream);
|
||||
/* reset memory stream */
|
||||
rewind(this->stream);
|
||||
memset(this->buffer, '\0', sizeof(this->buffer));
|
||||
/* log to memstream */
|
||||
this->logger->listener.signal(&this->logger->listener, thread, ike_sa,
|
||||
signal, level, format, args);
|
||||
/* flush is needed to append a '\0' */
|
||||
fflush(this->stream);
|
||||
|
||||
/* create a reader stream that reads out line by line */
|
||||
reader = fmemopen(this->buffer, sizeof(this->buffer), "r");
|
||||
|
||||
while (fgets(line, sizeof(line), reader))
|
||||
{
|
||||
if (line[0] == '\0')
|
||||
{
|
||||
/* abort on EOF */
|
||||
break;
|
||||
}
|
||||
else if (line[0] != '\n')
|
||||
{
|
||||
fprintf(this->out, "%.2d[%s] %s", thread, prefix, line);
|
||||
}
|
||||
}
|
||||
fclose(reader);
|
||||
funlockfile(this->stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of file_logger_t.set_level.
|
||||
*/
|
||||
static void set_level(private_file_logger_t *this, signal_t signal, level_t max)
|
||||
{
|
||||
this->logger->set_level(this->logger, signal, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of file_logger_t.destroy.
|
||||
*/
|
||||
static void destroy(private_file_logger_t *this)
|
||||
{
|
||||
fclose(this->stream);
|
||||
this->logger->destroy(this->logger);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
file_logger_t *file_logger_create(FILE *out)
|
||||
{
|
||||
private_file_logger_t *this = malloc_thing(private_file_logger_t);
|
||||
|
||||
/* public functions */
|
||||
this->public.listener.signal = (void(*)(bus_listener_t*,int,ike_sa_t*,signal_t,level_t,char*,va_list))signal_;
|
||||
this->public.set_level = (void(*)(file_logger_t*,signal_t,level_t))set_level;
|
||||
this->public.destroy = (void(*)(file_logger_t*))destroy;
|
||||
|
||||
/* private variables */
|
||||
this->out = out;
|
||||
this->stream = fmemopen(this->buffer, sizeof(this->buffer), "w");
|
||||
if (this->stream == NULL)
|
||||
{
|
||||
/* fallback to stderr */
|
||||
this->stream = stderr;
|
||||
}
|
||||
this->logger = stream_logger_create(this->stream);
|
||||
|
||||
return &this->public;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* @file file_logger.h
|
||||
*
|
||||
* @brief Interface of file_logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FILE_LOGGER_H_
|
||||
#define FILE_LOGGER_H_
|
||||
|
||||
#include <bus/bus.h>
|
||||
|
||||
|
||||
typedef struct file_logger_t file_logger_t;
|
||||
|
||||
/**
|
||||
* @brief Logger to files which implements bus_listener_t.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - file_logger_create()
|
||||
*
|
||||
* @ingroup listeners
|
||||
*/
|
||||
struct file_logger_t {
|
||||
|
||||
/**
|
||||
* Implements the bus_listener_t interface.
|
||||
*/
|
||||
bus_listener_t listener;
|
||||
|
||||
/**
|
||||
* @brief Set the loglevel for a signal type.
|
||||
*
|
||||
* @param this stream_logger_t object
|
||||
* @param singal type of signal
|
||||
* @param level max level to log
|
||||
*/
|
||||
void (*set_level) (file_logger_t *this, signal_t signal, level_t level);
|
||||
|
||||
/**
|
||||
* @brief Destroys a file_logger_t object.
|
||||
*
|
||||
* @param this file_logger_t object
|
||||
*/
|
||||
void (*destroy) (file_logger_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a file_logger_t object.
|
||||
*
|
||||
* @param out FILE to write to
|
||||
* @return file_logger_t object
|
||||
*
|
||||
* @ingroup listeners
|
||||
*/
|
||||
file_logger_t *file_logger_create(FILE *out);
|
||||
|
||||
|
||||
#endif /* FILE_LOGGER_H_ */
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* @file stream_logger.c
|
||||
*
|
||||
* @brief Implementation of stream_logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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 <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "stream_logger.h"
|
||||
|
||||
|
||||
typedef struct private_stream_logger_t private_stream_logger_t;
|
||||
|
||||
/**
|
||||
* @brief Private data of a stream_logger_t object.
|
||||
*/
|
||||
struct private_stream_logger_t {
|
||||
|
||||
/**
|
||||
* Public data
|
||||
*/
|
||||
stream_logger_t public;
|
||||
|
||||
/**
|
||||
* Maximum level to log
|
||||
*/
|
||||
level_t max;
|
||||
|
||||
/**
|
||||
* stream to write log output to
|
||||
*/
|
||||
FILE *out;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implementation of bus_listener_t.signal.
|
||||
*/
|
||||
static void signal_(private_stream_logger_t *this, int thread,
|
||||
ike_sa_t* ike_sa, signal_t signal, level_t level,
|
||||
char *format, va_list args)
|
||||
{
|
||||
FILE *o = this->out;
|
||||
|
||||
flockfile(o);
|
||||
|
||||
if (level <= this->max)
|
||||
{
|
||||
/* then print the info */
|
||||
switch (signal)
|
||||
{
|
||||
case SIG_IKE_UP:
|
||||
{
|
||||
if (level == LEV_SUCCESS)
|
||||
{
|
||||
fprintf(o, "established: %H[%D]...%H[%D]\n",
|
||||
ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
|
||||
ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(o, "establishing failed: %H[%D]...%H[%D]:\n",
|
||||
ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
|
||||
ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
|
||||
fprintf(o, " ");
|
||||
vfprintf(o, format, args);
|
||||
fprintf(o, "\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SIG_DBG_IKE:
|
||||
case SIG_DBG_CHD:
|
||||
case SIG_DBG_JOB:
|
||||
case SIG_DBG_CFG:
|
||||
case SIG_DBG_KNL:
|
||||
case SIG_DBG_NET:
|
||||
case SIG_DBG_ENC:
|
||||
{
|
||||
vfprintf(o, format, args);
|
||||
fprintf(o, "\n");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
funlockfile(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of stream_logger_t.set_level.
|
||||
*/
|
||||
static void set_level(private_stream_logger_t *this, signal_t signal, level_t max)
|
||||
{
|
||||
this->max = max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of stream_logger_t.destroy.
|
||||
*/
|
||||
static void destroy(private_stream_logger_t *this)
|
||||
{
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
stream_logger_t *stream_logger_create(FILE *out)
|
||||
{
|
||||
private_stream_logger_t *this = malloc_thing(private_stream_logger_t);
|
||||
|
||||
/* public functions */
|
||||
this->public.listener.signal = (void(*)(bus_listener_t*,int,ike_sa_t*,signal_t,level_t,char*,va_list))signal_;
|
||||
this->public.set_level = (void(*)(stream_logger_t*,signal_t,level_t))set_level;
|
||||
this->public.destroy = (void(*)(stream_logger_t*))destroy;
|
||||
|
||||
/* private variables */
|
||||
this->max = LEV_DBG4;
|
||||
this->out = out;
|
||||
|
||||
return &this->public;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* @file stream_logger.h
|
||||
*
|
||||
* @brief Interface of stream_logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef STREAM_LOGGER_H_
|
||||
#define STREAM_LOGGER_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <types.h>
|
||||
#include <bus/bus.h>
|
||||
|
||||
typedef struct stream_logger_t stream_logger_t;
|
||||
|
||||
/**
|
||||
* @brief Logger for a file stream which implements bus_listener_t.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - stream_logger_create()
|
||||
*
|
||||
* @ingroup listeners
|
||||
*/
|
||||
struct stream_logger_t {
|
||||
|
||||
/**
|
||||
* Implements the bus_listener_t interface.
|
||||
*/
|
||||
bus_listener_t listener;
|
||||
|
||||
/**
|
||||
* @brief Set the loglevel for a signal type.
|
||||
*
|
||||
* @param this stream_logger_t object
|
||||
* @param singal type of signal
|
||||
* @param level max level to log
|
||||
*/
|
||||
void (*set_level) (stream_logger_t *this, signal_t signal, level_t level);
|
||||
|
||||
/**
|
||||
* @brief Destroys a stream_logger_t object.
|
||||
*
|
||||
* @param this stream_logger_t object
|
||||
*/
|
||||
void (*destroy) (stream_logger_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a stream_logger_t object.
|
||||
*
|
||||
* @param out output stream to log to
|
||||
* @return stream_logger_t object
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
stream_logger_t *stream_logger_create(FILE *out);
|
||||
|
||||
#endif /* STREAM_LOGGER_H_ */
|
||||
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* @file sys_logger.c
|
||||
*
|
||||
* @brief Implementation of sys_logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* for open_memstream() */
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "sys_logger.h"
|
||||
|
||||
#include <bus/listeners/stream_logger.h>
|
||||
|
||||
|
||||
typedef struct private_sys_logger_t private_sys_logger_t;
|
||||
|
||||
/**
|
||||
* Private data of a sys_logger_t object
|
||||
*/
|
||||
struct private_sys_logger_t {
|
||||
|
||||
/**
|
||||
* Public data.
|
||||
*/
|
||||
sys_logger_t public;
|
||||
|
||||
/**
|
||||
* syslog facility to use
|
||||
*/
|
||||
int facility;
|
||||
|
||||
/**
|
||||
* Internal used stream logger that does the dirty work
|
||||
*/
|
||||
stream_logger_t *logger;
|
||||
|
||||
/**
|
||||
* Memory stream used for stream_logger
|
||||
*/
|
||||
FILE *stream;
|
||||
|
||||
/**
|
||||
* Underlying buffer for stream
|
||||
*/
|
||||
char buffer[4096];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of bus_listener_t.signal.
|
||||
*/
|
||||
static void signal_(private_sys_logger_t *this, int thread, ike_sa_t* ike_sa,
|
||||
signal_t signal, level_t level,
|
||||
char *format, va_list args)
|
||||
{
|
||||
char line[512];
|
||||
char *prefix;
|
||||
FILE *reader;
|
||||
|
||||
switch (signal)
|
||||
{
|
||||
case SIG_IKE_UP:
|
||||
case SIG_IKE_DOWN:
|
||||
case SIG_IKE_REKEY:
|
||||
case SIG_DBG_IKE:
|
||||
prefix = "IKE";
|
||||
break;
|
||||
case SIG_DBG_CHD:
|
||||
prefix = "CHD";
|
||||
break;
|
||||
case SIG_DBG_JOB:
|
||||
prefix = "JOG";
|
||||
break;
|
||||
case SIG_DBG_CFG:
|
||||
prefix = "CFG";
|
||||
break;
|
||||
case SIG_DBG_KNL:
|
||||
prefix = "KNL";
|
||||
break;
|
||||
case SIG_DBG_NET:
|
||||
prefix = "NET";
|
||||
break;
|
||||
case SIG_DBG_ENC:
|
||||
prefix = "ENC";
|
||||
break;
|
||||
default:
|
||||
prefix = "???";
|
||||
break;
|
||||
}
|
||||
|
||||
flockfile(this->stream);
|
||||
/* reset memory stream */
|
||||
rewind(this->stream);
|
||||
memset(this->buffer, '\0', sizeof(this->buffer));
|
||||
/* log to memstream */
|
||||
this->logger->listener.signal(&this->logger->listener, thread, ike_sa,
|
||||
signal, level, format, args);
|
||||
/* flush is needed to append a '\0' */
|
||||
fflush(this->stream);
|
||||
|
||||
/* create a reader stream that reads out line by line */
|
||||
reader = fmemopen(this->buffer, sizeof(this->buffer), "r");
|
||||
|
||||
while (fgets(line, sizeof(line), reader))
|
||||
{
|
||||
if (line[0] == '\0')
|
||||
{
|
||||
/* abort on EOF */
|
||||
break;
|
||||
}
|
||||
else if (line[0] != '\n')
|
||||
{
|
||||
syslog(this->facility|LOG_INFO, "%.2d[%s] %s", thread, prefix, line);
|
||||
}
|
||||
}
|
||||
fclose(reader);
|
||||
funlockfile(this->stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of sys_logger_t.set_level.
|
||||
*/
|
||||
static void set_level(private_sys_logger_t *this, signal_t signal, level_t max)
|
||||
{
|
||||
this->logger->set_level(this->logger, signal, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of sys_logger_t.destroy.
|
||||
*/
|
||||
static void destroy(private_sys_logger_t *this)
|
||||
{
|
||||
closelog();
|
||||
fclose(this->stream);
|
||||
this->logger->destroy(this->logger);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
sys_logger_t *sys_logger_create(int facility)
|
||||
{
|
||||
private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
|
||||
|
||||
/* public functions */
|
||||
this->public.listener.signal = (void(*)(bus_listener_t*,int,ike_sa_t*,signal_t,level_t,char*,va_list))signal_;
|
||||
this->public.set_level = (void(*)(sys_logger_t*,signal_t,level_t))set_level;
|
||||
this->public.destroy = (void(*)(sys_logger_t*))destroy;
|
||||
|
||||
/* private variables */
|
||||
this->facility = facility;
|
||||
this->stream = fmemopen(this->buffer, sizeof(this->buffer), "w");
|
||||
if (this->stream == NULL)
|
||||
{
|
||||
/* fallback to stderr */
|
||||
this->stream = stderr;
|
||||
}
|
||||
this->logger = stream_logger_create(this->stream);
|
||||
|
||||
return &this->public;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @file sys_logger.h
|
||||
*
|
||||
* @brief Interface of sys_logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef SYS_LOGGER_H_
|
||||
#define SYS_LOGGER_H_
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include <bus/bus.h>
|
||||
|
||||
|
||||
typedef struct sys_logger_t sys_logger_t;
|
||||
|
||||
/**
|
||||
* @brief Logger for syslog which implements bus_listener_t.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - sys_logger_create()
|
||||
*
|
||||
* @ingroup listeners
|
||||
*/
|
||||
struct sys_logger_t {
|
||||
|
||||
/**
|
||||
* Implements the bus_listener_t interface.
|
||||
*/
|
||||
bus_listener_t listener;
|
||||
|
||||
/**
|
||||
* @brief Set the loglevel for a signal type.
|
||||
*
|
||||
* @param this stream_logger_t object
|
||||
* @param singal type of signal
|
||||
* @param level max level to log
|
||||
*/
|
||||
void (*set_level) (sys_logger_t *this, signal_t signal, level_t level);
|
||||
|
||||
/**
|
||||
* @brief Destroys a sys_logger_t object.
|
||||
*
|
||||
* @param this sys_logger_t object
|
||||
*/
|
||||
void (*destroy) (sys_logger_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a sys_logger_t object.
|
||||
*
|
||||
* @param facility syslog facility to use
|
||||
* @return sys_logger_t object
|
||||
*
|
||||
* @ingroup listeners
|
||||
*/
|
||||
sys_logger_t *sys_logger_create(int facility);
|
||||
|
||||
|
||||
#endif /* SYS_LOGGER_H_ */
|
||||
Reference in New Issue
Block a user