renamed some IKEv2 OpenSSL scenarios
[strongswan.git] / src / charon / bus / listeners / sys_logger.c
index 3577376..5bcf28f 100644 (file)
@@ -1,10 +1,3 @@
-/**
- * @file sys_logger.c
- *
- * @brief Implementation of sys_logger_t.
- *
- */
-
 /*
  * Copyright (C) 2006 Martin Willi
  * Hochschule fuer Technik Rapperswil
  * 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;
 
@@ -49,99 +38,58 @@ struct private_sys_logger_t {
        int facility;
        
        /**
-        * Internal used stream logger that does the dirty work
+        * Maximum level to log, for each group
         */
-       stream_logger_t *logger;
-       
-       /**
-        * Memory stream used for stream_logger
-        */
-       FILE *stream;
-       
-       /**
-        * Underlying buffer for stream
-        */
-       char buffer[4096];
+       level_t levels[DBG_MAX];
 };
 
-
 /**
- * Implementation of bus_listener_t.signal.
+ * Implementation of listener_t.log.
  */
-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)
+static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
+                                int thread, ike_sa_t* ike_sa, char *format, va_list args)
 {
-       char line[512];
-       char *prefix;
-       FILE *reader;
-       
-       switch (signal)
+       if (level <= this->levels[group])
        {
-               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')
+               char buffer[8192];
+               char *current = buffer, *next;
+               
+               /* write in memory buffer first */
+               vsnprintf(buffer, sizeof(buffer), format, args);
+               
+               /* do a syslog with every line */
+               while (current)
                {
-                       /* abort on EOF */
-                       break;
-               }
-               else if (line[0] != '\n')
-               {
-                       syslog(this->facility|LOG_INFO, "%.2d[%s] %s", thread, prefix, line);
+                       next = strchr(current, '\n');
+                       if (next)
+                       {
+                               *(next++) = '\0';
+                       }
+                       syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
+                                  thread, debug_names, group, current);
+                       current = next;
                }
        }
-       fclose(reader);
-       funlockfile(this->stream);
+       /* always stay registered */
+       return TRUE;
 }
 
 /**
  * Implementation of sys_logger_t.set_level.
  */
-static void set_level(private_sys_logger_t *this, signal_t signal, level_t max)
+static void set_level(private_sys_logger_t *this, debug_t group, level_t level)
 {
-       this->logger->set_level(this->logger, signal, max);
+       if (group < DBG_ANY)
+       {
+               this->levels[group] = level;
+       }
+       else
+       {
+               for (group = 0; group < DBG_MAX; group++)
+               {
+                       this->levels[group] = level;
+               }
+       }
 }
 
 /**
@@ -150,8 +98,6 @@ static void set_level(private_sys_logger_t *this, signal_t signal, level_t max)
 static void destroy(private_sys_logger_t *this)
 {
        closelog();
-       fclose(this->stream);
-       this->logger->destroy(this->logger);
        free(this);
 }
 
@@ -163,19 +109,14 @@ 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;
+       memset(&this->public.listener, 0, sizeof(listener_t));
+       this->public.listener.log = (bool(*)(listener_t*,debug_t,level_t,int,ike_sa_t*,char*,va_list))log_;
+       this->public.set_level = (void(*)(sys_logger_t*,debug_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);
+       set_level(this, DBG_ANY, LEVEL_SILENT);
        
        return &this->public;
 }