dynamic logging configuration through strongswan.conf
authorMartin Willi <martin@strongswan.org>
Tue, 11 Nov 2008 10:52:37 +0000 (10:52 -0000)
committerMartin Willi <martin@strongswan.org>
Tue, 11 Nov 2008 10:52:37 +0000 (10:52 -0000)
fallback to existing ipsec.conf/stroke loglevel configuration

src/charon/bus/bus.c
src/charon/bus/bus.h
src/charon/bus/listeners/file_logger.c
src/charon/daemon.c
src/charon/daemon.h
src/charon/plugins/stroke/stroke_socket.c

index 7ad929b..670c535 100644 (file)
@@ -36,6 +36,19 @@ ENUM(debug_names, DBG_DMN, DBG_LIB,
        "LIB",
 );
 
+ENUM(debug_lower_names, DBG_DMN, DBG_LIB,
+       "dmn",
+       "mgr",
+       "ike",
+       "chd",
+       "job",
+       "cfg",
+       "knl",
+       "net",
+       "enc",
+       "lib",
+);
+
 typedef struct private_bus_t private_bus_t;
 
 /**
index 23c4650..43a24ce 100644 (file)
@@ -70,6 +70,11 @@ enum debug_t {
 extern enum_name_t *debug_names;
 
 /**
+ * short names of debug message group, lower case.
+ */
+extern enum_name_t *debug_lower_names;
+
+/**
  * Debug levels used to control output verbosity.
  */
 enum level_t {
index 9ce8397..f8e651e 100644 (file)
@@ -98,6 +98,10 @@ static void set_level(private_file_logger_t *this, debug_t group, level_t level)
  */
 static void destroy(private_file_logger_t *this)
 {
+       if (this->out != stdout && this->out != stderr)
+       {
+               fclose(this->out);
+       }
        free(this);
 }
 
index d628248..c5c43e8 100644 (file)
@@ -205,9 +205,10 @@ static void destroy(private_daemon_t *this)
        /* rehook library logging, shutdown logging */
        dbg = dbg_stderr;
        DESTROY_IF(this->public.bus);
-       DESTROY_IF(this->public.outlog);
-       DESTROY_IF(this->public.syslog);
-       DESTROY_IF(this->public.authlog);
+       this->public.file_loggers->destroy_offset(this->public.file_loggers,
+                                                                                       offsetof(file_logger_t, destroy));
+       this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
+                                                                                       offsetof(sys_logger_t, destroy));
        free(this);
 }
 
@@ -328,38 +329,133 @@ static void print_plugins()
 }
 
 /**
- * Initialize the daemon
+ * Initialize logging
  */
-static bool initialize(private_daemon_t *this, bool syslog, level_t levels[])
+static void initialize_loggers(private_daemon_t *this, bool use_stderr,
+                                                          level_t levels[])
 {
+       sys_logger_t *sys_logger;
+       file_logger_t *file_logger;
+       enumerator_t *enumerator;
+       char *facility, *filename;
+       int loggers_defined = 0;
        debug_t group;
+       level_t  def;
+       bool append;
+       FILE *file;
+       
+       /* setup sysloggers */
+       enumerator = lib->settings->create_section_enumerator(lib->settings,
+                                                                                                                 "charon.syslog");
+       while (enumerator->enumerate(enumerator, &facility))
+       {
+               loggers_defined++;
+               if (streq(facility, "daemon"))
+               {
+                       sys_logger = sys_logger_create(LOG_DAEMON);
+               }
+               else if (streq(facility, "auth"))
+               {
+                       sys_logger = sys_logger_create(LOG_AUTHPRIV);
+               }
+               else
+               {
+                       continue;
+               }
+               def = lib->settings->get_int(lib->settings,
+                                                                        "charon.syslog.%s.default", 1, facility);
+               for (group = 0; group < DBG_MAX; group++)
+               {
+                       sys_logger->set_level(sys_logger, group,
+                               lib->settings->get_int(lib->settings,
+                                                                          "charon.syslog.%s.%N", def,
+                                                                          facility, debug_lower_names, group));
+               }
+               this->public.sys_loggers->insert_last(this->public.sys_loggers,
+                                                                                         sys_logger);
+               this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
+       }
+       enumerator->destroy(enumerator);
+       
+       /* and file loggers */
+       enumerator = lib->settings->create_section_enumerator(lib->settings,
+                                                                                                                 "charon.filelog");
+       while (enumerator->enumerate(enumerator, &filename))
+       {
+               loggers_defined++;
+               if (streq(filename, "stderr"))
+               {
+                       file = stderr;
+               }
+               else if (streq(filename, "stdout"))
+               {
+                       file = stdout;
+               }
+               else
+               {
+                       append = lib->settings->get_bool(lib->settings,
+                                                                       "charon.filelog.%s.append", TRUE, filename);
+                       file = fopen(filename, append ? "a" : "w");
+                       if (file == NULL)
+                       {
+                               DBG1(DBG_DMN, "opening file %s for logging failed: %s",
+                                        filename, strerror(errno));
+                               continue;
+                       }
+               }
+               file_logger = file_logger_create(file);
+               def = lib->settings->get_int(lib->settings,
+                                                                        "charon.filelog.%s.default", 1, filename);
+               for (group = 0; group < DBG_MAX; group++)
+               {
+                       file_logger->set_level(file_logger, group,
+                               lib->settings->get_int(lib->settings,
+                                                                          "charon.filelog.%s.%N", def,
+                                                                          filename, debug_lower_names, group));
+               }
+               this->public.file_loggers->insert_last(this->public.file_loggers,
+                                                                                          file_logger);
+               this->public.bus->add_listener(this->public.bus, &file_logger->listener);
+       
+       }
+       enumerator->destroy(enumerator);
        
+       /* setup legacy style default loggers provided via command-line */
+       if (!loggers_defined)
+       {
+               file_logger = file_logger_create(stdout);
+               sys_logger = sys_logger_create(LOG_DAEMON);
+               this->public.bus->add_listener(this->public.bus, &file_logger->listener);
+               this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
+               this->public.file_loggers->insert_last(this->public.file_loggers,
+                                                                                          file_logger);
+               this->public.sys_loggers->insert_last(this->public.sys_loggers,
+                                                                                         sys_logger);
+               for (group = 0; group < DBG_MAX; group++)
+               {
+                       sys_logger->set_level(sys_logger, group, levels[group]);
+                       if (use_stderr)
+                       {
+                               file_logger->set_level(file_logger, group, levels[group]);
+                       }
+               }
+       }
+}
+
+/**
+ * Initialize the daemon
+ */
+static bool initialize(private_daemon_t *this, bool syslog, level_t levels[])
+{
        /* for uncritical pseudo random numbers */
        srandom(time(NULL) + getpid());
        
        /* setup bus and it's listeners first to enable log output */
        this->public.bus = bus_create();
-       this->public.outlog = file_logger_create(stdout);
-       this->public.syslog = sys_logger_create(LOG_DAEMON);
-       this->public.authlog = sys_logger_create(LOG_AUTHPRIV);
-       this->public.bus->add_listener(this->public.bus, &this->public.syslog->listener);
-       this->public.bus->add_listener(this->public.bus, &this->public.outlog->listener);
-       this->public.bus->add_listener(this->public.bus, &this->public.authlog->listener);
-       this->public.authlog->set_level(this->public.authlog, DBG_ANY, LEVEL_AUDIT);
        /* set up hook to log dbg message in library via charons message bus */
        dbg = dbg_bus;
        
-       /* apply loglevels */
-       for (group = 0; group < DBG_MAX; group++)
-       {
-               this->public.syslog->set_level(this->public.syslog,
-                                                                          group, levels[group]);
-               if (!syslog)
-               {
-                       this->public.outlog->set_level(this->public.outlog,
-                                                                                  group, levels[group]);
-               }
-       }
+       initialize_loggers(this, !syslog, levels);
        
        DBG1(DBG_DMN, "starting charon (strongSwan Version %s)", VERSION);
 
@@ -464,9 +560,8 @@ private_daemon_t *daemon_create(void)
        this->public.eap = NULL;
        this->public.sim = NULL;
        this->public.bus = NULL;
-       this->public.outlog = NULL;
-       this->public.syslog = NULL;
-       this->public.authlog = NULL;
+       this->public.file_loggers = linked_list_create();
+       this->public.sys_loggers = linked_list_create();
 #ifdef ME
        this->public.connect_manager = NULL;
        this->public.mediation_manager = NULL;
index da7d4e6..f498d8a 100644 (file)
@@ -217,7 +217,7 @@ struct daemon_t {
        backend_manager_t *backends;
        
        /**
-        * Manager IKEv2 cfg payload attributes
+        * Manager for IKEv2 cfg payload attributes
         */
        attribute_manager_t *attributes;
        
@@ -252,19 +252,14 @@ struct daemon_t {
        bus_t *bus;
        
        /**
-        * A bus listener logging to stdout
+        * A list of installed file_logger_t's
         */
-       file_logger_t *outlog;
+       linked_list_t *file_loggers;
        
        /**
-        * A bus listener logging to syslog
+        * A list of installed sys_logger_t's
         */
-       sys_logger_t *syslog;
-       
-       /**
-        * A bus listener logging most important events
-        */
-       sys_logger_t *authlog;
+       linked_list_t *sys_loggers;
        
        /**
         * Kernel Interface to communicate with kernel
index 850b94b..8c4ab78 100644 (file)
@@ -358,6 +358,9 @@ debug_t get_group_from_name(char *type)
 static void stroke_loglevel(private_stroke_socket_t *this,
                                                        stroke_msg_t *msg, FILE *out)
 {
+       enumerator_t *enumerator;
+       sys_logger_t *sys_logger;
+       file_logger_t *file_logger;
        debug_t group;
        
        pop_string(msg, &(msg->loglevel.type));
@@ -370,9 +373,19 @@ static void stroke_loglevel(private_stroke_socket_t *this,
                fprintf(out, "invalid type (%s)!\n", msg->loglevel.type);
                return;
        }
-       
-       charon->outlog->set_level(charon->outlog, group, msg->loglevel.level);
-       charon->syslog->set_level(charon->syslog, group, msg->loglevel.level);
+       /* we set the loglevel on ALL sys- and file-loggers */
+       enumerator = charon->sys_loggers->create_enumerator(charon->sys_loggers);
+       while (enumerator->enumerate(enumerator, &sys_logger))
+       {
+               sys_logger->set_level(sys_logger, group, msg->loglevel.level);
+       }
+       enumerator->destroy(enumerator);
+       enumerator = charon->file_loggers->create_enumerator(charon->file_loggers);
+       while (enumerator->enumerate(enumerator, &file_logger))
+       {
+               file_logger->set_level(file_logger, group, msg->loglevel.level);
+       }
+       enumerator->destroy(enumerator);
 }
 
 /**