- wrote logger_manager
authorJan Hutter <jhutter@hsr.ch>
Fri, 11 Nov 2005 10:29:32 +0000 (10:29 -0000)
committerJan Hutter <jhutter@hsr.ch>
Fri, 11 Nov 2005 10:29:32 +0000 (10:29 -0000)
15 files changed:
Source/charon/allocator.c
Source/charon/allocator.h
Source/charon/daemon.c
Source/charon/globals.h
Source/charon/logger.c
Source/charon/logger.h
Source/charon/logger_manager.c [new file with mode: 0644]
Source/charon/logger_manager.h [new file with mode: 0644]
Source/charon/parser.c
Source/charon/tester.h
Source/charon/tests.c
Source/charon/tests/generator_test.c
Source/charon/tests/parser_test.c
Source/charon/thread_pool.c
Source/charon/types.h

index c144911..26b3e5d 100644 (file)
 #include <string.h>
 #include <assert.h>
 #include <stdio.h>
-#ifndef ALLOCATOR_C_
-#define ALLOCATOR_C_ 
-#endif
 
 #include "allocator.h"
 #include "types.h"
index e0611b8..6ff1d23 100644 (file)
                void (*report_memory_leaks) (allocator_t *this);
        };
 
-       #ifndef ALLOCATOR_C_
                
-               /**
-                * @brief Global allocater_t object.
-                
-                * Only accessed over macros.
-                */
-               extern allocator_t *global_allocator;
-       #endif
+       /**
+        * @brief Global allocater_t object.
+        * 
+        * Only accessed over macros.
+        */
+       extern allocator_t *global_allocator;
+
        
        /**
         * Macro to allocate some memory
index 269cf75..930fbf6 100644 (file)
@@ -22,6 +22,7 @@
  
 #include <stdio.h>
  
+
 #include "types.h"
 #include "tester.h"
 #include "job_queue.h"
index 17282f0..50742b2 100644 (file)
 #include "job_queue.h"
 #include "event_queue.h"
 #include "socket.h"
+#include "logger_manager.h"
 
 
 extern send_queue_t *global_send_queue;
 extern job_queue_t *global_job_queue;
 extern event_queue_t *global_event_queue;
 extern socket_t *global_socket;
+extern logger_manager_t *global_logger_manager;
 
 #endif /*GLOBALS_H_*/
index da0f85c..98f69ab 100644 (file)
  * for more details.
  */
 
-
-
-#include "logger.h"
-#include "types.h"
-#include "allocator.h"
-
 #include <syslog.h>
 #include <stdarg.h>
 #include <string.h>
 #include <stdio.h>
+#include <time.h>
+
+#include "logger.h"
+#include "daemon.h"
+#include "types.h"
+#include "allocator.h"
 
 /**
  * Maximum length of al log entry (only used for logger_s.log)
@@ -37,7 +37,7 @@
 #define MAX_LOG 8192
 
 /**
- * @brief The logger object
+ * @brief The logger object.
  */
 typedef struct private_logger_s private_logger_t;
 struct private_logger_s {      
@@ -46,39 +46,80 @@ struct private_logger_s {
         */
        logger_t public;
        /**
-        * detail-level of logger
+        * Detail-level of logger.
         */
        logger_level_t level;
        /**
-        * name of logger
+        * Name of logger.
         */
        char *name;
+       /**
+        * File to write log output to .
+        * NULL for syslog.
+        */
+       FILE *output;
+       
+       /* private functions */
+       /**
+        * Logs a message to the associated log file.
+        */
+       void (*log_to_file) (private_logger_t *this, char *format, ...);
 };
 
-
 /**
- * implements logger_t-function log
- * @see logger_s.log
+ * Implements logger_t-function log.
+ * @see logger_s.log.
+ * 
+ * Yes, logg is wrong written :-).
  */
 static status_t logg(private_logger_t *this, logger_level_t loglevel, char *format, ...)
 {
        if ((this->level & loglevel) == loglevel)
        {
                char buffer[MAX_LOG];
-               snprintf(buffer, MAX_LOG, "%s: %s", this->name, format);
                va_list args;
-               va_start(args, format);
-               vsyslog(LOG_INFO, buffer, args);
-               va_end(args);   
-       }
 
+               if (this->output == NULL)
+               {
+                       /* syslog */
+                       snprintf(buffer, MAX_LOG, "%s: %s", this->name, format);
+                       va_start(args, format);
+                       vsyslog(LOG_INFO, buffer, args);
+                       va_end(args);
+               }
+               else
+               {
+                       /* File output */
+                       snprintf(buffer, MAX_LOG, "File %s: %s", this->name, format);
+                       va_start(args, format);
+                       this->log_to_file(this, buffer, args);
+                       va_end(args);
+               }
+
+       }
        return SUCCESS;
 }
 
+/**
+ * Implements private_logger_t-function log_to_file.
+ * @see private_logger_s.log_to_file.
+ */
+static void log_to_file(private_logger_t *this,char *format, ...)
+{
+       char buffer[MAX_LOG];
+       va_list args;
+       time_t current_time;
+       current_time = time(NULL);
+                       
+       snprintf(buffer, MAX_LOG, "%s\n", format);
+       va_start(args, format);
+       vfprintf(this->output, buffer, args);
+       va_end(args);
+}
 
 /**
- * implements logger_t-function destroy
- * @see logger_s.log_bytes
+ * Implements logger_t-function destroy.
+ * @see logger_s.log_bytes.
  */
 static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len)
 {
@@ -89,7 +130,13 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
                char *bytes_pos, *bytes_roof;
                int i;
 
-               syslog(LOG_INFO, "%s: %s (%d bytes)", this->name, label, len);  
+               if (this->output == NULL)
+               {
+                       syslog(LOG_INFO, "%s: %s (%d bytes)", this->name, label, len);  
+               }else
+               {
+                       this->log_to_file(this,"%s: %s (%d bytes)", this->name, label, len);
+               }
        
                bytes_pos = bytes;
                bytes_roof = bytes + len;
@@ -104,7 +151,14 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
                        {
                                *buffer_pos++ = '\0';
                                buffer_pos = buffer;
-                               syslog(LOG_INFO, "| %s", buffer);       
+                               if (this->output == NULL)
+                               {
+                                       syslog(LOG_INFO, "| %s", buffer);       
+                               }
+                               else
+                               {
+                                       this->log_to_file(this, "| %s", buffer);
+                               }
                        }
                        else if ((i % 8) == 0)
                        {
@@ -127,7 +181,14 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
                
                *buffer_pos++ = '\0';
                buffer_pos = buffer;
-               syslog(LOG_INFO, "| %s", buffer);       
+               if (this->output == NULL)
+               {               
+                       syslog(LOG_INFO, "| %s", buffer);
+               }
+               else
+               {
+                       this->log_to_file(this, "| %s", buffer);
+               }
        }
 
        return SUCCESS;
@@ -135,8 +196,8 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
 
 
 /**
- * implements logger_t-function log_chunk
- * @see logger_s.log_chunk
+ * Implements logger_t-function log_chunk.
+ * @see logger_s.log_chunk.
  */
 static status_t log_chunk(logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk)
 {
@@ -146,8 +207,8 @@ static status_t log_chunk(logger_t *this, logger_level_t loglevel, char *label,
 
 
 /**
- * implements logger_t-function enable_level
- * @see logger_s.enable_level
+ * Implements logger_t-function enable_level.
+ * @see logger_s.enable_level.
  */
 static status_t enable_level(private_logger_t *this, logger_level_t log_level)
 {
@@ -156,8 +217,8 @@ static status_t enable_level(private_logger_t *this, logger_level_t log_level)
 }
 
 /**
- * implements logger_t-function disable_level
- * @see logger_s.disable_level
+ * Implements logger_t-function disable_level.
+ * @see logger_s.disable_level.
  */
 static status_t disable_level(private_logger_t *this, logger_level_t log_level)
 {
@@ -166,8 +227,8 @@ static status_t disable_level(private_logger_t *this, logger_level_t log_level)
 }
 
 /**
- * implements logger_t-function destroy
- * @see logger_s.destroy
+ * Implements logger_t-function destroy.
+ * @see logger_s.destroy.
  */
 static status_t destroy(private_logger_t *this)
 {
@@ -178,7 +239,7 @@ static status_t destroy(private_logger_t *this)
 /*
  * Described in Header
  */    
-logger_t *logger_create(char *logger_name, logger_level_t log_level)
+logger_t *logger_create(char *logger_name, logger_level_t log_level,FILE * output)
 {
        private_logger_t *this = allocator_alloc_thing(private_logger_t);
                
@@ -194,10 +255,18 @@ logger_t *logger_create(char *logger_name, logger_level_t log_level)
        this->public.disable_level = (status_t(*)(logger_t*,logger_level_t))disable_level;
        this->public.destroy = (status_t(*)(logger_t*))destroy;
 
+       this->log_to_file = log_to_file;
+
+       /* private variables */
        this->level = log_level;
        this->name = logger_name;
+       this->output = output;
+
        
-       openlog("charon", 0, LOG_DAEMON);
+       if (output == NULL)
+       {
+               openlog(DEAMON_NAME, 0, LOG_DAEMON);
+       }
        
        return (logger_t*)this;
 }
index 035ddfe..457ab05 100644 (file)
@@ -23,6 +23,7 @@
 #ifndef LOGGER_H_
 #define LOGGER_H_
 
+#include <stdio.h>
 #include "types.h"
 
 /**
@@ -144,9 +145,10 @@ struct logger_s {
  *
  * @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 output                       FILE * if log has to go on a file output, NULL for syslog
  * @return                             logger_t object or NULL if failed
  */
-logger_t *logger_create(char *logger_name, logger_level_t log_level);
+logger_t *logger_create(char *logger_name, logger_level_t log_level,FILE * output);
 
 
 #endif /*LOGGER_H_*/
diff --git a/Source/charon/logger_manager.c b/Source/charon/logger_manager.c
new file mode 100644 (file)
index 0000000..5e331e7
--- /dev/null
@@ -0,0 +1,448 @@
+/**
+ * @file logger_manager.c
+ *
+ * @brief Logger manager. Manages globaly all logger objects
+ *
+ */
+
+/*
+ * Copyright (C) 2005 Jan Hutter, 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 "allocator.h"
+#include "logger_manager.h"
+#include "linked_list.h"
+
+/** 
+ * Maximum length of a logger name
+ */
+#define MAX_LOGGER_NAME 30
+
+typedef struct private_logger_manager_s private_logger_manager_t;
+struct private_logger_manager_s {      
+       /**
+        * Public data.
+        */
+       logger_manager_t public;
+
+       /**
+        * Managed loggers.
+        */
+       linked_list_t *loggers;
+       
+       /**
+        * Log Levels.
+        */
+       linked_list_t *logger_levels;
+       
+       /**
+        * Used to manage logger list.
+        */
+       pthread_mutex_t mutex;
+       
+       /**
+        * Default logger level for a created logger used if no specific logger_level is set
+        */
+       logger_level_t default_log_level;
+       
+       /**
+        * Sets set logger_level of a specific context.
+        * @param this                  calling object
+        * @param context               context to set level
+        * @param logger_level  logger_level to set
+        * @param enable                enable specific level or disable it
+        * @return SUCCESS
+        */
+       status_t (*set_logger_level) (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable);
+       
+};
+
+/**
+ * Entry in the logger_levels linked list
+ */
+typedef struct logger_levels_entry_s logger_levels_entry_t;
+
+struct logger_levels_entry_s{
+       logger_context_t context;
+       logger_level_t level;
+};
+
+/**
+ * Entry in the loggers linked list
+ */
+typedef struct loggers_entry_s loggers_entry_t;
+
+struct loggers_entry_s{
+       logger_context_t context;
+       logger_t *logger;
+};
+
+/**
+ * Implements logger_manager_t-function get_logger.
+ * @see logger_manager_s.get_logger.
+ */
+static status_t get_logger (private_logger_manager_t *this, logger_context_t context, logger_t **logger,char * name)
+{
+       
+       char * context_name;
+       FILE * output = NULL;
+       char buffer[MAX_LOGGER_NAME];
+       loggers_entry_t *entry;
+       logger_level_t logger_level = this->public.get_logger_level(&(this->public),context);
+
+       switch(context)
+       {
+               case PARSER:
+                       context_name = "PARSER";
+                       break;
+               case GENERATOR:
+                       context_name = "GENERATOR";
+                       break;
+               case IKE_SA:
+                       context_name = "IKE_SA";
+                       break;
+               case MESSAGE:
+                       context_name = "MESSAGE";
+                       break;
+               case WORKER_THREAD:
+                       context_name = "WORKER_THREAD";
+                       break;
+               case EVENT_THREAD:
+                       context_name = "EVENT_THREAD";
+                       break;
+               case SENDER_THREAD:
+                       context_name = "SENDER_THREAD";
+                       break;
+               case RECEIVER_THREAD:
+                       context_name = "RECEIVER_THREAD";
+                       break;
+               case TESTER:
+                       context_name = "TESTER";
+                       output = stdout;
+                       break;
+               default:
+                       context_name = "NO CONTEXT";
+                       break;          
+       }
+       
+       pthread_mutex_lock(&(this->mutex));
+       snprintf(buffer, MAX_LOGGER_NAME, "%s - %s",context_name,name);
+       
+       /* create logger with default log_level */
+       *logger = logger_create(buffer,logger_level,output);
+       
+       if (*logger == NULL)
+       {
+               pthread_mutex_unlock(&(this->mutex));           
+               return OUT_OF_RES;
+       }
+
+       entry = allocator_alloc_thing(loggers_entry_t);
+       
+       if (entry == NULL)
+       {
+               (*logger)->destroy(*logger);
+               pthread_mutex_unlock(&(this->mutex));           
+               return OUT_OF_RES;
+       }
+
+       entry->context = context;
+       entry->logger = *logger;
+
+       if (this->loggers->insert_last(this->loggers,entry) != SUCCESS)
+       {
+               allocator_free(entry);
+               (*logger)->destroy(*logger);
+               pthread_mutex_unlock(&(this->mutex));           
+               return OUT_OF_RES;
+       }
+
+       pthread_mutex_unlock(&(this->mutex));
+       return SUCCESS;
+       
+}
+
+static logger_level_t get_logger_level (private_logger_manager_t *this, logger_context_t context)
+{
+       linked_list_iterator_t *iterator;
+       logger_level_t logger_level = this->default_log_level;
+
+       pthread_mutex_lock(&(this->mutex));
+
+       if (this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE) != SUCCESS)
+       {
+               pthread_mutex_unlock(&(this->mutex));
+               return logger_level;
+       }
+       while (iterator->has_next(iterator))
+       {
+               
+               logger_levels_entry_t * entry;
+               if (iterator->current(iterator,(void **)&entry) != SUCCESS)
+               {       
+                       break;
+               }
+               if (entry->context == context)
+               {
+                       logger_level = entry->level;
+                       break;
+               }
+       }
+       
+       iterator->destroy(iterator);
+
+       pthread_mutex_unlock(&(this->mutex));
+       return logger_level;
+}
+
+/**
+ * Implements logger_manager_t-function destroy_logger.
+ * @see logger_manager_s.destroy_logger.
+ */
+static status_t destroy_logger (private_logger_manager_t *this,logger_t *logger)
+{
+       
+       linked_list_iterator_t *iterator;
+       status_t status;
+       
+       pthread_mutex_lock(&(this->mutex));
+       if (this->loggers->create_iterator(this->loggers,&iterator,TRUE) != SUCCESS)
+       {
+               pthread_mutex_unlock(&(this->mutex));
+               return OUT_OF_RES;
+       }
+
+       while (iterator->has_next(iterator))
+       {
+               
+               loggers_entry_t * entry;
+               status = iterator->current(iterator,(void **)&entry);
+               if (status != SUCCESS)
+               {       
+                       break;
+               }
+               status = NOT_FOUND;
+               if (entry->logger == logger)
+               {
+                       this->loggers->remove(this->loggers,iterator);
+                       allocator_free(entry);
+                       logger->destroy(logger);
+                       status = SUCCESS;
+                       break; 
+               }
+       }
+       iterator->destroy(iterator);
+       pthread_mutex_unlock(&(this->mutex));
+       return status;
+}
+
+/**
+ * Implements private_logger_manager_t-function set_logger_level.
+ * @see private_logger_manager_s.set_logger_level.
+ */
+static status_t set_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable)
+{
+       
+       linked_list_iterator_t *iterator;
+       status_t status;
+       
+       pthread_mutex_lock(&(this->mutex));
+       if (this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE) != SUCCESS)
+       {
+               pthread_mutex_unlock(&(this->mutex));
+               return OUT_OF_RES;
+       }
+
+       status = NOT_FOUND;
+       while (iterator->has_next(iterator))
+       {       
+               logger_levels_entry_t * entry;
+               status = iterator->current(iterator,(void **)&entry);
+               if (status != SUCCESS)
+               {       
+                       iterator->destroy(iterator);
+                       pthread_mutex_unlock(&(this->mutex));
+                       return status;
+               }
+               status = NOT_FOUND;
+               if (entry->context == context)
+               {
+                       if (enable)
+                       {
+                               entry->level |= logger_level;
+                       }
+                       else
+                       {
+                               entry->level &= ~logger_level;
+                       }
+
+                       status = SUCCESS;
+                       break; 
+               }
+       }
+       iterator->destroy(iterator);
+       
+       if (status == NOT_FOUND)
+       {
+
+               logger_levels_entry_t *entry = allocator_alloc_thing(logger_levels_entry_t);
+               if (entry == NULL)
+               {
+                       pthread_mutex_unlock(&(this->mutex));
+                       return OUT_OF_RES;
+               }
+               entry->context = context;
+               entry->level =  (enable) ? logger_level : this->default_log_level;
+
+               status = this->logger_levels->insert_last(this->logger_levels,entry);
+               if (status != SUCCESS)
+               {
+                       pthread_mutex_unlock(&(this->mutex));
+                       return status;
+               }
+       }
+       
+       if (this->loggers->create_iterator(this->loggers,&iterator,TRUE) != SUCCESS)
+       {
+               pthread_mutex_unlock(&(this->mutex));
+               return OUT_OF_RES;
+       }
+
+       while (iterator->has_next(iterator))
+       {
+               
+               loggers_entry_t * entry;
+               status = iterator->current(iterator,(void **)&entry);
+               if (status != SUCCESS)
+               {       
+                       iterator->destroy(iterator);
+                       pthread_mutex_unlock(&(this->mutex));
+                       return status;
+               }
+               if (entry->context == context)
+               {
+                       if (enable)
+                       {
+                               status = entry->logger->enable_level(entry->logger,logger_level);
+                       }
+                       else
+                       {
+                               status = entry->logger->disable_level(entry->logger,logger_level);
+                       }
+                       
+               }
+       }
+
+       iterator->destroy(iterator);
+       pthread_mutex_unlock(&(this->mutex));
+       return SUCCESS;
+}
+
+/**
+ * Implements logger_manager_t-function enable_logger_level.
+ * @see logger_manager_s.enable_logger_level.
+ */
+static status_t enable_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level)
+{
+       return set_logger_level(this,context,logger_level,TRUE);
+}
+
+/**
+ * Implements logger_manager_t-function disable_logger_level.
+ * @see logger_manager_s.disable_logger_level.
+ */
+static status_t disable_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level)
+{
+       return set_logger_level(this,context,logger_level,FALSE);
+}
+
+/**
+ * Implements logger_manager_t-function destroy.
+ * @see logger_manager_s.destroy.
+ */
+static status_t destroy(private_logger_manager_t *this)
+{
+       while (this->loggers->get_count(this->loggers) > 0)
+       {
+               loggers_entry_t *current_entry;
+               
+               this->loggers->remove_first(this->loggers,(void **)&current_entry);
+               
+               /* destroy logger object */
+               current_entry->logger->destroy(current_entry->logger);
+               
+               /* entry can be destroyed */
+               allocator_free(current_entry);          
+       }
+       
+       while (this->logger_levels->get_count(this->logger_levels) > 0)
+       {
+               logger_levels_entry_t *current_entry;
+               
+               this->logger_levels->remove_first(this->logger_levels,(void **)&current_entry);
+               
+               /* entry can be destroyed */
+               allocator_free(current_entry);
+       }
+       
+       this->loggers->destroy(this->loggers);
+       this->logger_levels->destroy(this->logger_levels);
+       pthread_mutex_destroy(&(this->mutex));
+       
+       allocator_free(this);
+       return SUCCESS;
+}
+
+/*
+ * Described in header
+ */
+logger_manager_t *logger_manager_create(logger_level_t default_log_level)
+{
+       private_logger_manager_t *this = allocator_alloc_thing(private_logger_manager_t);
+               
+       if (this == NULL)
+       {
+               return NULL;    
+       }
+
+       this->public.get_logger = (status_t(*)(logger_manager_t*,logger_context_t context, logger_t **logger, char *))get_logger;
+       this->public.destroy_logger = (status_t(*)(logger_manager_t*,logger_t *logger))destroy_logger;
+       this->public.destroy = (status_t(*)(logger_manager_t*))destroy;
+       this->public.get_logger_level = (logger_level_t (*)(logger_manager_t *, logger_context_t)) get_logger_level;
+       this->public.enable_logger_level = (status_t (*)(logger_manager_t *, logger_context_t,logger_level_t)) enable_logger_level;
+       this->public.disable_logger_level = (status_t (*)(logger_manager_t *, logger_context_t,logger_level_t)) disable_logger_level;
+       this->set_logger_level = (status_t (*)(private_logger_manager_t *, logger_context_t,logger_level_t,bool)) set_logger_level;
+       
+       /* private variables */
+       this->loggers = linked_list_create();
+       
+       if (this->loggers == NULL)
+       {
+               allocator_free(this);
+               return NULL;
+       }
+       this->logger_levels = linked_list_create();
+       if (this->logger_levels == NULL)
+       {
+               this->loggers->destroy(this->loggers);
+               allocator_free(this);
+               return NULL;
+       }
+       this->default_log_level = default_log_level;
+       
+       pthread_mutex_init(&(this->mutex), NULL);
+
+       return (logger_manager_t*)this;
+}
+
diff --git a/Source/charon/logger_manager.h b/Source/charon/logger_manager.h
new file mode 100644 (file)
index 0000000..920a51d
--- /dev/null
@@ -0,0 +1,139 @@
+/**
+ * @file logger_manager.h
+ *
+ * @brief Logger manager. Manages globaly all logger objects
+ *
+ */
+
+/*
+ * Copyright (C) 2005 Jan Hutter, 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 LOGGER_MANAGER_H_
+#define LOGGER_MANAGER_H_
+
+#include <pthread.h>
+
+#include "logger.h"
+
+/**
+ * @brief Context of a specific logger 
+ */
+typedef enum logger_context_e logger_context_t;
+
+enum logger_context_e{
+       PARSER,
+       GENERATOR,
+       IKE_SA,
+       MESSAGE,
+       WORKER_THREAD,
+       EVENT_THREAD,
+       SENDER_THREAD,
+       RECEIVER_THREAD,
+       TESTER,
+};
+
+
+/**
+ * @brief The logger_manager_t object
+ */
+typedef struct logger_manager_s logger_manager_t;
+
+struct logger_manager_s {
+       
+       /**
+        * @brief Gets a logger_t object for a specific logger context.
+        * 
+        * @warning logger_t objects which are not destroyed over function
+        * #logger_manager_s.destroy_logger are destroyed in logger_managers 
+        * destroy function. Don't use logger_t's own destroy function with 
+        * managed logger_t objects.
+        *
+        * @param this                  logger_manager_t object
+        * @param context               logger_context to use the logger for.
+        * @param[out] logger   pointer to a a place where the new logger is stored
+        * @param name                  name for the new logger. Context name is already included 
+        *                                              and has not to be specified.
+        * @return
+        *                                              - SUCCESS
+        *                                              - OUT_OF_RES
+        */
+       status_t (*get_logger) (logger_manager_t *this, logger_context_t context, logger_t **logger, char *name);
+       
+       
+       /**
+        * @brief Destroys a logger_t object which is not used anymore
+        * 
+        * @warning logger_t objects which are not destroyed over function
+        * #logger_manager_s.destroy_logger are destroyed in logger_managers 
+        * destroy function.
+        *
+        * @param this          logger_manager_t object
+        * @param logger                pointer to the logger which has to be destroyed
+        * @return
+        *                                      - SUCCESS
+        *                                      - OUT_OF_RES
+        *                                      - NOT_FOUND
+        */
+       status_t (*destroy_logger) (logger_manager_t *this,logger_t *logger);
+       
+       /**
+        * Returns the set logger_level of a specific context or 0.
+        * @param this                  calling object
+        * @param context               context to check level
+        * @return logger_level         for the given logger_context
+        */
+       logger_level_t (*get_logger_level) (logger_manager_t *this, logger_context_t context);
+       
+       /**
+        * Enables a logger_level of a specific context.
+        * @param this                  calling object
+        * @param context               context to set level
+        * @param logger_level  logger_level to eanble
+        * @return SUCCESS
+        */
+       status_t (*enable_logger_level) (logger_manager_t *this, logger_context_t context,logger_level_t logger_level);
+               
+
+       /**
+        * Disables a logger_level of a specific context.
+        * @param this                  calling object
+        * @param context               context to set level
+        * @param logger_level  logger_level to disable
+        * @return SUCCESS
+        */
+       status_t (*disable_logger_level) (logger_manager_t *this, logger_context_t context,logger_level_t logger_level);
+
+
+       /**
+        * @brief destroys a logger_manager_t object.
+        *
+        * @param this          logger_manager_t object
+        * @return
+        *                                      - SUCCESS in any case
+        */
+       status_t (*destroy) (logger_manager_t *this);
+};
+
+/**
+ * @brief Constructor to create a logger_manager_t object.
+ *
+ * @param default_log_level    default log level for a context
+ * @return                                     logger_manager_t object or NULL if failed
+ * 
+ */
+logger_manager_t *logger_manager_create(logger_level_t default_log_level);
+
+
+#endif /*LOGGER_MANAGER_H_*/
index 3755a6e..ecc0b23 100644 (file)
@@ -397,7 +397,7 @@ parser_t *parser_create(payload_info_t **payload_infos)
                return NULL;
        }
        
-       this->logger = logger_create("parser", ALL);
+       this->logger = logger_create("parser", ALL,NULL);
        if (this->logger == NULL)
        {
                allocator_free(this);
index 8d174fb..56bd366 100644 (file)
@@ -28,6 +28,7 @@
 #include "types.h"
 
 
+
 /**
  * @brief Specifies a test
  */
index 8d9e8fe..20efb75 100644 (file)
@@ -23,7 +23,7 @@
  
 #include <stdio.h>
 
-#include "logger.h"
+#include "logger_manager.h"
 #include "allocator.h"
 #include "tester.h"
 #include "job_queue.h"
@@ -167,7 +167,7 @@ socket_t *global_socket;
 /**
  * Global logger
  */
-logger_t *global_logger;
+logger_manager_t *global_logger_manager;
   
  int main()
 {
@@ -194,7 +194,7 @@ logger_t *global_logger;
        &packet_test,
        NULL
        };
-       global_logger = logger_create("Tester",ALL);
+       global_logger_manager = logger_manager_create(CONTROL);
        
        global_socket = socket_create(4600);
        
@@ -207,7 +207,7 @@ logger_t *global_logger;
        tester_t *tester = tester_create(test_output, FALSE);
 
        tester->perform_tests(tester,all_tests);
-       /*tester->perform_test(tester,&parser_test);   */
+/*     tester->perform_test(tester,&parser_test);   */
        
        tester->destroy(tester);
 
@@ -219,7 +219,7 @@ logger_t *global_logger;
        
        global_socket->destroy(global_socket);
        
-       global_logger->destroy(global_logger);
+       global_logger_manager->destroy(global_logger_manager);
        
 #ifdef LEAK_DETECTIVE
        /* Leaks are reported on stderr */
index 9c1d00e..380ae55 100644 (file)
@@ -22,7 +22,9 @@
  
 #include <string.h>
 
+#include "../globals.h"
 #include "../allocator.h"
+#include "../logger_manager.h"
 #include "generator_test.h"
 #include "../tester.h"
 #include "../logger.h"
@@ -32,7 +34,6 @@
 
 extern payload_info_t *payload_infos[];
 
-extern logger_t *global_logger;
 /*
  * Described in Header 
  */
@@ -41,7 +42,6 @@ void test_generator_with_unsupported_payload(tester_t *tester)
        generator_t *generator;
        generator_context_t *generator_context;
        void * data_struct;
-       chunk_t generated_data;
        
        generator = generator_create(payload_infos);
        tester->assert_true(tester,(generator != NULL), "generator create check");
@@ -65,6 +65,9 @@ void test_generator_with_header_payload(tester_t *tester)
        ike_header_t header_data;
        chunk_t generated_data;
        status_t status;
+       logger_t *logger;
+       
+       global_logger_manager->get_logger(global_logger_manager,TESTER,&logger,"header payload");
        
        header_data.initiator_spi = 1;
        header_data.responder_spi = 2;
@@ -101,7 +104,7 @@ void test_generator_with_header_payload(tester_t *tester)
 
 
        tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
-               
+       logger->log_chunk(logger,RAW,"generated header",&generated_data);               
        tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
        allocator_free_chunk(generated_data);
        generator_context->destroy(generator_context);
@@ -136,11 +139,12 @@ void test_generator_with_header_payload(tester_t *tester)
                0x00,0x0A,0xA1,0x1F,
        };
        
-       global_logger->log_chunk(global_logger,CONTROL,"generated header",&generated_data);
+       logger->log_chunk(logger,RAW,"generated header",&generated_data);
 
        tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
        allocator_free_chunk(generated_data);
        
        generator_context->destroy(generator_context);
+       global_logger_manager->destroy_logger(global_logger_manager,logger);
        tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
 }
index ad19d03..b3ad315 100644 (file)
@@ -24,7 +24,7 @@
 #include "../allocator.h"
 #include "parser_test.h"
 #include "../tester.h"
-#include "../logger.h"
+#include "../logger_manager.h"
 #include "../encodings.h"
 #include "../generator.h"
 #include "../parser.h"
@@ -32,7 +32,8 @@
 
 extern payload_info_t *payload_infos[];
 
-extern logger_t *global_logger;
+extern logger_manager_t *global_logger_manager;
+
 
 /*
  * Described in Header 
@@ -45,6 +46,10 @@ void test_parser_with_header_payload(tester_t *tester)
        status_t status;
        chunk_t test_chunk;
        
+       logger_t *logger;
+       
+       global_logger_manager->get_logger(global_logger_manager,TESTER,&logger,"header payload");
+       
        u_int8_t test_bytes[] = {
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x01,
@@ -83,7 +88,7 @@ void test_parser_with_header_payload(tester_t *tester)
        parser_context->destroy(parser_context);
        tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
        
-       global_logger->log_bytes(global_logger, RAW, "Header", (void*)header_data, sizeof(ike_header_t));
+       logger->log_bytes(logger, RAW, "Header", (void*)header_data, sizeof(ike_header_t));
        
        allocator_free(header_data);
 }
index 3eeccd4..76783ef 100644 (file)
@@ -143,7 +143,7 @@ thread_pool_t *thread_pool_create(size_t pool_size)
                allocator_free(this);
                return NULL;
        }       
-       this->logger = logger_create("thread_pool", ALL);
+       this->logger = logger_create("thread_pool", ALL,NULL);
        if (this->threads == NULL)
        {
                allocator_free(this);
index 96dd1a3..6abd44f 100644 (file)
@@ -70,7 +70,7 @@ struct chunk_s {
  */
 typedef int bool;
 #define FALSE  0
-#define TRUE   1
+#define TRUE           1
 
 
 #endif /*TYPES_H_*/