introduced new logging subsystem using bus:

passive listeners can register on the bus
  active listeners wait for signals actively
  multiplexing allows multiple listeners to receive debug signals
  a lot more...
This commit is contained in:
Martin Willi
2006-10-18 11:46:13 +00:00
parent 8cdce67afa
commit 60356f3375
129 changed files with 4264 additions and 6440 deletions
+5 -9
View File
@@ -35,11 +35,7 @@
#include <asn1/asn1.h>
/**
* String mappings for id_type_t.
*/
static const char *const id_type_name[] = {
ENUM_BEGIN(id_type_names, ID_ANY, ID_KEY_ID,
"ID_ANY",
"ID_IPV4_ADDR",
"ID_FQDN",
@@ -51,11 +47,11 @@ static const char *const id_type_name[] = {
"ID_IPV6_ADDR_RANGE",
"ID_DER_ASN1_DN",
"ID_DER_ASN1_GN",
"ID_KEY_ID",
};
"ID_KEY_ID");
ENUM_NEXT(id_type_names, ID_DER_ASN1_GN_URI, ID_DER_ASN1_GN_URI, ID_KEY_ID,
"ID_DER_ASN1_GN_URI");
ENUM_END(id_type_names, ID_DER_ASN1_GN_URI);
enum_names id_type_names =
{ ID_ANY, ID_KEY_ID, id_type_name, NULL };
/**
* X.501 acronyms for well known object identifiers (OIDs)
+2 -2
View File
@@ -116,9 +116,9 @@ enum id_type_t {
};
/**
* String mappings for id_type_t.
* enum names for id_type_t.
*/
extern enum_names id_type_names;
extern enum_name_t *id_type_names;
typedef struct identification_t identification_t;
+28 -32
View File
@@ -38,6 +38,7 @@
#include "leak_detective.h"
#include <types.h>
#include <library.h>
#ifdef LEAK_DETECTIVE
@@ -116,11 +117,6 @@ static memory_header_t first_header = {
next: NULL
};
/**
* logger for the leak detective
*/
static logger_t *logger;
/**
* standard hooks, used to temparily remove hooking
*/
@@ -149,11 +145,11 @@ static void log_stack_frames(void **stack_frames, int stack_frame_count)
strings = backtrace_symbols (stack_frames, stack_frame_count);
logger->log(logger, ERROR, " dumping %d stack frame addresses", stack_frame_count);
DBG1(" dumping %d stack frame addresses", stack_frame_count);
for (i = 0; i < stack_frame_count; i++)
{
logger->log(logger, ERROR, " %s", strings[i]);
DBG1(" %s", strings[i]);
}
free (strings);
}
@@ -166,7 +162,7 @@ static void log_stack_frames(void **stack_frames, int stack_frame_count)
*
* The range_size is calculated using the readelf utility, e.g.:
* readelf -s /lib/glibc.so.6
* These values may or may not be acceptable for another system.
* The values are for glibc-2.4 and may or may not be correct on other systems.
*/
typedef struct whitelist_t whitelist_t;
@@ -176,15 +172,16 @@ struct whitelist_t {
};
whitelist_t whitelist[] = {
{pthread_create, 381},
{pthread_setspecific, 256},
{mktime, 60},
{tzset, 126},
{inet_ntoa, 256},
{strerror, 173},
{getprotobynumber, 294},
{getservbyport, 309},
{register_printf_function, 150},
{pthread_create, 2542},
{pthread_setspecific, 217},
{mktime, 60},
{tzset, 123},
{inet_ntoa, 249},
{strerror, 180},
{getprotobynumber, 291},
{getservbyport, 311},
{register_printf_function, 159},
{syslog, 45},
};
/**
@@ -220,7 +217,7 @@ void report_leaks()
{
if (!is_whitelisted(hdr->stack_frames, hdr->stack_frame_count))
{
logger->log(logger, ERROR, "Leak (%d bytes at %p):", hdr->bytes, hdr + 1);
DBG1("Leak (%d bytes at %p):", hdr->bytes, hdr + 1);
log_stack_frames(hdr->stack_frames, hdr->stack_frame_count);
leaks++;
}
@@ -229,13 +226,13 @@ void report_leaks()
switch (leaks)
{
case 0:
logger->log(logger, CONTROL, "No leaks detected");
DBG1("No leaks detected");
break;
case 1:
logger->log(logger, ERROR, "One leak detected");
DBG1("One leak detected");
break;
default:
logger->log(logger, ERROR, "%d leaks detected", leaks);
DBG1("%d leaks detected", leaks);
break;
}
}
@@ -322,8 +319,8 @@ void free_hook(void *ptr, const void *caller)
uninstall_hooks();
if (hdr->magic != MEMORY_HEADER_MAGIC)
{
logger->log(logger, ERROR, "freeing of invalid memory (%p, MAGIC 0x%x != 0x%x):",
ptr, hdr->magic, MEMORY_HEADER_MAGIC);
DBG1("freeing of invalid memory (%p, MAGIC 0x%x != 0x%x):",
ptr, hdr->magic, MEMORY_HEADER_MAGIC);
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
log_stack_frames(stack_frames, stack_frame_count);
install_hooks();
@@ -368,7 +365,7 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
uninstall_hooks();
if (hdr->magic != MEMORY_HEADER_MAGIC)
{
logger->log(logger, ERROR, "reallocation of invalid memory (%p):", old);
DBG1("reallocation of invalid memory (%p):", old);
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
log_stack_frames(stack_frames, stack_frame_count);
install_hooks();
@@ -397,16 +394,15 @@ void *realloc_hook(void *old, size_t bytes, const void *caller)
/**
* Setup leak detective
*/
void leak_detective_init()
void __attribute__ ((constructor)) leak_detective_init()
{
logger = logger_manager->get_logger(logger_manager, LEAK_DETECT);
install_hooks();
}
/**
* Clean up leak detective
*/
void leak_detective_cleanup()
void __attribute__ ((destructor)) leak_detective_cleanup()
{
uninstall_hooks();
report_leaks();
@@ -415,7 +411,7 @@ void leak_detective_cleanup()
/**
* Log memory allocation statistics
*/
void leak_detective_status(logger_t *logger)
void leak_detective_status(FILE *stream)
{
u_int blocks = 0;
size_t bytes = 0;
@@ -429,10 +425,10 @@ void leak_detective_status(logger_t *logger)
}
pthread_mutex_unlock(&mutex);
logger->log(logger, CONTROL|LEVEL1, "allocation statistics:");
logger->log(logger, CONTROL|LEVEL1, " call stats: malloc: %d, free: %d, realloc: %d",
fprintf(stream, "allocation statistics:\n");
fprintf(stream, " call stats: malloc: %d, free: %d, realloc: %d\n",
count_malloc, count_free, count_realloc);
logger->log(logger, CONTROL|LEVEL1, " allocated %d blocks, total size %d bytes (avg. %d bytes)",
fprintf(stream, " allocated %d blocks, total size %d bytes (avg. %d bytes)\n",
blocks, bytes, bytes/blocks);
}
@@ -441,7 +437,7 @@ void leak_detective_status(logger_t *logger)
/**
* Dummy when !using LEAK_DETECTIVE
*/
void leak_detective_status(logger_t *logger)
void leak_detective_status(FILE *stream)
{
}
+1 -23
View File
@@ -22,36 +22,14 @@
#ifndef LEAK_DETECTIVE_H_
#define LEAK_DETECTIVE_H_
#include <utils/logger_manager.h>
/**
* Log status information about allocation
*/
void leak_detective_status(logger_t *logger);
#ifdef LEAK_DETECTIVE
void leak_detective_status(FILE *stream);
/**
* Max number of stack frames to include in a backtrace.
*/
#define STACK_FRAMES_COUNT 30
/**
* Initialize leak detective, activates it
*/
void leak_detective_init();
/**
* Cleanup leak detective, deactivates it
*/
void leak_detective_cleanup();
#else /* !LEAK_DETECTIVE */
#define leak_detective_init() {}
#define leak_detective_cleanup() {}
#endif /* LEAK_DETECTIVE */
#endif /* LEAK_DETECTIVE_H_ */
+27 -21
View File
@@ -127,6 +127,11 @@ struct private_iterator_t {
* Direction of iterator.
*/
bool forward;
/**
* Mutex to use to synchronize access
*/
pthread_mutex_t *mutex;
};
/**
@@ -361,6 +366,10 @@ static void insert_after(private_iterator_t * iterator, void *item)
*/
static void iterator_destroy(private_iterator_t *this)
{
if (this->mutex)
{
pthread_mutex_unlock(this->mutex);
}
free(this);
}
@@ -372,24 +381,6 @@ static int get_count(private_linked_list_t *this)
return this->count;
}
/**
* Implementation of linked_list_t.call_on_items.
*/
static void call_on_items(private_linked_list_t *this, void(*func)(void*))
{
iterator_t *iterator;
void *item;
iterator = this->public.create_iterator(&this->public,TRUE);
while (iterator->has_next(iterator))
{
iterator->current(iterator, &item);
(*func)(item);
}
iterator->destroy(iterator);
}
/**
* Implementation of linked_list_t.insert_first.
*/
@@ -629,7 +620,7 @@ static status_t get_last(private_linked_list_t *this, void **item)
static iterator_t *create_iterator (private_linked_list_t *linked_list, bool forward)
{
private_iterator_t *this = malloc_thing(private_iterator_t);
this->public.get_count = (bool (*) (iterator_t *this)) get_list_count;
this->public.iterate = (bool (*) (iterator_t *this, void **value)) iterate;
this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next;
@@ -640,11 +631,26 @@ static iterator_t *create_iterator (private_linked_list_t *linked_list, bool for
this->public.remove = (status_t (*) (iterator_t *this)) remove;
this->public.reset = (void (*) (iterator_t *this)) iterator_reset;
this->public.destroy = (void (*) (iterator_t *this)) iterator_destroy;
this->forward = forward;
this->current = NULL;
this->list = linked_list;
this->mutex = NULL;
return &this->public;
}
/**
* Implementation of linked_list_t.create_iterator_locked.
*/
static iterator_t *create_iterator_locked(private_linked_list_t *linked_list,
pthread_mutex_t *mutex)
{
private_iterator_t *this = (private_iterator_t*)create_iterator(linked_list, TRUE);
this->mutex = mutex;
pthread_mutex_lock(mutex);
return &this->public;
}
@@ -672,7 +678,7 @@ linked_list_t *linked_list_create()
this->public.get_count = (int (*) (linked_list_t *)) get_count;
this->public.create_iterator = (iterator_t * (*) (linked_list_t *,bool))create_iterator;
this->public.call_on_items = (void (*) (linked_list_t *, void(*func)(void*)))call_on_items;
this->public.create_iterator_locked = (iterator_t * (*) (linked_list_t *,pthread_mutex_t*))create_iterator_locked;
this->public.get_first = (status_t (*) (linked_list_t *, void **item))get_first;
this->public.get_last = (status_t (*) (linked_list_t *, void **item))get_last;
this->public.insert_first = (void (*) (linked_list_t *, void *item))insert_first;
+15 -21
View File
@@ -24,6 +24,8 @@
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#include <pthread.h>
#include <types.h>
#include <utils/iterator.h>
@@ -31,18 +33,13 @@
typedef struct linked_list_t linked_list_t;
/**
* @brief Class implementing a double linked list (named only as linked list).
* @brief Class implementing a double linked list.
*
* @warning Access to an object of this type is not thread-save.
* General purpose linked list. This list is not synchronized.
*
* @b Costructors:
* - linked_list_create()
*
* @see
* - job_queue_t
* - event_queue_t
* - send_queue_t
*
*
* @ingroup utils
*/
struct linked_list_t {
@@ -64,27 +61,24 @@ struct linked_list_t {
* @param forward iterator direction (TRUE: front to end)
* @return new iterator_t object
*/
iterator_t * (*create_iterator) (linked_list_t *linked_list, bool forward);
iterator_t *(*create_iterator) (linked_list_t *linked_list, bool forward);
/**
* @brief Call a function with list element as argument.
*
* This method accepts a function, which will be called for
* each list element once. The function must accept the list
* element as the first argument. Handy for destruction of
* list elements.
*
* @todo Additional vararg which are passed to the
* function would be nice...
* @brief Creates a iterator, locking a mutex.
*
* The supplied mutex is acquired immediately, and released
* when the iterator gets destroyed.
*
* @param linked_list calling object
* @param func function to call
* @param mutex mutex to use for exclusive access
* @return new iterator_t object
*/
void (*call_on_items) (linked_list_t *linked_list, void(*func)(void*));
iterator_t *(*create_iterator_locked) (linked_list_t *linked_list,
pthread_mutex_t *mutex);
/**
* @brief Inserts a new item at the beginning of the list.
*
*
* @param linked_list calling object
* @param[in] item item value to insert in list
*/
-384
View File
@@ -1,384 +0,0 @@
/**
* @file logger.c
*
* @brief Implementation of logger_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 <syslog.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include "logger.h"
/**
* Maximum length of a log entry (only used for logger_s.log).
*/
#define MAX_LOG 8192
/**
* Maximum number of logged bytes per line
*/
#define MAX_BYTES 16
typedef struct private_logger_t private_logger_t;
/**
* @brief Private data of a logger_t object.
*/
struct private_logger_t {
/**
* Public data.
*/
logger_t public;
/**
* Detail-level of logger.
*/
log_level_t level;
/**
* Name of logger.
*/
char *name;
/**
* File to write log output to.
* NULL for syslog.
*/
FILE *output;
/**
* Should a thread_id be included in the log?
*/
bool log_thread_id;
};
/**
* thread local storage for get_thread_number
*/
static pthread_key_t thread_ids;
static void make_key(void)
{
pthread_key_create(&thread_ids, NULL);
}
/**
* 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(void)
{
static int current_num = 0;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
int stored_num;
pthread_once(&key_once, make_key);
stored_num = (int)pthread_getspecific(thread_ids);
if (stored_num == 0)
{
pthread_setspecific(thread_ids, (void*)++current_num);
return current_num;
}
else
{
return stored_num;
}
}
/**
* prepend the logging prefix to string and store it in buffer
*/
static void prepend_prefix(private_logger_t *this, log_level_t loglevel, const char *string, char *buffer)
{
char thread_id[3] = "";
char log_type, log_details;
char *separator = (strlen(this->name) == 0)? "" : ":";
if (loglevel & CONTROL)
{
log_type = 'C';
}
else if (loglevel & ERROR)
{
log_type = 'E';
}
else if (loglevel & RAW)
{
log_type = 'R';
}
else if (loglevel & PRIVATE)
{
log_type = 'P';
}
else if (loglevel & AUDIT)
{
log_type = 'A';
}
else
{
log_type = '-';
}
if (loglevel & (LEVEL3 - LEVEL2))
{
log_details = '3';
}
else if (loglevel & (LEVEL2 - LEVEL1))
{
log_details = '2';
}
else if (loglevel & LEVEL1)
{
log_details = '1';
}
else
{
log_details = '0';
}
if (this->log_thread_id)
{
snprintf(thread_id, sizeof(thread_id), "%02d", get_thread_number());
}
snprintf(buffer, MAX_LOG, "%s[%c%c%s%s] %s",
thread_id, log_type, log_details, separator, this->name, string);
}
/**
* Convert a charon-loglevel to a syslog priority
*/
static int get_priority(log_level_t loglevel)
{
if (loglevel & ERROR)
{
return LOG_AUTHPRIV|LOG_ERR;
}
if (loglevel & AUDIT)
{
return LOG_AUTHPRIV|LOG_INFO;
}
return LOG_AUTHPRIV|LOG_DEBUG;
}
/**
* Implementation of logger_t.logv.
*/
static void logv(private_logger_t *this, log_level_t loglevel, const char *format, va_list args)
{
if ((this->level & loglevel) == loglevel)
{
char buffer[MAX_LOG];
if (this->output == NULL)
{
/* syslog */
prepend_prefix(this, loglevel, format, buffer);
vsyslog(get_priority(loglevel), buffer, args);
}
else
{
/* File output */
prepend_prefix(this, loglevel, format, buffer);
vfprintf(this->output, buffer, args);
fprintf(this->output, "\n");
}
}
}
/**
* Implementation of logger_t.log.
*/
static void logg(private_logger_t *this, log_level_t loglevel, const char *format, ...)
{
va_list args;
va_start(args, format);
logv(this, loglevel, format, args);
va_end(args);
}
/**
* Implementation of logger_t.log_bytes.
*/
static void log_bytes(private_logger_t *this, log_level_t loglevel, const char *label, const char *bytes, size_t len)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
if ((this->level & loglevel) == loglevel)
{
char thread_id[3] = "";
char buffer[MAX_LOG];
char ascii_buffer[MAX_BYTES+1];
char *buffer_pos = buffer;
const char format[] = "%s %d bytes @ %p";
const char *bytes_pos = bytes;
const char *bytes_roof = bytes + len;
int line_start = 0;
int i = 0;
/* since me can't do multi-line output to syslog,
* we must do multiple syslogs. To avoid
* problems in output order, lock this by a mutex.
*/
pthread_mutex_lock(&mutex);
prepend_prefix(this, loglevel, format, buffer);
if (this->log_thread_id)
{
snprintf(thread_id, sizeof(thread_id), "%02d", get_thread_number());
}
if (this->output == NULL)
{
syslog(get_priority(loglevel), buffer, label, len, bytes);
}
else
{
fprintf(this->output, buffer, label, len, bytes);
fprintf(this->output, "\n");
}
while (bytes_pos < bytes_roof)
{
static char hexdig[] = "0123456789ABCDEF";
*buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
ascii_buffer[i++] = (*bytes_pos > 31 && *bytes_pos < 127)
? *bytes_pos : '.';
if (++bytes_pos == bytes_roof || i == MAX_BYTES)
{
int padding = 3 * (MAX_BYTES - i);
while (padding--)
{
*buffer_pos++ = ' ';
}
*buffer_pos++ = '\0';
ascii_buffer[i] = '\0';
if (this->output == NULL)
{
syslog(get_priority(loglevel), "%s[ :%5d] %s %s", thread_id, line_start, buffer, ascii_buffer);
}
else
{
fprintf(this->output, "%s[ :%5d] %s %s\n", thread_id, line_start, buffer, ascii_buffer);
}
buffer_pos = buffer;
line_start += MAX_BYTES;
i = 0;
}
else
{
*buffer_pos++ = ' ';
}
}
pthread_mutex_unlock(&mutex);
}
}
/**
* Implementation of logger_t.log_chunk.
*/
static void log_chunk(logger_t *this, log_level_t loglevel, const char *label, chunk_t chunk)
{
this->log_bytes(this, loglevel, label, chunk.ptr, chunk.len);
}
/**
* Implementation of logger_t.enable_level.
*/
static void enable_level(private_logger_t *this, log_level_t log_level)
{
this->level |= log_level;
}
/**
* Implementation of logger_t.disable_level.
*/
static void disable_level(private_logger_t *this, log_level_t log_level)
{
this->level &= ~log_level;
}
/**
* Implementation of logger_t.set_output.
*/
static void set_output(private_logger_t *this, FILE * output)
{
this->output = output;
}
/**
* Implementation of logger_t.get_level.
*/
static log_level_t get_level(private_logger_t *this)
{
return this->level;
}
/**
* Implementation of logger_t.destroy.
*/
static void destroy(private_logger_t *this)
{
free(this->name);
free(this);
}
/*
* Described in header.
*/
logger_t *logger_create(char *logger_name, log_level_t log_level, bool log_thread_id, FILE * output)
{
private_logger_t *this = malloc_thing(private_logger_t);
/* public functions */
this->public.log = (void(*)(logger_t*,log_level_t,const char*,...))logg;
this->public.logv = (void(*)(logger_t*,log_level_t,const char*,va_list))logv;
this->public.log_bytes = (void(*)(logger_t*, log_level_t, const char*, const char*,size_t))log_bytes;
this->public.log_chunk = log_chunk;
this->public.enable_level = (void(*)(logger_t*,log_level_t))enable_level;
this->public.disable_level = (void(*)(logger_t*,log_level_t))disable_level;
this->public.get_level = (log_level_t(*)(logger_t*))get_level;
this->public.set_output = (void(*)(logger_t*,FILE*))set_output;
this->public.destroy = (void(*)(logger_t*))destroy;
if (logger_name == NULL)
{
logger_name = "";
}
/* private variables */
this->level = log_level;
this->log_thread_id = log_thread_id;
this->name = malloc(strlen(logger_name) + 1);
strcpy(this->name,logger_name);
this->output = output;
return (logger_t*)this;
}
-213
View File
@@ -1,213 +0,0 @@
/**
* @file logger.h
*
* @brief Interface of logger_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 LOGGER_H_
#define LOGGER_H_
#include <stdio.h>
#include <stdarg.h>
#include <types.h>
typedef enum log_level_t log_level_t;
/**
* @brief Log Levels supported by the logger object.
*
* Logleves are devided in two different kinds:
* - levels to specify the type of the log
* - levels to specify the detail-level of the log
*
* Use combinations of these to build detailed loglevels, such
* as CONTROL|LEVEL2 fore a detailed cotrol level, or
* use RAW to see all raw data dumps (except private).
*
* @ingroup utils
*/
enum log_level_t {
/**
* Control flow.
*/
CONTROL = 1,
/**
* Error reporting.
*/
ERROR = 2,
/**
* Logs important for the sysadmin.
*/
AUDIT = 4,
/**
* Raw data dumps.
*/
RAW = 8,
/**
* Private data dumps.
*/
PRIVATE = 16,
/**
* Log most important output, can be omitted.
*/
LEVEL0 = 0,
/**
* Log more detailed output.
*/
LEVEL1 = 32,
/**
* Log even more detailed output.
*/
LEVEL2 = LEVEL1 + 64,
/**
* Use maximum detailed output.
*/
LEVEL3 = LEVEL2 + 128,
/**
* Summary for all types with all detail-levels.
*/
FULL = LEVEL3 + CONTROL + ERROR + RAW + PRIVATE + AUDIT
};
typedef struct logger_t logger_t;
/**
* @brief Class to simplify logging.
*
* @b Constructors:
* - logger_create()
*
* @ingroup utils
*/
struct logger_t {
/**
* @brief Log an entry, using printf()-like params.
*
* All specified loglevels must be activated that
* the log is done.
*
* @param this logger_t object
* @param loglevel or'ed set of log_level_t's
* @param format printf like format string
* @param ... printf like parameters
*/
void (*log) (logger_t *this, log_level_t log_level, const char *format, ...);
/**
* @brief Log an entry, using vprintf() style va_list parameters.
*
* All specified loglevels must be activated that
* the log is done.
*
* @param this logger_t object
* @param loglevel or'ed set of log_level_t's
* @param format printf like format string
* @param args va_list argument list
*/
void (*logv) (logger_t *this, log_level_t log_level, const char *format, va_list args);
/**
* @brief Log some bytes, useful for debugging.
*
* All specified loglevels must be activated that
* the log is done.
*
* @param this logger_t object
* @param loglevel or'ed set of log_level_t's
* @param label a labeling name, logged with the bytes
* @param bytes pointer to the bytes to dump
* @param len number of bytes to dump
*/
void (*log_bytes) (logger_t *this, log_level_t loglevel, const char *label, const char *bytes, size_t len);
/**
* @brief Log a chunk, useful for debugging.
*
* All specified loglevels must be activated that
* the log is done.
*
* @param this logger_t object
* @param loglevel or'ed set of log_level_t's
* @param label a labeling name, logged with the bytes
* @param chunk chunk to log
*/
void (*log_chunk) (logger_t *this, log_level_t loglevel, const char *label, chunk_t chunk);
/**
* @brief Enables a loglevel for the current logger_t object.
*
* @param this logger_t object
* @param log_level loglevel to enable
*/
void (*enable_level) (logger_t *this, log_level_t log_level);
/**
* @brief Disables a loglevel for the current logger_t object.
*
* @param this logger_t object
* @param log_level loglevel to enable
*/
void (*disable_level) (logger_t *this, log_level_t log_level);
/**
* @brief Set the output of the logger.
*
* Use NULL for syslog.
*
* @param this logger_t object
* @param output file, where log output should be written
*/
void (*set_output) (logger_t *this, FILE *output);
/**
* @brief Get the currently used loglevel.
*
* @param this logger_t object
* @return currently used loglevel
*/
log_level_t (*get_level) (logger_t *this);
/**
* @brief Destroys a logger_t object.
*
* @param this logger_t object
*/
void (*destroy) (logger_t *this);
};
/**
* @brief Constructor to create a logger_t object.
*
* @param logger_name name for the logger_t object
* @param log_level or'ed set of log_levels to assign to the new logger_t object
* @param log_thread_id TRUE if thread id should also be logged
* @param output FILE * if log has to go on a file output, NULL for syslog
* @return logger_t object
*
* @ingroup utils
*/
logger_t *logger_create(char *logger_name, log_level_t log_level, bool log_thread_id, FILE * output);
#endif /*LOGGER_H_*/
-220
View File
@@ -1,220 +0,0 @@
/**
* @file logger_manager.c
*
* @brief Implementation of logger_manager_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 "logger_manager.h"
#include <definitions.h>
#include <utils/linked_list.h>
/**
* String mappings for logger_context_t
*/
mapping_t logger_context_t_mappings[] = {
{PARSER, "PARSER"},
{GENERATOR, "GENERATOR"},
{IKE_SA, "IKE_SA"},
{IKE_SA_MANAGER, "IKE_SA_MANAGER"},
{CHILD_SA, "CHILD_SA"},
{MESSAGE, "MESSAGE"},
{THREAD_POOL, "THREAD_POOL"},
{WORKER, "WORKER"},
{SCHEDULER, "SCHEDULER"},
{SENDER, "SENDER"},
{RECEIVER, "RECEIVER"},
{SOCKET, "SOCKET"},
{TESTER, "TESTER"},
{DAEMON, "DAEMON"},
{CONFIG, "CONFIG"},
{ENCRYPTION_PAYLOAD, "ENCRYPTION_PAYLOAD"},
{PAYLOAD, "PAYLOAD"},
{DER_DECODER, "DER_DECODER"},
{DER_ENCODER, "DER_ENCODER"},
{ASN1, "ASN1"},
{XFRM, "XFRM"},
{LEAK_DETECT, "LEAK_DETECT"},
{MAPPING_END, NULL},
};
struct {
char *name;
log_level_t level;
bool log_thread_ids;
} logger_defaults[] = {
{ "PARSR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* PARSER */
{ "GNRAT", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* GENERATOR */
{ "IKESA", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* IKE_SA */
{ "SAMGR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* IKE_SA_MANAGER */
{ "CHDSA", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* CHILD_SA */
{ "MESSG", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* MESSAGE */
{ "TPOOL", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* THREAD_POOL */
{ "WORKR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* WORKER */
{ "SCHED", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* SCHEDULER */
{ "SENDR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* SENDER */
{ "RECVR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* RECEIVER */
{ "SOCKT", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* SOCKET */
{ "TESTR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* TESTER */
{ "DAEMN", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* DAEMON */
{ "CONFG", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* CONFIG */
{ "ENCPL", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* ENCRYPTION_PAYLOAD */
{ "PAYLD", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* PAYLOAD */
{ "DERDC", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* DER_DECODER */
{ "DEREC", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* DER_ENCODER */
{ "ASN_1", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* ASN1 */
{ "XFRM ", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* XFRM */
{ "LEAKD", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* LEAK_DETECT */
};
typedef struct private_logger_manager_t private_logger_manager_t;
/**
* Private data of logger_manager_t object.
*/
struct private_logger_manager_t {
/**
* Public data.
*/
logger_manager_t public;
/**
* Array of loggers, one for each context
*/
logger_t *loggers[LOGGER_CONTEXT_ROOF];
};
/**
* The one and only instance of the logger manager
*/
static private_logger_manager_t private_logger_manager;
/**
* Exported pointer for the logger manager
*/
logger_manager_t *logger_manager = (logger_manager_t *)&private_logger_manager;
/**
* Implementation of logger_manager_t.get_logger.
*/
static logger_t *get_logger(private_logger_manager_t *this, logger_context_t context)
{
return this->loggers[context];
}
/**
* Implementation of logger_manager_t.get_log_level.
*/
static log_level_t get_log_level (private_logger_manager_t *this, logger_context_t context)
{
return this->loggers[context]->get_level(this->loggers[context]);
}
/**
* Implementation of private_logger_manager_t.enable_log_level.
*/
static void enable_log_level(private_logger_manager_t *this, logger_context_t context, log_level_t level)
{
if (context == ALL_LOGGERS)
{
for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
{
this->loggers[context]->enable_level(this->loggers[context], level);
}
}
else
{
this->loggers[context]->enable_level(this->loggers[context], level);
}
}
/**
* Implementation of private_logger_manager_t.disable_log_level.
*/
static void disable_log_level(private_logger_manager_t *this, logger_context_t context, log_level_t level)
{
if (context == ALL_LOGGERS)
{
for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
{
this->loggers[context]->disable_level(this->loggers[context], level);
}
}
else
{
this->loggers[context]->disable_level(this->loggers[context], level);
}
}
/**
* Implementation of private_logger_manager_t.set_output.
*/
static void set_output(private_logger_manager_t *this, logger_context_t context, FILE *output)
{
if (context == ALL_LOGGERS)
{
for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
{
this->loggers[context]->set_output(this->loggers[context], output);
}
}
else
{
this->loggers[context]->set_output(this->loggers[context], output);
}
}
/**
* Creates the instance of the logger manager at library startup
*/
void logger_manager_init()
{
int i;
logger_manager->get_logger = (logger_t *(*)(logger_manager_t*,logger_context_t context))get_logger;
logger_manager->get_log_level = (log_level_t (*)(logger_manager_t *, logger_context_t)) get_log_level;
logger_manager->enable_log_level = (void (*)(logger_manager_t *, logger_context_t, log_level_t)) enable_log_level;
logger_manager->disable_log_level = (void (*)(logger_manager_t *, logger_context_t, log_level_t)) disable_log_level;
logger_manager->set_output = (void (*)(logger_manager_t *, logger_context_t, FILE*)) set_output;
for (i = 0; i < LOGGER_CONTEXT_ROOF; i++)
{
private_logger_manager.loggers[i] = logger_create(logger_defaults[i].name,
logger_defaults[i].level,
logger_defaults[i].log_thread_ids,
INITIAL_LOG_OUTPUT);
}
}
/**
* Destroy the logger manager at library exit
*/
void logger_manager_cleanup()
{
int i;
for (i = 0; i < LOGGER_CONTEXT_ROOF; i++)
{
private_logger_manager.loggers[i]->destroy(private_logger_manager.loggers[i]);
}
}
-161
View File
@@ -1,161 +0,0 @@
/**
* @file logger_manager.h
*
* @brief Interface of logger_manager_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 LOGGER_MANAGER_H_
#define LOGGER_MANAGER_H_
#include <pthread.h>
#include <utils/logger.h>
#define INITIAL_LOG_OUTPUT stdout
typedef enum logger_context_t logger_context_t;
/**
* @brief Context of a specific logger.
*
* @ingroup utils
*/
enum logger_context_t {
ALL_LOGGERS = -1,
PARSER = 0,
GENERATOR,
IKE_SA,
IKE_SA_MANAGER,
CHILD_SA,
MESSAGE,
THREAD_POOL,
WORKER,
SCHEDULER,
SENDER,
RECEIVER,
SOCKET,
TESTER,
DAEMON,
CONFIG,
ENCRYPTION_PAYLOAD,
PAYLOAD,
DER_DECODER,
DER_ENCODER,
ASN1,
XFRM,
LEAK_DETECT,
LOGGER_CONTEXT_ROOF,
};
typedef struct logger_manager_t logger_manager_t;
/**
* @brief Class to manage logger_t objects.
*
* The logger manager manages all logger_t object in a list and
* allows their manipulation. Via a logger_context_t, the loglevel
* of a specific logging type can be adjusted at runtime.
* This class differs from others, as it has no constructor or destroy
* function. The one and only instance "logger_manager" is created at
* library start and destroyed at exit.
*
* @b Constructors:
* - none, logger_manager is the single instance
* use logger_manager_init/logger_manager_cleanup
*
* @see logger_t
*
* @ingroup utils
*/
struct logger_manager_t {
/**
* @brief Gets a logger_t object for a specific logger context.
*
* @param this logger_manager_t object
* @param context logger_context to use the logger for
* @param name name for the new logger. Context name is already included
* and has not to be specified (so NULL is allowed)
* @return logger_t object
*/
logger_t *(*get_logger) (logger_manager_t *this, logger_context_t context);
/**
* @brief Returns the set log_level of a specific context.
*
* @param this calling object
* @param context context to check level
* @return log_level for the given logger_context
*/
log_level_t (*get_log_level) (logger_manager_t *this, logger_context_t context);
/**
* @brief Enables a logger level of a specific context.
*
* Use context ALL_LOGGERS to manipulate all loggers.
*
* @param this calling object
* @param context context to set level
* @param log_level logger level to eanble
*/
void (*enable_log_level) (logger_manager_t *this, logger_context_t context,log_level_t log_level);
/**
* @brief Disables a logger level of a specific context.
*
* Use context ALL_LOGGERS to manipulate all loggers.
*
* @param this calling object
* @param context context to set level
* @param log_level logger level to disable
*/
void (*disable_log_level) (logger_manager_t *this, logger_context_t context,log_level_t log_level);
/**
* @brief Sets the output of a logger.
*
* Use context ALL_LOGGERS to redirect all loggers.
*
* @param this calling object
* @param context context to set output
* @param log_level logger level to disable
*/
void (*set_output) (logger_manager_t *this, logger_context_t context, FILE *output);
};
/**
* The single and global instance of the logger_manager
*/
extern logger_manager_t *logger_manager;
/**
* Initialize the logger manager with all its logger.
* Has to be called before logger_manager is accessed.
*/
void logger_manager_init(void);
/**
* Free any resources hold by the logger manager. Do
* not access logger_manager after this call.
*/
void logger_manager_cleanup(void);
#endif /*LOGGER_MANAGER_H_*/
-255
View File
@@ -1,255 +0,0 @@
/**
* @file tester.c
*
* @brief Implementation of tester_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include "tester.h"
typedef struct private_tester_t private_tester_t;
/**
* @brief Private Data of tester_t class.
*
*/
struct private_tester_t {
/**
* Protected interface of tester_t.
*/
protected_tester_t protected;
/**
* Runs a specific test.
*
* @param tester associated tester object
* @param test_function test function to perform
* @param test_name name for the given test
*/
void (*run_test) (private_tester_t *tester, void (*test_function) (protected_tester_t * tester), char * test_name);
/**
* Returns the difference of to timeval structs in microseconds.
*
* @warning this function is also defined in the event queue
* in later improvements, this function can be added to a general
* class type!
*
* @param end_time end time
* @param start_time start time
*
* @TODO make object function or move to utils!
*
* @return difference in microseconds
*/
long (*time_difference) (private_tester_t *tester,struct timeval *end_time, struct timeval *start_time);
/**
* Output is written into this file.
*/
FILE* output;
/**
* Number of already performed tests.
*/
int tests_count;
/**
* Number of failed tests.
*/
int failed_tests_count;
/**
* Number of failed asserts in current test.
*/
int failed_asserts_count;
/**
* TRUE if also succeeded asserts should be written to output.
*/
bool display_succeeded_asserts;
/**
* Mutex to make this class thread-save.
*/
pthread_mutex_t mutex;
};
/**
* Implementation of tester_t.perform_tests.
*/
static void perform_tests(private_tester_t *this,test_t **tests)
{
int current_test = 0;
fprintf(this->output,"\nStart testing...\n\n");
fprintf(this->output,"_____________________________________________________________________\n");
fprintf(this->output,"Testname | running time\n");
fprintf(this->output,"_______________________________________________________|_____________\n");
while (tests[current_test] != NULL)
{
this->run_test(this,tests[current_test]->test_function,tests[current_test]->test_name);
current_test++;
}
fprintf(this->output,"=====================================================================\n");
fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
fprintf(this->output,"=====================================================================\n");
}
/**
* Implementation of tester_t.perform_test.
*/
static void perform_test(private_tester_t *this, test_t *test)
{
test_t *tests[] = {test, NULL};
return (perform_tests(this,tests));
}
/**
* Returns the difference of to timeval structs in microseconds.
*
* @warning this function is also defined in the event queue
* in later improvements, this function can be added to a general
* class type!
*
* @param end_time end time
* @param start_time start time
*
* @TODO make object function or move to utils!
*
* @return difference in microseconds
*/
static long time_difference(private_tester_t *this,struct timeval *end_time, struct timeval *start_time)
{
long seconds, microseconds;
seconds = (end_time->tv_sec - start_time->tv_sec);
microseconds = (end_time->tv_usec - start_time->tv_usec);
return ((seconds * 1000000) + microseconds);
}
/**
* Implementation of private_tester_t.run_test.
*/
static void run_test(private_tester_t *this, void (*test_function) (protected_tester_t * tester), char * test_name)
{
struct timeval start_time, end_time;
long timediff;
this->tests_count++;
this->failed_asserts_count = 0;
fprintf(this->output,"%-55s\n", test_name);
gettimeofday(&start_time,NULL);
test_function(&(this->protected));
gettimeofday(&end_time,NULL);
timediff = this->time_difference(this,&end_time, &start_time);
if (this->failed_asserts_count > 0)
{
fprintf(this->output," => Test failed: %-37s|%10ld us\n",test_name,timediff);
}else
{
fprintf(this->output,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff);
}
if (this->failed_asserts_count > 0)
{
this->failed_tests_count++;
}
}
/**
* Implementation of tester_t.assert_true.
*/
static void assert_true(private_tester_t *this, bool to_be_true,char * assert_name)
{
if (assert_name == NULL)
{
assert_name = "unknown";
}
pthread_mutex_lock(&(this->mutex));
if (!to_be_true)
{
this->failed_asserts_count++;
fprintf(this->output," check '%s' failed!\n", assert_name);
}else
{
if (this->display_succeeded_asserts)
{
fprintf(this->output," check '%s' succeeded\n", assert_name);
}
}
pthread_mutex_unlock(&(this->mutex));
}
/**
* Implementation of tester_t.assert_false.
*/
static void assert_false(private_tester_t *this, bool to_be_false,char * assert_name)
{
this->protected.assert_true(&(this->protected),(!to_be_false),assert_name);
}
/**
* Implementation of tester_t.destroy.
*/
static void destroy(private_tester_t *tester)
{
private_tester_t *this = (private_tester_t*) tester;
pthread_mutex_destroy(&(this->mutex));
free(this);
}
/*
* Described in header.
*/
tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
{
private_tester_t *this = malloc_thing(private_tester_t);
/* public functions */
this->protected.public.destroy = (void (*) (tester_t *))destroy;
this->protected.public.perform_tests = (void (*) (tester_t *, test_t**)) perform_tests;
this->protected.public.perform_test = (void (*) (tester_t *, test_t*))perform_test;
this->protected.assert_true = (void (*) (protected_tester_t *, bool, char*)) assert_true;
this->protected.assert_false = (void (*) (protected_tester_t *, bool, char*)) assert_false;
/* private functions */
this->run_test = run_test;
this->time_difference = time_difference;
/* private data */
this->display_succeeded_asserts = display_succeeded_asserts;
this->failed_tests_count = 0;
this->tests_count = 0;
this->output = output;
pthread_mutex_init(&(this->mutex),NULL);
return &(this->protected.public);
}
-149
View File
@@ -1,149 +0,0 @@
/**
* @file tester.h
*
* @brief Interface of tester_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 TESTER_H_
#define TESTER_H_
#include <stdio.h>
#include <types.h>
/* must be defined here cause it is used in test_t */
typedef struct protected_tester_t protected_tester_t;
typedef struct test_t test_t;
/**
* @brief Representing a specified test.
*
* @ingroup utils
*/
struct test_t {
/**
* Testfunction called for this test.
*
* @param tester associated tester_t object
*/
void (*test_function) (protected_tester_t * tester);
/**
* Name of the test.
*/
char * test_name;
};
typedef struct tester_t tester_t;
/**
* @brief A class to perform tests.
*
* @b Constructors:
* - tester_create()
*
* @ingroup utils
*/
struct tester_t {
/**
* @brief Test all testcases in array tests with specific tester_t object.
*
* @param tester tester_t object
* @param tests pointer to an array of test_t-pointers.
* The last item has to be NULL to mark end of array.
*/
void (*perform_tests) (tester_t *tester,test_t **tests);
/**
* @brief Run a specific test case.
*
* @param this tester_t object
* @param test pointer to a test_t object which will be performed
*/
void (*perform_test) (tester_t *tester, test_t *test);
/**
* @brief Destroys a tester_t object.
*
* @param tester tester_t object
*/
void (*destroy) (tester_t *tester);
};
/**
* @brief A class used in a specific testcase.
*
* For each testcase an object of this type is passed to the testfunction. The testfunction uses this
* object to check specific asserts with protected_tester_t.assert_true and protected_tester_t.assert_false.
*
* @b Constructors:
* - tester_create()
*
* @ingroup utils
*/
struct protected_tester_t {
/**
* Public functions of a tester_t object
*/
tester_t public;
/**
* @brief Is called in a testcase to check a specific situation for TRUE.
*
* Log-Values to the tester output are protected from multiple access.
*
* @param this tester_t object
* @param to_be_true assert which has to be TRUE
* @param assert_name name of the assertion
*/
void (*assert_true) (protected_tester_t *tester, bool to_be_true, char *assert_name);
/**
* @brief Is called in a testcase to check a specific situation for FALSE.
*
* Log-Values to the tester output are protected from multiple access.
*
* @param this tester_t object
* @param to_be_false assert which has to be FALSE
* @param assert_name name of the assertion
*/
void (*assert_false) (protected_tester_t *tester, bool to_be_false, char *assert_name);
};
/**
* @brief Creates a tester_t object used to perform tests with.
*
* @param output test output is written to this output.
* @param display_succeeded_asserts has to be TRUE, if all asserts should be displayed,
* FALSE otherwise
*
* @return tester_t object
*
* @ingroup utils
*/
tester_t *tester_create(FILE *output, bool display_succeeded_asserts);
#endif /*TESTER_H_*/